jannie

jannie (mirror)
Log | Files | Refs | README | LICENSE

mod.rs (1594B)


      1 use std::path::{Path, PathBuf};
      2 
      3 pub mod logging;
      4 
      5 #[derive(Clone, Debug, serde::Deserialize)]
      6 pub struct Config {
      7     #[serde(default)]
      8     pub logging: logging::LoggingConfig,
      9     pub root: PathBuf,
     10     #[serde(default)]
     11     pub inventory_format: InventoryFormat,
     12     #[serde(default)]
     13     pub filters: Vec<Filter>,
     14 }
     15 
     16 impl Config {
     17     pub fn new(config_path: &Path) -> Result<Self, Error> {
     18         let config = config::Config::builder()
     19             .add_source(config::File::from(config_path))
     20             .build()?;
     21         Ok(config.try_deserialize()?)
     22     }
     23 }
     24 
     25 #[derive(Clone, Debug, Default, serde::Deserialize)]
     26 #[serde(rename_all = "snake_case")]
     27 pub enum InventoryFormat {
     28     #[default]
     29     Yaml,
     30     Json,
     31 }
     32 
     33 #[derive(Clone, Debug, serde::Deserialize)]
     34 pub struct Filter {
     35     pub path: PathBuf,
     36     #[serde(default)]
     37     #[serde(rename = "type")]
     38     pub filter_type: FilterType,
     39 }
     40 
     41 impl Filter {
     42     pub fn is_whitelist(&self) -> bool {
     43         self.filter_type.is_whitelist()
     44     }
     45 
     46     pub fn is_blacklist(&self) -> bool {
     47         self.filter_type.is_blacklist()
     48     }
     49 }
     50 
     51 #[derive(Clone, Debug, Default, serde::Deserialize)]
     52 #[serde(rename_all = "snake_case")]
     53 pub enum FilterType {
     54     #[default]
     55     Whitelist,
     56     Blacklist,
     57 }
     58 
     59 impl FilterType {
     60     pub fn is_whitelist(&self) -> bool {
     61         matches!(self, FilterType::Whitelist)
     62     }
     63 
     64     pub fn is_blacklist(&self) -> bool {
     65         matches!(self, FilterType::Blacklist)
     66     }
     67 }
     68 
     69 #[derive(thiserror::Error, Debug)]
     70 pub enum Error {
     71     #[error("Error building config: {0}")]
     72     Build(#[from] config::ConfigError),
     73 }