|
|
@ -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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|