Implement PartialEq accross Insertable and Queryable diesel models.

This commit is contained in:
Jordan Petridis 2018-01-20 19:09:25 +02:00
parent 6c9e0984a5
commit da8c3a7827
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 36 additions and 32 deletions

View File

@ -6,7 +6,6 @@ use diesel::prelude::*;
use database::connection;
use errors::*;
use models::Podcast;
use models::new_episode::{NewEpisodeMinimal, NewEpisodeMinimalBuilder};
use schema::episode;
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
@ -443,20 +442,6 @@ impl From<Episode> for EpisodeMinimal {
}
}
impl Into<NewEpisodeMinimal> for EpisodeMinimal {
fn into(self) -> NewEpisodeMinimal {
NewEpisodeMinimalBuilder::default()
.title(self.title)
.uri(self.uri)
.guid(self.guid)
.epoch(self.epoch)
.duration(self.duration)
.podcast_id(self.podcast_id)
.build()
.unwrap()
}
}
impl EpisodeMinimal {
/// Get the value of the sqlite's `ROW_ID`
pub fn rowid(&self) -> i32 {

View File

@ -10,7 +10,7 @@ use rss;
use database::connection;
use dbqueries;
use errors::*;
use models::{Episode, Index, Insert, Update};
use models::{Episode, EpisodeMinimal, Index, Insert, Update};
use parser;
use utils::{replace_extra_spaces, url_cleaner};
@ -80,14 +80,10 @@ impl Index for NewEpisode {
let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?;
if exists {
let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?;
let other = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?;
// This is messy
if (self.title() != old.title()) || (self.uri() != old.uri())
|| (self.duration() != old.duration())
|| (self.epoch() != old.epoch()) || (self.guid() != old.guid())
{
self.update(old.rowid())
if self != &other {
self.update(other.rowid())
} else {
Ok(())
}
@ -97,6 +93,14 @@ impl Index for NewEpisode {
}
}
impl PartialEq<EpisodeMinimal> for NewEpisode {
fn eq(&self, other: &EpisodeMinimal) -> bool {
(self.title() != other.title()) || (self.uri() != other.uri())
|| (self.duration() != other.duration()) || (self.epoch() != other.epoch())
|| (self.guid() != other.guid())
}
}
impl NewEpisode {
/// Parses an `rss::Item` into a `NewEpisode` Struct.
#[allow(dead_code)]
@ -161,6 +165,14 @@ pub(crate) struct NewEpisodeMinimal {
podcast_id: i32,
}
impl PartialEq<EpisodeMinimal> for NewEpisodeMinimal {
fn eq(&self, other: &EpisodeMinimal) -> bool {
(self.title() != other.title()) || (self.uri() != other.uri())
|| (self.duration() != other.duration()) || (self.epoch() != other.epoch())
|| (self.guid() != other.guid())
}
}
impl NewEpisodeMinimal {
pub(crate) fn new(item: &rss::Item, parent_id: i32) -> Result<Self> {
if item.title().is_none() {
@ -224,7 +236,6 @@ impl NewEpisodeMinimal {
}
}
#[allow(dead_code)]
// Ignore the following getters. They are used in unit tests mainly.
impl NewEpisodeMinimal {
pub(crate) fn title(&self) -> &str {
@ -239,6 +250,10 @@ impl NewEpisodeMinimal {
self.guid.as_ref().map(|s| s.as_str())
}
pub(crate) fn duration(&self) -> Option<i32> {
self.duration
}
pub(crate) fn epoch(&self) -> i32 {
self.epoch
}

View File

@ -62,14 +62,10 @@ impl Index for NewPodcast {
let exists = dbqueries::podcast_exists(self.source_id)?;
if exists {
let old = dbqueries::get_podcast_from_source_id(self.source_id)?;
let other = dbqueries::get_podcast_from_source_id(self.source_id)?;
// This is messy
if (self.link() != old.link()) || (self.title() != old.title())
|| (self.image_uri() != old.image_uri())
|| (self.description() != old.description())
{
self.update(old.id())
if self != &other {
self.update(other.id())
} else {
Ok(())
}
@ -79,6 +75,14 @@ impl Index for NewPodcast {
}
}
impl PartialEq<Podcast> for NewPodcast {
fn eq(&self, other: &Podcast) -> bool {
(self.link() != other.link()) || (self.title() != other.title())
|| (self.image_uri() != other.image_uri())
|| (self.description() != other.description())
}
}
impl NewPodcast {
/// Parses a `rss::Channel` into a `NewPodcast` Struct.
pub(crate) fn new(chan: &rss::Channel, source_id: i32) -> NewPodcast {

View File

@ -56,7 +56,7 @@ fn determine_ep_state(ep: NewEpisodeMinimal, item: &rss::Item) -> Result<IndexSt
let old = dbqueries::get_episode_minimal_from_pk(ep.title(), ep.podcast_id())?;
let rowid = old.rowid();
if ep != old.into() {
if ep != old {
Ok(IndexState::Update((ep.into_new_episode(item), rowid)))
} else {
Ok(IndexState::NotChanged)