h-data: Refactor the Diesel helper traits to use Associated Types.
This commit is contained in:
parent
7a17b3df4b
commit
2d291a08fc
@ -31,10 +31,12 @@ pub struct Episode {
|
||||
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
|
||||
/// Database.
|
||||
fn save(&self) -> Result<Episode, DataError> {
|
||||
fn save(&self) -> Result<Episode, Self::Error> {
|
||||
let db = connection();
|
||||
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
|
||||
/// Database.
|
||||
fn save(&self) -> Result<usize, DataError> {
|
||||
fn save(&self) -> Result<usize, Self::Error> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let db = connection();
|
||||
@ -363,10 +367,12 @@ pub struct EpisodeCleanerQuery {
|
||||
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
|
||||
/// Database.
|
||||
fn save(&self) -> Result<usize, DataError> {
|
||||
fn save(&self) -> Result<usize, Self::Error> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let db = connection();
|
||||
|
||||
@ -30,22 +30,30 @@ pub enum IndexState<T> {
|
||||
NotChanged,
|
||||
}
|
||||
|
||||
pub trait Insert<T, E> {
|
||||
fn insert(&self) -> Result<T, E>;
|
||||
pub trait Insert<T> {
|
||||
type Error;
|
||||
|
||||
fn insert(&self) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait Update<T, E> {
|
||||
fn update(&self, i32) -> Result<T, E>;
|
||||
pub trait Update<T> {
|
||||
type Error;
|
||||
|
||||
fn update(&self, i32) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
// This might need to change in the future
|
||||
pub trait Index<T, E>: Insert<T, E> + Update<T, E> {
|
||||
fn index(&self) -> Result<T, E>;
|
||||
pub trait Index<T>: Insert<T> + Update<T> {
|
||||
type Error;
|
||||
|
||||
fn index(&self) -> Result<T, <Self as Index<T>>::Error>;
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// the Database.
|
||||
fn save(&self) -> Result<T, E>;
|
||||
fn save(&self) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
@ -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> {
|
||||
use schema::episode::dsl::*;
|
||||
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> {
|
||||
use schema::episode::dsl::*;
|
||||
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
|
||||
// changed.
|
||||
fn index(&self) -> Result<(), DataError> {
|
||||
|
||||
@ -26,8 +26,10 @@ pub(crate) struct NewPodcast {
|
||||
source_id: i32,
|
||||
}
|
||||
|
||||
impl Insert<(), DataError> for NewPodcast {
|
||||
fn insert(&self) -> Result<(), DataError> {
|
||||
impl Insert<()> for NewPodcast {
|
||||
type Error = DataError;
|
||||
|
||||
fn insert(&self) -> Result<(), Self::Error> {
|
||||
use schema::podcast::dsl::*;
|
||||
let db = connection();
|
||||
let con = db.get()?;
|
||||
@ -40,8 +42,10 @@ impl Insert<(), DataError> for NewPodcast {
|
||||
}
|
||||
}
|
||||
|
||||
impl Update<(), DataError> for NewPodcast {
|
||||
fn update(&self, podcast_id: i32) -> Result<(), DataError> {
|
||||
impl Update<()> for NewPodcast {
|
||||
type Error = DataError;
|
||||
|
||||
fn update(&self, podcast_id: i32) -> Result<(), Self::Error> {
|
||||
use schema::podcast::dsl::*;
|
||||
let db = connection();
|
||||
let con = db.get()?;
|
||||
@ -57,7 +61,9 @@ impl Update<(), DataError> for NewPodcast {
|
||||
|
||||
// TODO: Maybe return an Enum<Action(Resut)> Instead.
|
||||
// It would make unti testing better too.
|
||||
impl Index<(), DataError> for NewPodcast {
|
||||
impl Index<()> for NewPodcast {
|
||||
type Error = DataError;
|
||||
|
||||
fn index(&self) -> Result<(), DataError> {
|
||||
let exists = dbqueries::podcast_exists(self.source_id)?;
|
||||
|
||||
|
||||
@ -25,10 +25,12 @@ pub struct Podcast {
|
||||
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
|
||||
/// Database.
|
||||
fn save(&self) -> Result<Podcast, DataError> {
|
||||
fn save(&self) -> Result<Podcast, Self::Error> {
|
||||
let db = connection();
|
||||
let tempdb = db.get()?;
|
||||
|
||||
|
||||
@ -34,10 +34,12 @@ pub struct Source {
|
||||
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
|
||||
/// Database.
|
||||
fn save(&self) -> Result<Source, DataError> {
|
||||
fn save(&self) -> Result<Source, Self::Error> {
|
||||
let db = connection();
|
||||
let con = db.get()?;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user