//! The report of a configuration: what each option ended up as and where its value came //! from, for a program to print (`--print-config`) or to serve. use std::collections::BTreeMap; use std::path::PathBuf; use serde_json::Value as JsonValue; use crate::layer::{Origin, Provenance}; use crate::option::Schema; /// How a report shows text with placeholders. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub enum Values { /// As it ends up. #[default] Interpolated, /// As written, placeholders and all. Original, } /// The reports of a configuration, either way of showing text with placeholders: made /// once, at a program's start, for it to serve. #[derive(Debug, Clone, Default)] pub struct Reports { interpolated: JsonValue, original: JsonValue, } impl Reports { /// Both reports (`report`). pub fn of( schema: &Schema, settings: &serde_json::Map, origins: &BTreeMap, files: &[PathBuf], ) -> Reports { Reports { interpolated: report(schema, settings, origins, files, Values::Interpolated), original: report(schema, settings, origins, files, Values::Original), } } pub fn get(&self, values: Values) -> &JsonValue { match values { Values::Interpolated => &self.interpolated, Values::Original => &self.original, } } } /// The effective configuration with each value's source, as JSON: `files`, those read, /// and `options`, each of `schema`'s by name, with its `value` (of `settings`, the /// settings in effect by option name; `null` where they hold none), its `source` /// (`default`, `file` with the `file`, or `argument`), and where they apply `imported`, /// `templates`, `unset` and `secret`. Imported values are left out (they may be secret); /// their importers are listed instead. Secrets are left out however they were given. /// Text with placeholders is shown as `values` says, the text as written listed. Hidden /// options are left out unless set. pub fn report( schema: &Schema, settings: &serde_json::Map, origins: &BTreeMap, files: &[PathBuf], values: Values, ) -> JsonValue { let options: serde_json::Map = schema .options .iter() .filter(|spec| !spec.hidden || origins.contains_key(spec.name)) .map(|spec| { let mut entry = serde_json::Map::new(); let origin = origins.get(spec.name); match origin { None => { entry.insert("source".into(), "default".into()); } Some(origin) => { match &origin.provenance { Provenance::File(path) => { entry.insert("source".into(), "file".into()); entry.insert("file".into(), path.display().to_string().into()); } Provenance::Argument => { entry.insert("source".into(), "argument".into()); } } if origin.is_imported() { entry.insert("imported".into(), origin.imports.clone().into()); } // Of a secret, the text may hold some of it. if !origin.templates.is_empty() && !spec.secret { entry.insert("templates".into(), origin.templates.clone().into()); } if origin.unset { entry.insert("unset".into(), true.into()); } } } let given = origin.is_some_and(|origin| !origin.unset); if spec.secret && given { entry.insert("secret".into(), true.into()); } else if !origin.is_some_and(Origin::is_imported) { let written = origin.and_then(|origin| origin.written.clone()); let value = match (values, written) { (Values::Original, Some(written)) => written, _ => settings.get(spec.name).cloned().unwrap_or(JsonValue::Null), }; entry.insert("value".into(), value); } (spec.name.to_owned(), JsonValue::Object(entry)) }) .collect(); serde_json::json!({ "files": files.iter().map(|path| path.display().to_string()).collect::>(), "options": options, }) }