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 client = dbg!(get_mastodon_connection());
let top = dbg!(config.last_favorite.to_string());
let storage: Box<dyn Storage> = if let Some(joplin) = &config.joplin {
Box::new(Joplin::new_from_config(&joplin))
} else {
Box::new(Filesystem::new())
let storage: Box<dyn Storage> = 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<String> {
if first.is_some() {
first
} else {
@ -66,9 +65,9 @@ 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 {
match elefren_toml::from_file("mastodon.toml") {
Ok(data) => Mastodon::from(data),
Err(_) => {
println!("Your server URL: ");
let mut server = String::new();
io::stdin()
@ -83,4 +82,5 @@ fn get_mastodon_connection() -> Mastodon {
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon
}
}
}

20
src/storage/joplin.rs

@ -73,33 +73,37 @@ 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(),
}
} else {
},
None => {
println!("The notebook {} does not exist", &config.folder);
panic!("The specified notebook does not exist");
}
}
}
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 {
if folder.title == *config.folder {
return Some(folder.id);
}
}
None
} else {
}
Err(_) => {
println!("Failed to retrieve the notebook list");
panic!("Failed to retrieve Joplin notebook list");
}
}
}
fn get_folder_list(config: &JoplinConfig) -> Result<Vec<Folder>, Error> {
let mut page = 1;

Loading…
Cancel
Save