|
|
|
@ -13,30 +13,21 @@ pub struct Config { |
|
|
|
|
pub origin_descr: Descr, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
#[derive(thiserror::Error, Debug)] |
|
|
|
|
pub enum GetError { |
|
|
|
|
#[error("not found")] |
|
|
|
|
NotFound, |
|
|
|
|
Unexpected(Box<dyn Error>), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<E: Error + 'static> From<E> for GetError { |
|
|
|
|
fn from(e: E) -> GetError { |
|
|
|
|
GetError::Unexpected(Box::from(e)) |
|
|
|
|
} |
|
|
|
|
#[error(transparent)] |
|
|
|
|
Unexpected(Box<dyn Error>), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
#[derive(thiserror::Error, Debug)] |
|
|
|
|
pub enum SetError { |
|
|
|
|
NotFound, |
|
|
|
|
#[error(transparent)] |
|
|
|
|
Unexpected(Box<dyn Error>), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<E: Error + 'static> From<E> for SetError { |
|
|
|
|
fn from(e: E) -> SetError { |
|
|
|
|
SetError::Unexpected(Box::from(e)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[automock] |
|
|
|
|
pub trait Store { |
|
|
|
|
fn get(&self, domain: &str) -> Result<Config, GetError>; |
|
|
|
@ -69,17 +60,21 @@ impl Store for FSStore { |
|
|
|
|
let config_file = |
|
|
|
|
fs::File::open(self.config_file_path(domain)).map_err(|e| match e.kind() { |
|
|
|
|
io::ErrorKind::NotFound => GetError::NotFound, |
|
|
|
|
_ => e.into(), |
|
|
|
|
_ => GetError::Unexpected(Box::from(e)), |
|
|
|
|
})?; |
|
|
|
|
|
|
|
|
|
Ok(serde_json::from_reader(config_file)?) |
|
|
|
|
Ok(serde_json::from_reader(config_file).map_err(|e| GetError::Unexpected(Box::from(e)))?) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn set(&self, domain: &str, config: &Config) -> Result<(), SetError> { |
|
|
|
|
fs::create_dir_all(self.config_dir_path(domain))?; |
|
|
|
|
fs::create_dir_all(self.config_dir_path(domain)) |
|
|
|
|
.map_err(|e| SetError::Unexpected(Box::from(e)))?; |
|
|
|
|
|
|
|
|
|
let config_file = fs::File::create(self.config_file_path(domain)) |
|
|
|
|
.map_err(|e| SetError::Unexpected(Box::from(e)))?; |
|
|
|
|
|
|
|
|
|
let config_file = fs::File::create(self.config_file_path(domain))?; |
|
|
|
|
serde_json::to_writer(config_file, config)?; |
|
|
|
|
serde_json::to_writer(config_file, config) |
|
|
|
|
.map_err(|e| SetError::Unexpected(Box::from(e)))?; |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|