h-data: Refactor the Diesel helper traits to use Associated Types.

This commit is contained in:
Jordan Petridis 2018-04-17 01:33:50 +03:00
parent 7a17b3df4b
commit 2d291a08fc
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
6 changed files with 56 additions and 26 deletions

View File

@ -31,10 +31,12 @@ pub struct Episode {
podcast_id: i32, podcast_id: i32,
} }
impl Save<Episode, DataError> for Episode { impl Save<Episode> for Episode {
type Error = DataError;
/// Helper method to easily save/"sync" current state of self to the /// Helper method to easily save/"sync" current state of self to the
/// Database. /// Database.
fn save(&self) -> Result<Episode, DataError> { fn save(&self) -> Result<Episode, Self::Error> {
let db = connection(); let db = connection();
let tempdb = db.get()?; let tempdb = db.get()?;
@ -224,10 +226,12 @@ impl From<Episode> for EpisodeWidgetQuery {
} }
} }
impl Save<usize, DataError> for EpisodeWidgetQuery { impl Save<usize> for EpisodeWidgetQuery {
type Error = DataError;
/// Helper method to easily save/"sync" current state of self to the /// Helper method to easily save/"sync" current state of self to the
/// Database. /// Database.
fn save(&self) -> Result<usize, DataError> { fn save(&self) -> Result<usize, Self::Error> {
use schema::episode::dsl::*; use schema::episode::dsl::*;
let db = connection(); let db = connection();
@ -363,10 +367,12 @@ pub struct EpisodeCleanerQuery {
played: Option<i32>, played: Option<i32>,
} }
impl Save<usize, DataError> for EpisodeCleanerQuery { impl Save<usize> for EpisodeCleanerQuery {
type Error = DataError;
/// Helper method to easily save/"sync" current state of self to the /// Helper method to easily save/"sync" current state of self to the
/// Database. /// Database.
fn save(&self) -> Result<usize, DataError> { fn save(&self) -> Result<usize, Self::Error> {
use schema::episode::dsl::*; use schema::episode::dsl::*;
let db = connection(); let db = connection();

View File

@ -30,22 +30,30 @@ pub enum IndexState<T> {
NotChanged, NotChanged,
} }
pub trait Insert<T, E> { pub trait Insert<T> {
fn insert(&self) -> Result<T, E>; type Error;
fn insert(&self) -> Result<T, Self::Error>;
} }
pub trait Update<T, E> { pub trait Update<T> {
fn update(&self, i32) -> Result<T, E>; type Error;
fn update(&self, i32) -> Result<T, Self::Error>;
} }
// This might need to change in the future // This might need to change in the future
pub trait Index<T, E>: Insert<T, E> + Update<T, E> { pub trait Index<T>: Insert<T> + Update<T> {
fn index(&self) -> Result<T, E>; type Error;
fn index(&self) -> Result<T, <Self as Index<T>>::Error>;
} }
/// FIXME: DOCS /// FIXME: DOCS
pub trait Save<T, E> { pub trait Save<T> {
/// The Error type to be returned.
type Error;
/// Helper method to easily save/"sync" current state of a diesel model to /// Helper method to easily save/"sync" current state of a diesel model to
/// the Database. /// the Database.
fn save(&self) -> Result<T, E>; fn save(&self) -> Result<T, Self::Error>;
} }

View File

@ -43,7 +43,9 @@ impl From<NewEpisodeMinimal> for NewEpisode {
} }
} }
impl Insert<(), DataError> for NewEpisode { impl Insert<()> for NewEpisode {
type Error = DataError;
fn insert(&self) -> Result<(), DataError> { fn insert(&self) -> Result<(), DataError> {
use schema::episode::dsl::*; use schema::episode::dsl::*;
let db = connection(); let db = connection();
@ -58,7 +60,9 @@ impl Insert<(), DataError> for NewEpisode {
} }
} }
impl Update<(), DataError> for NewEpisode { impl Update<()> for NewEpisode {
type Error = DataError;
fn update(&self, episode_id: i32) -> Result<(), DataError> { fn update(&self, episode_id: i32) -> Result<(), DataError> {
use schema::episode::dsl::*; use schema::episode::dsl::*;
let db = connection(); let db = connection();
@ -73,7 +77,9 @@ impl Update<(), DataError> for NewEpisode {
} }
} }
impl Index<(), DataError> for NewEpisode { impl Index<()> for NewEpisode {
type Error = DataError;
// Does not update the episode description if it's the only thing that has // Does not update the episode description if it's the only thing that has
// changed. // changed.
fn index(&self) -> Result<(), DataError> { fn index(&self) -> Result<(), DataError> {

View File

@ -26,8 +26,10 @@ pub(crate) struct NewPodcast {
source_id: i32, source_id: i32,
} }
impl Insert<(), DataError> for NewPodcast { impl Insert<()> for NewPodcast {
fn insert(&self) -> Result<(), DataError> { type Error = DataError;
fn insert(&self) -> Result<(), Self::Error> {
use schema::podcast::dsl::*; use schema::podcast::dsl::*;
let db = connection(); let db = connection();
let con = db.get()?; let con = db.get()?;
@ -40,8 +42,10 @@ impl Insert<(), DataError> for NewPodcast {
} }
} }
impl Update<(), DataError> for NewPodcast { impl Update<()> for NewPodcast {
fn update(&self, podcast_id: i32) -> Result<(), DataError> { type Error = DataError;
fn update(&self, podcast_id: i32) -> Result<(), Self::Error> {
use schema::podcast::dsl::*; use schema::podcast::dsl::*;
let db = connection(); let db = connection();
let con = db.get()?; let con = db.get()?;
@ -57,7 +61,9 @@ impl Update<(), DataError> for NewPodcast {
// TODO: Maybe return an Enum<Action(Resut)> Instead. // TODO: Maybe return an Enum<Action(Resut)> Instead.
// It would make unti testing better too. // It would make unti testing better too.
impl Index<(), DataError> for NewPodcast { impl Index<()> for NewPodcast {
type Error = DataError;
fn index(&self) -> Result<(), DataError> { fn index(&self) -> Result<(), DataError> {
let exists = dbqueries::podcast_exists(self.source_id)?; let exists = dbqueries::podcast_exists(self.source_id)?;

View File

@ -25,10 +25,12 @@ pub struct Podcast {
source_id: i32, source_id: i32,
} }
impl Save<Podcast, DataError> for Podcast { impl Save<Podcast> for Podcast {
type Error = DataError;
/// Helper method to easily save/"sync" current state of self to the /// Helper method to easily save/"sync" current state of self to the
/// Database. /// Database.
fn save(&self) -> Result<Podcast, DataError> { fn save(&self) -> Result<Podcast, Self::Error> {
let db = connection(); let db = connection();
let tempdb = db.get()?; let tempdb = db.get()?;

View File

@ -34,10 +34,12 @@ pub struct Source {
http_etag: Option<String>, http_etag: Option<String>,
} }
impl Save<Source, DataError> for Source { impl Save<Source> for Source {
type Error = DataError;
/// Helper method to easily save/"sync" current state of self to the /// Helper method to easily save/"sync" current state of self to the
/// Database. /// Database.
fn save(&self) -> Result<Source, DataError> { fn save(&self) -> Result<Source, Self::Error> {
let db = connection(); let db = connection();
let con = db.get()?; let con = db.get()?;