some error handeling
This commit is contained in:
parent
875ea1e53e
commit
1c51c53eef
|
|
@ -27,10 +27,10 @@ impl Default for Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ConfigError{
|
pub enum ConfigError {
|
||||||
IOError(&'static str),
|
IOError(&'static str),
|
||||||
ParseError(&'static str),
|
ParseError(&'static str),
|
||||||
EnvError(&'static str)
|
EnvError(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -43,20 +43,31 @@ impl Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_default(cfg_file: &str) -> Result<(), ConfigError> {
|
pub fn write_default(cfg_file: &str) -> Result<(), ConfigError> {
|
||||||
let buf = serde_json::to_string_pretty(&Self::default())
|
let buf = serde_json::to_string_pretty(&Self::default()).or_else(|_| {
|
||||||
.or_else(|_| return Err(ConfigError::ParseError("could not serialize default config")))?;
|
return 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")))?;
|
let mut f = File::create(cfg_file)
|
||||||
f.write_all(&buf.as_bytes())
|
.or_else(|_| Err(ConfigError::IOError("Could not open config file")))?;
|
||||||
.or_else(|_| return Err(ConfigError::IOError("could not write default config to file")))?;
|
f.write_all(&buf.as_bytes()).or_else(|_| {
|
||||||
|
return Err(ConfigError::IOError(
|
||||||
|
"could not write default config to file",
|
||||||
|
));
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expected_locations() -> Result<Vec<PathBuf>, ConfigError> {
|
pub fn expected_locations() -> Result<Vec<PathBuf>, ConfigError> {
|
||||||
let cfg_name = "rusty_task.json";
|
let cfg_name = "rusty_task.json";
|
||||||
let home = var("HOME").or(Err(ConfigError::EnvError("$HOME environment variable not set")))?;
|
let home = var("HOME").or(Err(ConfigError::EnvError(
|
||||||
let pwd = var("PWD").or(Err(ConfigError::EnvError("$PWD environment variable not set")))?;
|
"$HOME environment variable not set",
|
||||||
|
)))?;
|
||||||
|
let pwd = var("PWD").or(Err(ConfigError::EnvError(
|
||||||
|
"$PWD environment variable not set",
|
||||||
|
)))?;
|
||||||
|
|
||||||
let mut home_config_cfg = PathBuf::from(home.clone());
|
let mut home_config_cfg = PathBuf::from(home.clone());
|
||||||
home_config_cfg.push(".config");
|
home_config_cfg.push(".config");
|
||||||
|
|
|
||||||
31
src/main.rs
31
src/main.rs
|
|
@ -5,7 +5,7 @@ mod todo;
|
||||||
use crate::cli::Args;
|
use crate::cli::Args;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::{Config, ConfigError};
|
||||||
use crate::todo::File as TodoFile;
|
use crate::todo::File as TodoFile;
|
||||||
use crate::todo::{Status as TaskStatus, TaskGroup};
|
use crate::todo::{Status as TaskStatus, TaskGroup};
|
||||||
use chrono::naive::NaiveDate;
|
use chrono::naive::NaiveDate;
|
||||||
|
|
@ -26,16 +26,21 @@ use std::{env, str};
|
||||||
//TODO create custom errors for better error handling
|
//TODO create custom errors for better error handling
|
||||||
//TODO Default path for note_dir should start with curent path not home
|
//TODO Default path for note_dir should start with curent path not home
|
||||||
|
|
||||||
|
// Nested errors look bad, code smell?
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ExitError {
|
enum ExitError {
|
||||||
ConfigError(String),
|
ConfigError(ConfigError),
|
||||||
IOError(String, io::Error),
|
IOError(String, io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), ExitError> {
|
fn main() -> Result<(), ExitError> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let expected_cfg_files = Config::expected_locations().unwrap();
|
let expected_cfg_files = match Config::expected_locations() {
|
||||||
|
Err(e) => return Err(ExitError::ConfigError(e)),
|
||||||
|
Ok(cfg_files) => cfg_files,
|
||||||
|
};
|
||||||
|
|
||||||
let cfg_files: Vec<&Path> = expected_cfg_files
|
let cfg_files: Vec<&Path> = expected_cfg_files
|
||||||
.iter()
|
.iter()
|
||||||
.map(|file| Path::new(file))
|
.map(|file| Path::new(file))
|
||||||
|
|
@ -43,12 +48,9 @@ fn main() -> Result<(), ExitError> {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if cfg_files.len() <= 0 {
|
if cfg_files.len() <= 0 {
|
||||||
let status = Config::write_default(expected_cfg_files[0].to_str().unwrap());
|
match Config::write_default(expected_cfg_files[0].to_str()?) {
|
||||||
if let Err(e) = status {
|
Err(e) => return Err(ExitError::ConfigError(e)),
|
||||||
return Err(ExitError::ConfigError(format!(
|
_ => (),
|
||||||
"Could not write to default cfg location: {:#?}",
|
|
||||||
e
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,9 +73,9 @@ fn main() -> Result<(), ExitError> {
|
||||||
let data_dir = match &cfg.notes_dir {
|
let data_dir = match &cfg.notes_dir {
|
||||||
Some(dir) => get_data_dir(dir),
|
Some(dir) => get_data_dir(dir),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ExitError::ConfigError(
|
return Err(ExitError::ConfigError(ConfigError::IOError(
|
||||||
"Could not get notes dir from config".to_string(),
|
"Could not get notes dir from config",
|
||||||
))
|
)))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -125,8 +127,6 @@ fn main() -> Result<(), ExitError> {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// let new_file = write_file(&data_dir, &today, &data);
|
|
||||||
|
|
||||||
let content = generate_file_content(&data, &today);
|
let content = generate_file_content(&data, &today);
|
||||||
let file_path = get_filepath(&data_dir, &today);
|
let file_path = get_filepath(&data_dir, &today);
|
||||||
write_file(&file_path, &content);
|
write_file(&file_path, &content);
|
||||||
|
|
@ -138,7 +138,8 @@ fn main() -> Result<(), ExitError> {
|
||||||
let data = sections
|
let data = sections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
||||||
.collect(); let content = generate_file_content(&data, &today);
|
.collect();
|
||||||
|
let content = generate_file_content(&data, &today);
|
||||||
let file_path = get_filepath(&data_dir, &today);
|
let file_path = get_filepath(&data_dir, &today);
|
||||||
write_file(&file_path, &content);
|
write_file(&file_path, &content);
|
||||||
file_path
|
file_path
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue