From f2c66c8e47fddd468b0b6894e7c1902b0baecc16 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 7 Nov 2018 20:05:51 -0500 Subject: no need to define my own type here didn't realize this already existed --- src/db.rs | 9 ++++----- src/error.rs | 3 --- src/exporter.rs | 9 +++++---- src/lastfm/mod.rs | 12 +++++------- src/main.rs | 7 ++----- 5 files changed, 16 insertions(+), 24 deletions(-) delete mode 100644 src/error.rs diff --git a/src/db.rs b/src/db.rs index 0d81d7e..67dec84 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,4 +1,3 @@ -use error::Result; use lastfm; use failure::Fail; @@ -17,7 +16,7 @@ pub struct DB { } impl DB { - pub fn new>(path: &P) -> Result { + pub fn new>(path: &P) -> failure::Fallible { let conn = if path.as_ref().exists() { rusqlite::Connection::open(path) .map_err(|e| { @@ -37,7 +36,7 @@ impl DB { fn create>( path: &P - ) -> Result { + ) -> failure::Fallible { println!( "Initializing database at {}", path.as_ref().to_string_lossy(), @@ -62,7 +61,7 @@ impl DB { } } - pub fn most_recent_timestamp(&self) -> Result> { + pub fn most_recent_timestamp(&self) -> failure::Fallible> { 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, 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 = std::result::Result; 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 { + pub fn tracks_to_sync(&self) -> failure::Fallible { let ts = self.db.most_recent_timestamp()?; Ok(self.lastfm.track_count(ts.map(|x| x + 1))?) } - pub fn sync(&self, track_cb: F) -> Result<()> { + pub fn sync( + &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::>>()?; + .collect::>>()?; self.page = Some(page - 1); Ok(()) } @@ -111,7 +109,7 @@ impl LastFMClient { } } - pub fn track_count(&self, from: Option) -> Result { + pub fn track_count(&self, from: Option) -> failure::Fallible { 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) -> Result { + fn get_total_pages(&self, from: Option) -> failure::Fallible { let data = self.recent_tracks(from)?; Ok(data.recenttracks.attr.totalPages.parse()?) } @@ -128,7 +126,7 @@ impl LastFMClient { fn recent_tracks( &self, from: Option, - ) -> Result { + ) -> failure::Fallible { 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 { +fn program_name() -> failure::Fallible { let program = std::env::args() .next() .ok_or_else(|| format_err!("no program name found"))?; -- cgit v1.2.3