1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
//! 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<String, JsonValue>,
origins: &BTreeMap<String, Origin>,
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<String, JsonValue>,
origins: &BTreeMap<String, Origin>,
files: &[PathBuf],
values: Values,
) -> JsonValue {
let options: serde_json::Map<String, JsonValue> = 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::<Vec<_>>(),
"options": options,
})
}
|