diff --git a/src/ci.rs b/src/ci.rs index 5c29986..1fd4696 100644 --- a/src/ci.rs +++ b/src/ci.rs @@ -1,9 +1,10 @@ use anyhow::{Context, Result}; use log::info; +use serde::{Deserialize, Serialize}; use std::env; use strum_macros::EnumString; -#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString, Serialize, Deserialize)] pub enum CIKind { /// ksnotify is running on GitLab CI. #[strum(serialize = "gitlab")] diff --git a/src/config.rs b/src/config.rs index 8722093..f433be7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use crate::{ci, notifier}; use anyhow::Result; use log::info; -use std::collections::HashMap; +use serde::{Deserialize, Serialize}; use std::env; use std::fs; use std::path::PathBuf; @@ -18,7 +18,7 @@ enum NotifierKind { Slack, } -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct Config { pub ci: ci::CIKind, pub notifier: notifier::NotifierKind, @@ -52,27 +52,9 @@ impl Config { fn from_file(path: PathBuf) -> Result { info!("cli arguments are not set, use configuration file"); let config_string = fs::read_to_string(path)?; - let doc: HashMap = serde_yaml::from_str(&config_string)?; - let ci = ci::CIKind::from_str(doc.get("ci").expect("failed to load the CI type"))?; - let notifier = notifier::NotifierKind::from_str( - doc.get("notifier") - .expect("failed to load the Notifier type"), - )?; - let suppress_skaffold = doc - .get("suppress_skaffold") - .expect("failed to load the suppress_skaffold flag") - .parse::()?; - let patch = doc - .get("patch") - .expect("failed to load the patch flag") - .parse::()?; + let config: Config = serde_yaml::from_str(&config_string)?; - Ok(Self { - ci, - notifier, - suppress_skaffold, - patch, - }) + Ok(config) } fn from_env() -> Result { diff --git a/src/notifier.rs b/src/notifier.rs index 758e3bb..da7a14c 100644 --- a/src/notifier.rs +++ b/src/notifier.rs @@ -2,9 +2,10 @@ pub mod gitlab; use crate::template; use anyhow::Result; +use serde::{Deserialize, Serialize}; use strum_macros::EnumString; -#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString, Serialize, Deserialize)] pub enum NotifierKind { #[strum(serialize = "gitlab")] GitLab,