From 568bc460de773dd0464a9011710f019c6b18c643 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 13 Jan 2021 13:50:08 -0300 Subject: [PATCH] Rust-analyzer suggested `match` and it was nice --- src/main.rs | 40 ++++++++++++++++++++-------------------- src/storage/joplin.rs | 34 +++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index e3b292b..05a5367 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,10 +34,9 @@ fn main() { let config = dbg!(config::Config::get()); let client = dbg!(get_mastodon_connection()); let top = dbg!(config.last_favorite.to_string()); - let storage: Box = if let Some(joplin) = &config.joplin { - Box::new(Joplin::new_from_config(&joplin)) - } else { - Box::new(Filesystem::new()) + let storage: Box = match &config.joplin { + Some(joplin) => Box::new(Joplin::new_from_config(&joplin)), + None => Box::new(Filesystem::new()), }; let most_recent_favourite = client @@ -51,7 +50,7 @@ fn main() { record }) .fold(None, { - |first, current| { + |first, current| -> Option { if first.is_some() { first } else { @@ -66,21 +65,22 @@ fn main() { /// Get a connection with Mastodon; if there is no set up with any account yet, /// requests one. fn get_mastodon_connection() -> Mastodon { - if let Ok(data) = elefren_toml::from_file("mastodon.toml") { - Mastodon::from(data) - } else { - println!("Your server URL: "); - let mut server = String::new(); - io::stdin() - .read_line(&mut server) - .expect("You need to enter yoru server URL"); + match elefren_toml::from_file("mastodon.toml") { + Ok(data) => Mastodon::from(data), + Err(_) => { + println!("Your server URL: "); + let mut server = String::new(); + io::stdin() + .read_line(&mut server) + .expect("You need to enter yoru server URL"); - let registration = Registration::new(server.trim()) - .client_name("downfav") - .build() - .unwrap(); - let mastodon = cli::authenticate(registration).unwrap(); - elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap(); - mastodon + let registration = Registration::new(server.trim()) + .client_name("downfav") + .build() + .unwrap(); + let mastodon = cli::authenticate(registration).unwrap(); + elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap(); + mastodon + } } } diff --git a/src/storage/joplin.rs b/src/storage/joplin.rs index 5ca2b64..f1efebd 100644 --- a/src/storage/joplin.rs +++ b/src/storage/joplin.rs @@ -73,31 +73,35 @@ impl Storage for Joplin { } impl Joplin { - pub fn new_from_config(config: &JoplinConfig) -> Joplin { - if let Some(folder_id) = Joplin::find_folder(config) { - Joplin { + pub(crate) fn new_from_config(config: &JoplinConfig) -> Joplin { + match Joplin::find_folder(config) { + Some(folder_id) => Joplin { port: config.port, token: config.token.to_string(), - folder_id: folder_id, + folder_id, client: reqwest::Client::new(), + }, + None => { + println!("The notebook {} does not exist", &config.folder); + panic!("The specified notebook does not exist"); } - } else { - println!("The notebook {} does not exist", &config.folder); - panic!("The specified notebook does not exist"); } } fn find_folder(config: &JoplinConfig) -> Option { - if let Ok(folders) = dbg!(Joplin::get_folder_list(config)) { - for folder in folders { - if folder.title == *config.folder { - return Some(folder.id); + match dbg!(Joplin::get_folder_list(config)) { + Ok(folders) => { + for folder in folders { + if folder.title == *config.folder { + return Some(folder.id); + } } + None + } + Err(_) => { + println!("Failed to retrieve the notebook list"); + panic!("Failed to retrieve Joplin notebook list"); } - None - } else { - println!("Failed to retrieve the notebook list"); - panic!("Failed to retrieve Joplin notebook list"); } }