use config::{Config, ConfigError, Environment, File};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub enum Operators {
Equals,
NotEquals,
Contains,
NotContains,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Variable {
pub key: String,
pub operator: Operators,
pub value: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Tag {
pub operator: Operators,
pub name: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Pagination {
pub start_page: String,
pub max_depth: String,
pub page_size: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Query {
pub name: Option<String>,
pub variables: Option<Vec<Variable>>,
pub tags: Option<Vec<Tag>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Workspaces {
pub query: Query,
pub pagination: Pagination,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Core {
pub log: String,
pub token: String,
pub org: String,
pub project: Option<String>,
pub output: String,
pub workspaces: Workspaces,
}
impl Core {
pub fn new() -> Result<Self, ConfigError> {
let s = Config::builder()
.set_default("log", "warn".to_string())?
.set_default("output", "report.json".to_string())?
.set_default("workspaces.pagination.start_page", "1".to_string())?
.set_default("workspaces.pagination.max_depth", "1".to_string())?
.set_default("workspaces.pagination.page_size", "20".to_string())?
.add_source(File::with_name("settings.toml").required(false))
.add_source(Environment::default())
.build()?;
s.try_deserialize()
}
}