diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-07 20:05:51 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-07 20:07:14 -0500 |
commit | f2c66c8e47fddd468b0b6894e7c1902b0baecc16 (patch) | |
tree | 2c80d5c737c6428d9b604d8c2862575c238d5748 /src | |
parent | b18c6b8135bfdc884b0a0cf03196d3de0a343490 (diff) | |
download | lastfm-query-f2c66c8e47fddd468b0b6894e7c1902b0baecc16.tar.gz lastfm-query-f2c66c8e47fddd468b0b6894e7c1902b0baecc16.zip |
no need to define my own type here
didn't realize this already existed
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 9 | ||||
-rw-r--r-- | src/error.rs | 3 | ||||
-rw-r--r-- | src/exporter.rs | 9 | ||||
-rw-r--r-- | src/lastfm/mod.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 7 |
5 files changed, 16 insertions, 24 deletions
@@ -1,4 +1,3 @@ -use error::Result; use lastfm; use failure::Fail; @@ -17,7 +16,7 @@ pub struct DB { } impl DB { - pub fn new<P: AsRef<std::path::Path>>(path: &P) -> Result<DB> { + pub fn new<P: AsRef<std::path::Path>>(path: &P) -> failure::Fallible<DB> { let conn = if path.as_ref().exists() { rusqlite::Connection::open(path) .map_err(|e| { @@ -37,7 +36,7 @@ impl DB { fn create<P: AsRef<std::path::Path>>( path: &P - ) -> Result<rusqlite::Connection> { + ) -> failure::Fallible<rusqlite::Connection> { println!( "Initializing database at {}", path.as_ref().to_string_lossy(), @@ -62,7 +61,7 @@ impl DB { } } - pub fn most_recent_timestamp(&self) -> Result<Option<i64>> { + pub fn most_recent_timestamp(&self) -> failure::Fallible<Option<i64>> { Ok(self.conn.query_row( "SELECT timestamp FROM tracks ORDER BY timestamp DESC LIMIT 1", rusqlite::NO_PARAMS, @@ -79,7 +78,7 @@ impl DB { &self, tracks: impl Iterator<Item=lastfm::Track>, mut f: F - ) -> Result<()> { + ) -> failure::Fallible<()> { let mut sth = self.conn.prepare("INSERT INTO tracks VALUES (?, ?, ?, ?)")?; for track in tracks { sth.execute( diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index b508e3e..0000000 --- a/src/error.rs +++ /dev/null @@ -1,3 +0,0 @@ -use failure::Error; - -pub type Result<T> = std::result::Result<T, Error>; diff --git a/src/exporter.rs b/src/exporter.rs index 89ad32c..29347f9 100644 --- a/src/exporter.rs +++ b/src/exporter.rs @@ -1,8 +1,6 @@ use db; use lastfm; -use error::Result; - pub struct Exporter<'d, 'l> { db: &'d db::DB, lastfm: &'l lastfm::LastFMClient, @@ -19,12 +17,15 @@ impl<'d, 'l> Exporter<'d, 'l> { } } - pub fn tracks_to_sync(&self) -> Result<u64> { + pub fn tracks_to_sync(&self) -> failure::Fallible<u64> { let ts = self.db.most_recent_timestamp()?; Ok(self.lastfm.track_count(ts.map(|x| x + 1))?) } - pub fn sync<F: FnMut(lastfm::Track)>(&self, track_cb: F) -> Result<()> { + pub fn sync<F: FnMut(lastfm::Track)>( + &self, + track_cb: F, + ) -> failure::Fallible<()> { let ts = self.db.most_recent_timestamp()?; self.db.insert_tracks( self.lastfm.tracks(ts.map(|x| x + 1)), diff --git a/src/lastfm/mod.rs b/src/lastfm/mod.rs index b510eb3..53e501e 100644 --- a/src/lastfm/mod.rs +++ b/src/lastfm/mod.rs @@ -1,8 +1,6 @@ use failure; use reqwest; -use error::Result; - mod api_types; const API_ROOT: &'static str = "https://ws.audioscrobbler.com/2.0/"; @@ -37,7 +35,7 @@ impl<'a> Tracks<'a> { } } - fn get_next_page(&mut self) -> Result<()> { + fn get_next_page(&mut self) -> failure::Fallible<()> { if !self.page.is_some() { self.page = Some(self.client.get_total_pages(self.from)?); } @@ -78,7 +76,7 @@ impl<'a> Tracks<'a> { timestamp: t.date.as_ref().unwrap().uts.parse()?, }) }) - .collect::<Result<Vec<Track>>>()?; + .collect::<failure::Fallible<Vec<Track>>>()?; self.page = Some(page - 1); Ok(()) } @@ -111,7 +109,7 @@ impl LastFMClient { } } - pub fn track_count(&self, from: Option<i64>) -> Result<u64> { + pub fn track_count(&self, from: Option<i64>) -> failure::Fallible<u64> { let data = self.recent_tracks(from)?; Ok(data.recenttracks.attr.total.parse()?) } @@ -120,7 +118,7 @@ impl LastFMClient { Tracks::new(&self, from) } - fn get_total_pages(&self, from: Option<i64>) -> Result<u64> { + fn get_total_pages(&self, from: Option<i64>) -> failure::Fallible<u64> { let data = self.recent_tracks(from)?; Ok(data.recenttracks.attr.totalPages.parse()?) } @@ -128,7 +126,7 @@ impl LastFMClient { fn recent_tracks( &self, from: Option<i64>, - ) -> Result<api_types::recent_tracks> { + ) -> failure::Fallible<api_types::recent_tracks> { let req = self.client .get(API_ROOT) .query(&[ diff --git a/src/main.rs b/src/main.rs index 4d66a92..ed5a189 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,15 +12,12 @@ extern crate serde_json; extern crate serde_derive; mod cli; -mod error; mod exporter; mod lastfm; mod paths; mod db; -use error::Result; - -fn run(opts: cli::Options) -> Result<()> { +fn run(opts: cli::Options) -> failure::Fallible<()> { let db = db::DB::new(&paths::dbpath())?; let lastfm = lastfm::LastFMClient::new(&opts.api_key, &opts.username); let exporter = exporter::Exporter::new(&db, &lastfm); @@ -42,7 +39,7 @@ fn run(opts: cli::Options) -> Result<()> { Ok(()) } -fn program_name() -> Result<String> { +fn program_name() -> failure::Fallible<String> { let program = std::env::args() .next() .ok_or_else(|| format_err!("no program name found"))?; |