refactor
This commit is contained in:
parent
5c515f6d87
commit
1b281d7b36
5 changed files with 76 additions and 75 deletions
|
@ -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> {
|
||||
|
|
|
@ -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<HashMap<String, Yaml>, 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(())
|
||||
|
|
64
src/command/config/migrate/v20240701.rs
Normal file
64
src/command/config/migrate/v20240701.rs
Normal file
|
@ -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<HashMap<String, Yaml>, 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(())
|
||||
}
|
|
@ -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!()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue