Browse Source

Rust-analyzer suggested `match` and it was nice

master
Julio Biason 4 years ago
parent
commit
568bc460de
  1. 16
      src/main.rs
  2. 20
      src/storage/joplin.rs

16
src/main.rs

@ -34,10 +34,9 @@ fn main() {
let config = dbg!(config::Config::get()); let config = dbg!(config::Config::get());
let client = dbg!(get_mastodon_connection()); let client = dbg!(get_mastodon_connection());
let top = dbg!(config.last_favorite.to_string()); let top = dbg!(config.last_favorite.to_string());
let storage: Box<dyn Storage> = if let Some(joplin) = &config.joplin { let storage: Box<dyn Storage> = match &config.joplin {
Box::new(Joplin::new_from_config(&joplin)) Some(joplin) => Box::new(Joplin::new_from_config(&joplin)),
} else { None => Box::new(Filesystem::new()),
Box::new(Filesystem::new())
}; };
let most_recent_favourite = client let most_recent_favourite = client
@ -51,7 +50,7 @@ fn main() {
record record
}) })
.fold(None, { .fold(None, {
|first, current| { |first, current| -> Option<String> {
if first.is_some() { if first.is_some() {
first first
} else { } else {
@ -66,9 +65,9 @@ fn main() {
/// Get a connection with Mastodon; if there is no set up with any account yet, /// Get a connection with Mastodon; if there is no set up with any account yet,
/// requests one. /// requests one.
fn get_mastodon_connection() -> Mastodon { fn get_mastodon_connection() -> Mastodon {
if let Ok(data) = elefren_toml::from_file("mastodon.toml") { match elefren_toml::from_file("mastodon.toml") {
Mastodon::from(data) Ok(data) => Mastodon::from(data),
} else { Err(_) => {
println!("Your server URL: "); println!("Your server URL: ");
let mut server = String::new(); let mut server = String::new();
io::stdin() io::stdin()
@ -83,4 +82,5 @@ fn get_mastodon_connection() -> Mastodon {
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap(); elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon mastodon
} }
}
} }

20
src/storage/joplin.rs

@ -73,33 +73,37 @@ impl Storage for Joplin {
} }
impl Joplin { impl Joplin {
pub fn new_from_config(config: &JoplinConfig) -> Joplin { pub(crate) fn new_from_config(config: &JoplinConfig) -> Joplin {
if let Some(folder_id) = Joplin::find_folder(config) { match Joplin::find_folder(config) {
Joplin { Some(folder_id) => Joplin {
port: config.port, port: config.port,
token: config.token.to_string(), token: config.token.to_string(),
folder_id: folder_id, folder_id,
client: reqwest::Client::new(), client: reqwest::Client::new(),
} },
} else { None => {
println!("The notebook {} does not exist", &config.folder); println!("The notebook {} does not exist", &config.folder);
panic!("The specified notebook does not exist"); panic!("The specified notebook does not exist");
} }
} }
}
fn find_folder(config: &JoplinConfig) -> Option<String> { fn find_folder(config: &JoplinConfig) -> Option<String> {
if let Ok(folders) = dbg!(Joplin::get_folder_list(config)) { match dbg!(Joplin::get_folder_list(config)) {
Ok(folders) => {
for folder in folders { for folder in folders {
if folder.title == *config.folder { if folder.title == *config.folder {
return Some(folder.id); return Some(folder.id);
} }
} }
None None
} else { }
Err(_) => {
println!("Failed to retrieve the notebook list"); println!("Failed to retrieve the notebook list");
panic!("Failed to retrieve Joplin notebook list"); panic!("Failed to retrieve Joplin notebook list");
} }
} }
}
fn get_folder_list(config: &JoplinConfig) -> Result<Vec<Folder>, Error> { fn get_folder_list(config: &JoplinConfig) -> Result<Vec<Folder>, Error> {
let mut page = 1; let mut page = 1;

Loading…
Cancel
Save