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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use std::{
    any,
    collections::{HashMap, HashSet},
    io::{self, Write as _},
};

use anstream::stream::{AsLockedWrite, RawStream};
use anstyle::{AnsiColor, Color, Style};
use smart_config::{
    metadata::ConfigMetadata,
    value::{FileFormat, ValueOrigin, WithOrigin},
    visit::{ConfigVisitor, VisitConfig},
    ConfigRepository, ParseError, ParseErrors,
};

use crate::{
    utils::{write_json_value, write_value, STRING},
    ParamRef, Printer, CONFIG_PATH,
};

const SECTION: Style = Style::new().bold();
const ARROW: Style = Style::new().bold();
const INACTIVE: Style = Style::new().italic();
const RUST: Style = Style::new().dimmed();
const JSON_FILE: Style = Style::new()
    .bg_color(Some(Color::Ansi(AnsiColor::Cyan)))
    .fg_color(None);
const YAML_FILE: Style = Style::new()
    .bg_color(Some(Color::Ansi(AnsiColor::Green)))
    .fg_color(None);
const DOTENV_FILE: Style = Style::new()
    .bg_color(Some(Color::Ansi(AnsiColor::Magenta)))
    .fg_color(None);
const ERROR_LABEL: Style = Style::new()
    .bold()
    .bg_color(Some(Color::Ansi(AnsiColor::Red)))
    .fg_color(None);

#[derive(Debug)]
struct ParamValuesVisitor {
    config: &'static ConfigMetadata,
    variant: Option<usize>,
    param_values: HashMap<usize, serde_json::Value>,
}

impl ParamValuesVisitor {
    fn new(config: &'static ConfigMetadata) -> Self {
        Self {
            config,
            variant: None,
            param_values: HashMap::new(),
        }
    }
}

impl ConfigVisitor for ParamValuesVisitor {
    fn visit_tag(&mut self, variant_index: usize) {
        self.variant = Some(variant_index);
    }

    fn visit_param(&mut self, param_index: usize, value: &dyn any::Any) {
        let param = self.config.params[param_index];
        let json = if param.type_description().contains_secrets() {
            "[REDACTED]".into()
        } else {
            param.deserializer.serialize_param(value)
        };
        self.param_values.insert(param_index, json);
    }

    fn visit_nested_config(&mut self, _config_index: usize, _config: &dyn VisitConfig) {
        // Don't recurse into nested configs, we debug them separately
    }
}

/// Type ID of the config + path to the config / param.
type ErrorKey = (any::TypeId, String);

#[derive(Debug)]
struct ConfigErrors {
    by_param: HashMap<ErrorKey, Vec<ParseError>>,
    by_config: HashMap<ErrorKey, Vec<ParseError>>,
}

impl ConfigErrors {
    fn new(repo: &ConfigRepository<'_>) -> Self {
        let mut by_param = HashMap::<_, Vec<_>>::new();
        let mut by_config = HashMap::<_, Vec<_>>::new();
        for config_parser in repo.iter() {
            if !config_parser.config().is_top_level() {
                // The config should be parsed as a part of the parent config. Filtering out these configs
                // might not be sufficient to prevent error duplication because a parent config may be inserted after
                // the config itself, so we perform additional deduplication below.
                continue;
            }

            if let Err(errors) = config_parser.parse_opt() {
                // Only insert errors for a certain param / config if errors for it were not encountered before.
                let mut new_params = HashSet::new();
                let mut new_configs = HashSet::new();

                for err in errors {
                    let key = (err.config().ty.id(), err.path().to_owned());
                    if err.param().is_some() {
                        if !by_param.contains_key(&key) || new_params.contains(&key) {
                            by_param.entry(key.clone()).or_default().push(err);
                            new_params.insert(key);
                        }
                    } else if !by_config.contains_key(&key) || new_configs.contains(&key) {
                        by_config.entry(key.clone()).or_default().push(err);
                        new_configs.insert(key);
                    }
                }
            }
        }
        Self {
            by_param,
            by_config,
        }
    }
}

impl From<ConfigErrors> for Result<(), ParseErrors> {
    fn from(errors: ConfigErrors) -> Self {
        let errors = errors
            .by_config
            .into_values()
            .chain(errors.by_param.into_values())
            .flatten();
        errors.collect()
    }
}

