Made Disel NewModels private.

This commit is contained in:
Jordan Petridis 2017-11-22 05:50:25 +02:00
parent ee7c5ca26e
commit 9bc6df2cba
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
12 changed files with 35 additions and 51 deletions

View File

@ -12,7 +12,7 @@ use rayon::prelude::*;
use test::Bencher; use test::Bencher;
use hammond_data::models::NewSource; use hammond_data::Source;
use hammond_data::feed::{index, Feed}; use hammond_data::feed::{index, Feed};
use std::io::BufReader; use std::io::BufReader;
@ -36,7 +36,7 @@ fn index_urls() {
URLS.par_iter() URLS.par_iter()
.map(|&(buff, url)| { .map(|&(buff, url)| {
// Create and insert a Source into db // Create and insert a Source into db
let s = NewSource::new_with_uri(url).into_source().unwrap(); let s = Source::from_url(url).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(buff)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(buff)).unwrap();
Feed::from_channel_source(chan, s) Feed::from_channel_source(chan, s)

View File

@ -1,4 +1,3 @@
use diesel::prelude::*; use diesel::prelude::*;
use diesel; use diesel;
use models::{Episode, NewEpisode, NewPodcast, NewSource, Podcast, Source}; use models::{Episode, NewEpisode, NewPodcast, NewSource, Podcast, Source};

View File

@ -33,7 +33,7 @@ impl Feed {
Ok(()) Ok(())
} }
fn index_channel(&self) -> Result<Podcast> { pub fn index_channel(&self) -> Result<Podcast> {
let pd = parser::new_podcast(&self.channel, *self.source.id()); let pd = parser::new_podcast(&self.channel, *self.source.id());
// Convert NewPodcast to Podcast // Convert NewPodcast to Podcast
pd.into_podcast() pd.into_podcast()

View File

@ -27,9 +27,9 @@ extern crate xdg;
pub mod dbqueries; pub mod dbqueries;
pub mod utils; pub mod utils;
pub mod models;
pub mod feed; pub mod feed;
pub mod errors; pub mod errors;
mod models;
mod parser; mod parser;
mod schema; mod schema;
@ -41,6 +41,8 @@ use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
// use std::time::Duration; // use std::time::Duration;
pub use models::{Episode, Podcast, Source};
// type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>; // type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
type Database = Arc<Mutex<SqliteConnection>>; type Database = Arc<Mutex<SqliteConnection>>;

View File

@ -40,7 +40,7 @@ impl<'a> NewSource<'a> {
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "episode"] #[table_name = "episode"]
#[derive(Debug, Clone)] #[derive(Debug, Clone, Default)]
pub struct NewEpisode<'a> { pub struct NewEpisode<'a> {
pub title: Option<&'a str>, pub title: Option<&'a str>,
pub uri: Option<String>, pub uri: Option<String>,

View File

@ -1,4 +1,3 @@
use reqwest; use reqwest;
use diesel::SaveChangesDsl; use diesel::SaveChangesDsl;
use diesel::result::QueryResult; use diesel::result::QueryResult;
@ -9,7 +8,7 @@ use schema::{episode, podcast, source};
use feed::Feed; use feed::Feed;
use errors::*; use errors::*;
use models::insertables::NewPodcast; use models::insertables::NewSource;
use connection; use connection;
use std::io::Read; use std::io::Read;
@ -153,23 +152,6 @@ pub struct Podcast {
source_id: i32, source_id: i32,
} }
/// This is meant only to be used to make unit tests easier.
impl From<NewPodcast> for Podcast {
fn from(pd: NewPodcast) -> Podcast {
Podcast {
id: 0,
title: pd.title,
link: pd.link,
description: pd.description,
image_uri: pd.image_uri,
source_id: pd.source_id,
always_dl: false,
archive: false,
favorite: false,
}
}
}
impl Podcast { impl Podcast {
pub fn source_id(&self) -> i32 { pub fn source_id(&self) -> i32 {
self.source_id self.source_id
@ -336,4 +318,8 @@ impl<'a> Source {
Ok(Feed::from_channel_source(chan, self)) Ok(Feed::from_channel_source(chan, self))
} }
pub fn from_url(uri: &str) -> QueryResult<Source> {
NewSource::new_with_uri(uri).into_source()
}
} }

View File

@ -10,7 +10,7 @@ use std::io::{BufWriter, Read, Write};
use std::path::Path; use std::path::Path;
use errors::*; use errors::*;
use hammond_data::models::{Episode, Podcast}; use hammond_data::{Episode, Podcast};
use hammond_data::{DL_DIR, HAMMOND_CACHE}; use hammond_data::{DL_DIR, HAMMOND_CACHE};
// TODO: Replace path that are of type &str with std::path. // TODO: Replace path that are of type &str with std::path.
@ -193,7 +193,7 @@ pub fn cache_image(pd: &Podcast) -> Option<String> {
mod tests { mod tests {
use super::*; use super::*;
use hammond_data::{DL_DIR, HAMMOND_CACHE}; use hammond_data::{DL_DIR, HAMMOND_CACHE};
use hammond_data::models::NewPodcast; use hammond_data::Source;
use std::fs; use std::fs;
@ -206,14 +206,13 @@ mod tests {
#[test] #[test]
fn test_cache_image() { fn test_cache_image() {
let pd = NewPodcast { let pd = Source::from_url("http://www.newrustacean.com/feed.xml")
title: "New Rustacean".to_string(), .unwrap()
description: "".to_string(), .refresh()
link: "".to_string(), .unwrap()
image_uri: Some("http://newrustacean.com/podcast.png".to_string()), .index_channel()
source_id: 0, .unwrap();
};
let pd = Podcast::from(pd);
let img_path = cache_image(&pd); let img_path = cache_image(&pd);
let foo_ = format!( let foo_ = format!(
"{}{}/cover.png", "{}{}/cover.png",

View File

@ -1,7 +1,7 @@
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use hammond_data::models::NewSource; use hammond_data::Source;
use hammond_data::utils::url_cleaner; use hammond_data::utils::url_cleaner;
use podcasts_view::update_podcasts_view; use podcasts_view::update_podcasts_view;
@ -54,10 +54,10 @@ pub fn get_headerbar(stack: &gtk::Stack) -> gtk::HeaderBar {
} }
fn on_add_bttn_clicked(stack: &gtk::Stack, url: &str) { fn on_add_bttn_clicked(stack: &gtk::Stack, url: &str) {
let source = NewSource::new_with_uri(url).into_source(); let source = Source::from_url(url);
info!("{:?} feed added", url);
if let Ok(s) = source { if let Ok(s) = source {
info!("{:?} feed added", url);
// update the db // update the db
utils::refresh_feed(stack, Some(vec![s]), None); utils::refresh_feed(stack, Some(vec![s]), None);
} else { } else {

View File

@ -2,7 +2,7 @@ use glib;
use gtk; use gtk;
use hammond_data::feed; use hammond_data::feed;
use hammond_data::models::Source; use hammond_data::Source;
use std::{thread, time}; use std::{thread, time};
use std::cell::RefCell; use std::cell::RefCell;

View File

@ -4,7 +4,7 @@ use gdk_pixbuf::Pixbuf;
use diesel::associations::Identifiable; use diesel::associations::Identifiable;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::models::Podcast; use hammond_data::Podcast;
use widgets::podcast::*; use widgets::podcast::*;

View File

@ -1,6 +1,6 @@
use open; use open;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::models::{Episode, Podcast}; use hammond_data::{Episode, Podcast};
use hammond_downloader::downloader; use hammond_downloader::downloader;
use hammond_data::utils::*; use hammond_data::utils::*;
use hammond_data::errors::*; use hammond_data::errors::*;

View File

@ -5,7 +5,7 @@ use gdk_pixbuf::Pixbuf;
use std::fs; use std::fs;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::models::Podcast; use hammond_data::Podcast;
use hammond_downloader::downloader; use hammond_downloader::downloader;
use widgets::episode::episodes_listbox; use widgets::episode::episodes_listbox;
@ -118,19 +118,17 @@ pub fn update_podcast_widget(stack: &gtk::Stack, pd: &Podcast) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hammond_data::models::NewPodcast; use hammond_data::Source;
use super::*; use super::*;
#[test] #[test]
fn test_get_pixbuf_from_path() { fn test_get_pixbuf_from_path() {
let pd = NewPodcast { let pd = Source::from_url("http://www.newrustacean.com/feed.xml")
title: "New Rustacean".to_string(), .unwrap()
description: "".to_string(), .refresh()
link: "".to_string(), .unwrap()
image_uri: Some("http://newrustacean.com/podcast.png".to_string()), .index_channel()
source_id: 0, .unwrap();
};
let pd = Podcast::from(pd);
let pxbuf = get_pixbuf_from_path(&pd); let pxbuf = get_pixbuf_from_path(&pd);
assert!(pxbuf.is_some()); assert!(pxbuf.is_some());