Browse Source

Use a single configuration file

master
Julio Biason 4 years ago
parent
commit
569f353efe
  1. 103
      src/config/mod.rs
  2. 50
      src/main.rs

103
src/config/mod.rs

@ -19,6 +19,7 @@
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use elefren::Data;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use serde_derive::Serialize; use serde_derive::Serialize;
@ -31,49 +32,83 @@ pub struct JoplinConfig {
pub token: String, pub token: String,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct Favourite {
pub last: String,
}
impl Favourite {
pub fn new() -> Self {
Self { last: "".into() }
}
pub fn new_with_value(new_last: &str) -> Self {
Self {
last: new_last.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
pub last_favorite: String, pub favourite: Favourite,
pub mastodon: Data,
pub joplin: Option<JoplinConfig>, pub joplin: Option<JoplinConfig>,
} }
/// Errors while loading the configuration file
#[derive(Debug)]
pub enum ConfigError {
/// There is no configuration file
NoConfig,
/// The configuration file format is invalid
InvalidConfig,
}
impl From<toml::de::Error> for ConfigError {
fn from(_: toml::de::Error) -> Self {
// Since we only have one single error so far...
ConfigError::InvalidConfig
}
}
impl From<std::io::Error> for ConfigError {
fn from(_: std::io::Error) -> Self {
// This is not optimal: Some IO errors we can't recover (like
// PermissionDenied)
ConfigError::NoConfig
}
}
impl Config { impl Config {
pub fn get() -> Config { pub fn get() -> Result<Config, ConfigError> {
if let Ok(mut fp) = File::open("downfav.toml") { let mut fp = File::open("downfav.toml")?;
let mut contents = String::new(); let mut contents = String::new();
fp.read_to_string(&mut contents).unwrap(); fp.read_to_string(&mut contents).unwrap();
let config: Config = toml::from_str(&contents).unwrap_or(Config { Ok(toml::from_str(&contents)?)
last_favorite: "".to_string(), }
joplin: None,
}); pub fn save(self, most_recent_favourite: &str) -> Self {
config let new_configuration = Self {
} else { favourite: Favourite::new_with_value(most_recent_favourite),
Config { ..self
last_favorite: "".to_string(), };
joplin: None, let content = toml::to_string(&new_configuration).unwrap();
}
if let Ok(mut fp) = File::create("downfav.toml") {
fp.write_all(content.as_bytes()).unwrap();
} }
new_configuration
} }
}
pub fn save(&self, most_recent_favourite: Option<String>) -> () { impl From<elefren::data::Data> for Config {
if let Some(id) = most_recent_favourite { fn from(data: elefren::data::Data) -> Self {
let new_configuration = Config { Config {
last_favorite: id, favourite: Favourite::new(),
joplin: match &self.joplin { mastodon: data,
None => None, joplin: None, // at least, not yet
Some(x) => Some(JoplinConfig {
folder: x.folder.to_string(),
token: x.token.to_string(),
port: x.port,
}),
},
};
let content = toml::to_string(&new_configuration).unwrap();
if let Ok(mut fp) = File::create("downfav.toml") {
fp.write_all(content.as_bytes()).unwrap();
}
} }
} }
} }

50
src/main.rs

@ -19,7 +19,6 @@
use std::io; use std::io;
use elefren::helpers::cli; use elefren::helpers::cli;
use elefren::helpers::toml as elefren_toml;
use elefren::prelude::*; use elefren::prelude::*;
use crate::storage::data::Data; use crate::storage::data::Data;
@ -31,14 +30,21 @@ mod config;
mod storage; mod storage;
fn main() { fn main() {
let config = dbg!(config::Config::get()); let config = match config::Config::get() {
let client = dbg!(get_mastodon_connection()); Ok(config) => config,
let top = dbg!(config.last_favorite.to_string()); Err(_) => {
let data = connect_to_mastodon();
config::Config::from(data)
}
};
let top = dbg!(config.favourite.last.to_string());
let storage: Box<dyn Storage> = match &config.joplin { let storage: Box<dyn Storage> = match &config.joplin {
Some(joplin) => Box::new(Joplin::new_from_config(&joplin)), Some(joplin) => Box::new(Joplin::new_from_config(&joplin)),
None => Box::new(Filesystem::new()), None => Box::new(Filesystem::new()),
}; };
let client = Mastodon::from(config.mastodon.clone());
let most_recent_favourite = client let most_recent_favourite = client
.favourites() .favourites()
.unwrap() .unwrap()
@ -59,28 +65,22 @@ fn main() {
} }
}); });
config.save(most_recent_favourite); if let Some(new_favourite) = most_recent_favourite {
config.save(&new_favourite);
}
} }
/// Get a connection with Mastodon; if there is no set up with any account yet, /// Create a connection to a mastodon server.
/// requests one. fn connect_to_mastodon() -> elefren::data::Data {
fn get_mastodon_connection() -> Mastodon { println!("Your server URL: ");
match elefren_toml::from_file("mastodon.toml") { let mut server = String::new();
Ok(data) => Mastodon::from(data), io::stdin()
Err(_) => { .read_line(&mut server)
println!("Your server URL: "); .expect("You need to enter yoru 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()) let registration = Registration::new(server.trim())
.client_name("downfav") .client_name("downfav")
.build() .build()
.unwrap(); .unwrap();
let mastodon = cli::authenticate(registration).unwrap(); cli::authenticate(registration).unwrap().data
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon
}
}
} }

Loading…
Cancel
Save