path handling and unwraps

This commit is contained in:
2024-02-28 17:20:55 -05:00
parent 1c51c53eef
commit e0c81885c8
3 changed files with 33 additions and 79 deletions

View File

@@ -11,17 +11,17 @@ use std::path::PathBuf;
#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
pub editor: Option<String>,
pub sections: Option<Vec<String>>,
pub notes_dir: Option<String>,
pub editor: String,
pub sections: Vec<String>,
pub notes_dir: String,
}
impl Default for Config {
fn default() -> Self {
Config {
editor: Some("nano".into()),
sections: Some(vec!["Daily".into(), "Weekly".into(), "Monthly".into()]),
notes_dir: Some("Notes".into()),
editor: "nano".into(),
sections: vec!["Daily".into(), "Weekly".into(), "Monthly".into()],
notes_dir: "./Notes".into(),
}
}
}
@@ -44,17 +44,17 @@ impl Config {
pub fn write_default(cfg_file: &str) -> Result<(), ConfigError> {
let buf = serde_json::to_string_pretty(&Self::default()).or_else(|_| {
return Err(ConfigError::ParseError(
Err(ConfigError::ParseError(
"could not serialize default config",
));
))
})?;
let mut f = File::create(cfg_file)
.or_else(|_| Err(ConfigError::IOError("Could not open config file")))?;
f.write_all(&buf.as_bytes()).or_else(|_| {
return Err(ConfigError::IOError(
Err(ConfigError::IOError(
"could not write default config to file",
));
))
})?;
Ok(())