impl<W: RawStream + AsLockedWrite> Printer<W> {
    /// Prints debug info for all param values in the provided `repo`. If params fail to deserialize,
    /// corresponding error(s) are output as well.
    ///
    /// # Errors
    ///
    /// - Propagates I/O errors.
    /// - Returns the exhaustive parsing result. Depending on the application, some parsing errors (e.g., missing params for optional configs)
    ///   may not be fatal.
    #[allow(clippy::missing_panics_doc)] // false positive
    pub fn print_debug(
        self,
        repo: &ConfigRepository<'_>,
        mut filter: impl FnMut(ParamRef<'_>) -> bool,
    ) -> io::Result<Result<(), ParseErrors>> {
        let mut writer = self.writer;
        if repo.sources().is_empty() {
            writeln!(&mut writer, "configuration is empty")?;
            return Ok(Ok(()));
        }

        writeln!(&mut writer, "{SECTION}Configuration sources:{SECTION:#}")?;
        for source in repo.sources() {
            write!(&mut writer, "- ")?;
            write_origin(&mut writer, &source.origin)?;
            writeln!(&mut writer, ", {} param(s)", source.param_count)?;
        }

        writeln!(&mut writer)?;
        writeln!(&mut writer, "{SECTION}Values:{SECTION:#}")?;

        let errors = ConfigErrors::new(repo);
        let merged = repo.merged();
        for config_parser in repo.iter() {
            let config = config_parser.config();
            let config_name = config.metadata().ty.name_in_code();
            let config_id = (config.metadata().ty.id(), config.prefix().to_owned());

            if let Some(errors) = errors.by_config.get(&config_id) {
                writeln!(
                    writer,
                    "{CONFIG_PATH}{}{CONFIG_PATH:#} {RUST}[Rust: {config_name}]{RUST:#}, config",
                    config.prefix()
                )?;
                write_de_errors(&mut writer, errors)?;
            }

            let (variant, mut param_values) =
                if let Ok(Some(boxed_config)) = config_parser.parse_opt() {
                    let visitor_fn = config.metadata().visitor;
                    let mut visitor = ParamValuesVisitor::new(config.metadata());
                    visitor_fn(boxed_config.as_ref(), &mut visitor);
                    (visitor.variant, visitor.param_values)
                } else {
                    (None, HashMap::new())
                };

            let variant = variant.map(|idx| {
                // `unwrap()` is safe by construction: if there's an active variant, the config must have a tag
                let tag = config.metadata().tag.unwrap();
                let name = tag.variants[idx].name;
                // Add the tag value, using the fact that the tag is always the last param in the config.
                let tag_param_idx = config.metadata().params.len() - 1;
                param_values.insert(tag_param_idx, name.into());

                ActiveTagVariant {
                    canonical_path: ParamRef {
                        config,
                        param: tag.param,
                    }
                    .canonical_path(),
                    name,
                }
            });

            for (param_idx, param) in config.metadata().params.iter().enumerate() {
                let param_ref = ParamRef { config, param };
                if !filter(param_ref) {
                    continue;
                }
                let canonical_path = param_ref.canonical_path();

                let raw_value = merged.pointer(&canonical_path);
                let param_value = param_values.get(&param_idx);
                let mut param_written = false;
                if param_value.is_some() || raw_value.is_some() {
                    write_param(
                        &mut writer,
                        param_ref,
                        &canonical_path,
                        param_value,
                        raw_value,
                        variant.as_ref(),
                    )?;
                    param_written = true;
                }

                let param_id = (config_id.0, canonical_path.clone());
                if let Some(errors) = errors.by_param.get(&param_id) {
                    if !param_written {
                        let field_name = param.rust_field_name;
                        let rust_variant = if let Some(variant) = param.tag_variant {
                            format!("::{}", variant.rust_name)
                        } else {
                            String::new()
                        };
                        writeln!(
                            writer,
                            "{canonical_path} {RUST}[Rust: {config_name}{rust_variant}.{field_name}]{RUST:#}"
                        )?;
                    }
                    write_de_errors(&mut writer, errors)?;
                }
            }
        }
        Ok(errors.into())
    }
}

fn write_origin(writer: &mut impl io::Write, origin: &ValueOrigin) -> io::Result<()> {
    match origin {
        ValueOrigin::EnvVars => {
            write!(writer, "{DOTENV_FILE}env{DOTENV_FILE:#}")
        }
        ValueOrigin::File { name, format } => {
            let style = match format {
                FileFormat::Json => JSON_FILE,
                FileFormat::Yaml => YAML_FILE,
                FileFormat::Dotenv => DOTENV_FILE,
                _ => Style::new(),
            };
            write!(writer, "{style}{format}:{style:#}{name}")
        }
        ValueOrigin::Path { source, path } => {
            if matches!(source.as_ref(), ValueOrigin::EnvVars) {
                write!(writer, "{DOTENV_FILE}env:{DOTENV_FILE:#}{path:?}")
            } else {
                write_origin(writer, source)?;
                if !path.is_empty() {
                    write!(writer, " {ARROW}->{ARROW:#} .{path}")?;
                }
                Ok(())
            }
        }
        ValueOrigin::Synthetic { source, transform } => {
            write_origin(writer, source)?;
            write!(writer, " {ARROW}->{ARROW:#} {transform}")
        }
        _ => write!(writer, "{origin}"),
    }
}

#[derive(Debug)]
struct ActiveTagVariant {
    canonical_path: String,
    name: &'static str,
}

fn write_param(
    writer: &mut impl io::Write,
    param_ref: ParamRef<'_>,
    path: &str,
    visited_value: Option<&serde_json::Value>,
    raw_value: Option<&WithOrigin>,
    active_variant: Option<&ActiveTagVariant>,
) -> io::Result<()> {
    let activity_style = if visited_value.is_some() {
        Style::new()
    } else {
        INACTIVE
    };
    let rust_variant = if let Some(variant) = param_ref.param.tag_variant {
        format!("::{}", variant.rust_name)
    } else {
        String::new()
    };

    write!(
        writer,
        "{activity_style}{path}{activity_style:#} {RUST}[Rust: {}{rust_variant}.{}]{RUST:#}",
        param_ref.config.metadata().ty.name_in_code(),
        param_ref.param.rust_field_name
    )?;

    if let Some(value) = visited_value {
        write!(writer, " = ")?;
        write_json_value(writer, value, 0)?;
        writeln!(writer)?;
    } else {
        writeln!(writer)?;
    }

    if let (Some(param_variant), Some(active_variant)) =
        (param_ref.param.tag_variant, active_variant)
    {
        let tag_path = &active_variant.canonical_path;
        let param_variant_name = param_variant.name;
        let (label, eq) = if param_variant_name == active_variant.name {
            ("Active", "==")
        } else {
            ("Inactive", "!=")
        };
        writeln!(
            writer,
            "  {label}: {tag_path} {eq} {STRING}'{param_variant_name}'{STRING:#}"
        )?;
    }

    if let Some(value) = raw_value {
        write!(writer, "  Raw: ")?;
        write_value(writer, value, 2)?;
        writeln!(writer)?;
        write!(writer, "  Origin: ")?;
        write_origin(writer, &value.origin)?;
        writeln!(writer)?;
    }
    Ok(())
}

fn write_de_errors(writer: &mut impl io::Write, errors: &[ParseError]) -> io::Result<()> {
    if errors.len() == 1 {
        write!(writer, "  {ERROR_LABEL}Error:{ERROR_LABEL:#} ")?;
        write_de_error(writer, &errors[0])
    } else {
        writeln!(writer, "  {ERROR_LABEL}Errors:{ERROR_LABEL:#}")?;
        for err in errors {
            write!(writer, "  - ")?;
            write_de_error(writer, err)?;
        }
        Ok(())
    }
}

fn write_de_error(writer: &mut impl io::Write, err: &ParseError) -> io::Result<()> {
    writeln!(writer, "{}", err.inner())?;
    if let Some(validation) = err.validation() {
        writeln!(writer, "    {SECTION}validation:{SECTION:#} {validation}")?;
    }
    writeln!(
        writer,
        "    at {SECTION}{path}{SECTION:#}",
        path = err.path()
    )?;
    if !matches!(err.origin(), ValueOrigin::Unknown) {
        write!(writer, "    ")?;
        write_origin(writer, err.origin())?;
        writeln!(writer)?;
    }
    Ok(())
}