diff --git a/src/command/config.rs b/src/command/config.rs index c01cfb4..426bfdb 100644 --- a/src/command/config.rs +++ b/src/command/config.rs @@ -21,9 +21,9 @@ pub(crate) enum ConfigVersion { #[derive(thiserror::Error, Debug)] pub(crate) enum Error { #[error(transparent)] - Migrate(#[from] migrate::Error), + Migrate(#[from] migrate::ConfigMigrateError), #[error(transparent)] - Validate(#[from] validate::Error), + Validate(#[from] validate::ConfigValidateError), } pub(crate) fn run(command: Commands) -> Result<(), Error> { diff --git a/src/command/config/migrate.rs b/src/command/config/migrate.rs index f80e1ee..680fc49 100644 --- a/src/command/config/migrate.rs +++ b/src/command/config/migrate.rs @@ -1,79 +1,16 @@ -use std::{ - collections::HashMap, - env, fs, - io::{self, Read}, -}; -use yaml_rust::{Yaml, YamlLoader}; +mod v20240701; -pub(super) fn run(version: super::ConfigVersion) -> Result<(), Error> { - match version { - super::ConfigVersion::V20240701 => migrate_20240701(), - } -} +use super::ConfigVersion; #[derive(thiserror::Error, Debug)] -pub(crate) enum Error { +pub(crate) enum ConfigMigrateError { #[error("failed to parse the old config file (.config/default.yml)")] - ReadOldConfig(#[from] ReadYamlConfigError), + ReadOldConfig(#[from] v20240701::ReadYamlConfigError), } -#[derive(thiserror::Error, Debug)] -pub(crate) enum ReadYamlConfigError { - #[error(transparent)] - ReadFile(#[from] io::Error), - #[error(transparent)] - Yaml(#[from] yaml_rust::ScanError), - #[error("invalid config file ({0})")] - InvalidConfig(String), -} - -fn read_default_yml() -> Result, ReadYamlConfigError> { - let cwd = env::current_dir()?; - let mut default_yml = fs::File::open(cwd.join(".config/default.yml"))?; - - let mut buffer = String::new(); - default_yml.read_to_string(&mut buffer)?; - - let content = YamlLoader::load_from_str(&buffer)?; - - if content.is_empty() { - return Err(ReadYamlConfigError::InvalidConfig( - "file is empty".to_string(), - )); - } - if content.len() > 2 || content[0].is_array() { - return Err(ReadYamlConfigError::InvalidConfig( - "top-level should not be an array".to_string(), - )); - } - - let content = content[0] - .clone() - .into_hash() - .ok_or(ReadYamlConfigError::InvalidConfig( - "invalid format".to_string(), - ))?; - - let mut res = HashMap::new(); - - for (key, val) in content { - let Some(key) = key.as_str() else { - return Err(ReadYamlConfigError::InvalidConfig(format!( - "non-string key found: {:?}", - key - ))); - }; - res.insert(key.to_owned(), val); - } - - Ok(res) -} - -fn migrate_20240701() -> Result<(), Error> { - let old_config = read_default_yml()?; - - for (k, v) in old_config { - println!("{}:\n {:?}", k, v); +pub(super) fn run(version: ConfigVersion) -> Result<(), ConfigMigrateError> { + match version { + ConfigVersion::V20240701 => v20240701::run()?, } Ok(()) diff --git a/src/command/config/migrate/v20240701.rs b/src/command/config/migrate/v20240701.rs new file mode 100644 index 0000000..b9b843d --- /dev/null +++ b/src/command/config/migrate/v20240701.rs @@ -0,0 +1,64 @@ +use std::{collections::HashMap, env, fs, io::Read}; +use yaml_rust::{Yaml, YamlLoader}; + +#[derive(thiserror::Error, Debug)] +pub(crate) enum ReadYamlConfigError { + #[error(transparent)] + ReadFile(#[from] std::io::Error), + #[error(transparent)] + Yaml(#[from] yaml_rust::ScanError), + #[error("invalid config file ({0})")] + InvalidConfig(String), +} + +fn read_default_yml() -> Result, ReadYamlConfigError> { + let cwd = env::current_dir()?; + let mut default_yml = fs::File::open(cwd.join(".config/default.yml"))?; + + let mut buffer = String::new(); + default_yml.read_to_string(&mut buffer)?; + + let content = YamlLoader::load_from_str(&buffer)?; + + if content.is_empty() { + return Err(ReadYamlConfigError::InvalidConfig( + "file is empty".to_string(), + )); + } + if content.len() > 2 || content[0].is_array() { + return Err(ReadYamlConfigError::InvalidConfig( + "top-level should not be an array".to_string(), + )); + } + + let content = content[0] + .clone() + .into_hash() + .ok_or(ReadYamlConfigError::InvalidConfig( + "invalid format".to_string(), + ))?; + + let mut res = HashMap::new(); + + for (key, val) in content { + let Some(key) = key.as_str() else { + return Err(ReadYamlConfigError::InvalidConfig(format!( + "non-string key found: {:?}", + key + ))); + }; + res.insert(key.to_owned(), val); + } + + Ok(res) +} + +pub(super) fn run() -> Result<(), ReadYamlConfigError> { + let old_config = read_default_yml()?; + + for (k, v) in old_config { + println!("{}:\n {:?}", k, v); + } + + Ok(()) +} diff --git a/src/command/config/validate.rs b/src/command/config/validate.rs index 5744af4..be96c6e 100644 --- a/src/command/config/validate.rs +++ b/src/command/config/validate.rs @@ -1,7 +1,7 @@ #[derive(thiserror::Error, Debug)] #[error("failed to validate the config file")] -pub(crate) struct Error; +pub(crate) struct ConfigValidateError; -pub(super) fn run() -> Result<(), Error> { +pub(super) fn run() -> Result<(), ConfigValidateError> { unimplemented!() } diff --git a/src/main.rs b/src/main.rs index 021f452..fe9abd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn run() -> Result<(), command::Error> { fn main() { let res = run(); - if let Err(err) = res { + if let Err(err) = res { println!("An error occurred!"); println!(" raw: {:?}", err); println!(" message: {}", err);