Browse Source

Rust-analyzer suggested `match` and it was nice

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

40
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,21 +65,22 @@ 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()
.read_line(&mut server) .read_line(&mut server)
.expect("You need to enter yoru server URL"); .expect("You need to enter yoru server URL");
let registration = Registration::new(server.trim()) let registration = Registration::new(server.trim())
.client_name("downfav") .client_name("downfav")
.build() .build()
.unwrap(); .unwrap();
let mastodon = cli::authenticate(registration).unwrap(); let mastodon = cli::authenticate(registration).unwrap();
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap(); elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon mastodon
}
} }
} }

34
src/storage/joplin.rs

@ -73,31 +73,35 @@ 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(),
},
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<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)) {
for folder in folders { Ok(folders) => {
if folder.title == *config.folder { for folder in folders {
return Some(folder.id); 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");
} }
} }

Loading…
Cancel
Save