mogh_config 3.0.2

Utilities to merge config from multiple files / directories.
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
use std::{path::PathBuf, sync::LazyLock};

use serde::de::DeserializeOwned;

/// The json type name of a value, for error messages
/// which must not include the value itself.
pub fn value_type(value: &serde_json::Value) -> &'static str {
  match value {
    serde_json::Value::Null => "null",
    serde_json::Value::Bool(_) => "boolean",
    serde_json::Value::Number(_) => "number",
    serde_json::Value::String(_) => "string",
    serde_json::Value::Array(_) => "array",
    serde_json::Value::Object(_) => "object",
  }
}

/// Redacts values from a serde error message, since config values
/// may be secrets and errors are logged: double quoted strings
/// (`invalid type: string "hunter2"`) and backticked tokens
/// (`invalid type: integer `4829``). An unknown enum variant or
/// field is redacted whole, whatever it holds (serde doesn't escape
/// it), keeping the names expected after it:
/// `unknown variant [redacted], expected `fast` or `safe``.
pub fn redact_serde_error(e: &serde_json::Error) -> String {
  redact_message(&e.to_string(), false)
}

/// [redact_serde_error] for the message of any parser. serde's own
/// message comes first, or with `path_prefix` (yaml) may follow
/// the path of the value it is about (`server.mode: ...`).
fn redact_message(message: &str, path_prefix: bool) -> String {
  // Where serde's message starts. serde writes each value right
  // after its own wording, and the first one comes after the path,
  // so the wording before the first quote / backtick tells where
  // the path ends: the path (yaml keys) may hold anything.
  static WORDING: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(
      r"(?:\A|: )((?:invalid (?:type|value): (?:string|character|boolean|integer|floating point)|unknown (?:variant|field)|missing field|duplicate field) )\z",
    )
    .unwrap()
  });
  let start = match message.find(['"', '`']) {
    Some(quote) if path_prefix => {
      match WORDING.captures(&message[..quote]) {
        Some(wording) => wording.get(1).unwrap().start(),
        // A quote or backtick in the path, which can't be told
        // apart from a value's: keep only the text before it, and
        // where the error is.
        None => {
          static LOCATION: LazyLock<regex::Regex> =
            LazyLock::new(|| {
              regex::Regex::new(
                r" at (?:line \d+ column \d+|position \d+)\z",
              )
              .unwrap()
            });
          let location = LOCATION
            .find(&message[quote..])
            .map_or("", |location| location.as_str());
          return format!(
            "{}[redacted]{location}",
            &message[..quote]
          );
        }
      }
    }
    _ => 0,
  };
  // serde writes an unknown enum variant or field name in
  // backticks without escaping it, so it may hold backticks or
  // quotes itself: it runs up to serde's own `, expected` / `, there
  // are no`, the last one, as it may hold these too. The names
  // expected after it are the program's, and stay readable. Only at
  // the start of serde's message: text like it anywhere else is
  // inside a value, eg. `invalid type: string "unknown variant `a`,
  // expected b", expected u16`, whose string is redacted whole
  // below.
  static UNKNOWN: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(
      r"(?s)\A(unknown (?:variant|field) )`.*`(, expected|, there are no)",
    )
    .unwrap()
  });
  let (path, rest) = message.split_at(start);
  if let Some(unknown) = UNKNOWN.captures(rest) {
    let end = unknown.get(0).unwrap().end();
    return format!(
      "{path}{}[redacted]{}{}",
      &unknown[1],
      &unknown[2],
      &rest[end..]
    );
  }
  redact_values(message)
}

/// Redacts the values serde writes into its messages: strings
/// escaped in double quotes (`string "a \"b\""`), a character in
/// backticks (which may be a backtick itself), and numbers and
/// booleans in backticks.
fn redact_values(message: &str) -> String {
  static QUOTED: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(
      r#"(?s)"(?:[^"\\]|\\.)*"|character `.`|`[^`]*`"#,
    )
    .unwrap()
  });
  QUOTED
    .replace_all(message, |caps: &regex::Captures| {
      if caps[0].starts_with("character ") {
        "character [redacted]"
      } else {
        "[redacted]"
      }
    })
    .into_owned()
}

/// The message of a toml error, redacted like [redact_serde_error],
/// with the line and column of its span in `contents`. Not the
/// error's `Display`, which quotes the offending line, nor its
/// `Debug`, which includes all of `contents`.
pub(crate) fn redact_toml_error(
  e: &toml::de::Error,
  contents: &str,
) -> String {
  let message = redact_message(e.message().trim_end(), false);
  let Some((line, column)) =
    e.span().and_then(|span| line_column(contents, span.start))
  else {
    return message;
  };
  format!("{message} at line {line} column {column}")
}

/// The yaml error message (with its line and column), redacted like
/// [redact_serde_error].
pub(crate) fn redact_yaml_error(e: &serde_yaml_ng::Error) -> String {
  redact_message(&e.to_string(), true)
}

/// The 1 based line and column (in chars) of a byte offset.
fn line_column(
  contents: &str,
  offset: usize,
) -> Option<(usize, usize)> {
  let before = contents.get(..offset)?;
  let line = before.matches('\n').count() + 1;
  let column = before
    .rsplit('\n')
    .next()
    .map_or(0, |line| line.chars().count())
    + 1;
  Some((line, column))
}

/// Deserialize the merged config into the final type, mapping
/// failures to [Error::ParseFinalJson] with the path of the
/// offending field and the type found there, never its value.
/// String values coerce into the requested type the way `envy`
/// reads the process environment (numbers, booleans, comma
/// separated lists, `Option`, unit enum variants), see
/// [crate::lenient].
pub fn deserialize_final<T: DeserializeOwned>(
  value: &serde_json::Value,
) -> crate::Result<T> {
  serde_path_to_error::deserialize(crate::lenient::Lenient(
    value.clone(),
  ))
  .map_err(|e| {
    let path = e.path();
    let found = value_at(value, path).map(value_type);
    Error::ParseFinalJson {
      path: path.to_string(),
      found,
      message: redact_serde_error(e.inner()),
    }
  })
}

/// The value at a serde error path, if the path resolves.
fn value_at<'a>(
  mut value: &'a serde_json::Value,
  path: &serde_path_to_error::Path,
) -> Option<&'a serde_json::Value> {
  use serde_path_to_error::Segment;
  for segment in path.iter() {
    value = match segment {
      Segment::Map { key } | Segment::Enum { variant: key } => {
        value.get(key)?
      }
      // A sequence split out of a string (see [crate::lenient]):
      // the string is what was found there.
      Segment::Seq { .. } if value.is_string() => return Some(value),
      Segment::Seq { index } => value.get(index)?,
      Segment::Unknown => return None,
    };
  }
  Some(value)
}

/// Config errors never include config values,
/// which may be secrets, only keys and types.
#[derive(Debug, thiserror::Error)]
pub enum Error {
  #[error(
    "Types on field {key} do not match | got {found}, expected object"
  )]
  ObjectFieldTypeMismatch {
    key: String,
    /// The json type name of the value found.
    found: &'static str,
  },

  #[error(
    "Types on field {key} do not match | got {found}, expected array"
  )]
  ArrayFieldTypeMismatch {
    key: String,
    /// The json type name of the value found.
    found: &'static str,
  },

  #[error("Failed to open file at {path} | {e:?}")]
  FileOpen { e: std::io::Error, path: PathBuf },

  #[error("Failed to read contents of file at {path} | {e:?}")]
  ReadFileContents { e: std::io::Error, path: PathBuf },

  /// The parser's error is not kept: it quotes the file.
  #[error("Failed to parse toml file at {path} | {message}")]
  ParseToml {
    path: PathBuf,
    /// The parser's message with values redacted
    /// ([redact_serde_error]), and the line and column.
    message: String,
  },

  /// The parser's error is not kept, see [Error::ParseToml].
  #[error("Failed to parse yaml file at {path} | {message}")]
  ParseYaml {
    path: PathBuf,
    /// The parser's message with values redacted
    /// ([redact_serde_error]), and the line and column.
    message: String,
  },

  /// The parser's error is not kept, see [Error::ParseToml].
  #[error("Failed to parse json file at {path} | {message}")]
  ParseJson {
    path: PathBuf,
    /// The parser's message with values redacted
    /// ([redact_serde_error]), and the line and column.
    message: String,
  },

  /// See [crate::parse_env_file]; the message names the line and
  /// what was expected, never a value.
  #[error("Failed to parse env file at {path} | {e}")]
  ParseEnvFile {
    e: crate::env_file::EnvFileError,
    path: PathBuf,
  },

  #[error("Unsupported file type at {path}")]
  UnsupportedFileType { path: PathBuf },

  /// See [deserialize_final]. The message has values redacted
  /// ([redact_serde_error]); `found` is the json type at `path`.
  #[error(
    "Failed to parse merged config into final type at '{path}' | found {} | {message}",
    found.unwrap_or("nothing")
  )]
  ParseFinalJson {
    /// Dot separated path to the offending field, `.` for the root.
    path: String,
    /// The json type name found at `path`, if it resolves.
    found: Option<&'static str>,
    message: String,
  },

  #[error("Failed to serialize config to json string | {e:?}")]
  SerializeJson { e: serde_json::Error },

  #[error("Failed to read directory at {path:?}")]
  ReadDir { path: PathBuf, e: std::io::Error },

  #[error("Failed to get file handle for file in directory {path:?}")]
  DirFile { e: std::io::Error, path: PathBuf },

  #[error("Failed to get file name for file at {path:?}")]
  GetFileName { path: PathBuf },

  #[error("Failed to get metadata for path {path:?} | {e:?}")]
  ReadPathMetaData { path: PathBuf, e: std::io::Error },

  #[error("Parsed value is not object")]
  ValueIsNotObject,

  /// A [crate::ConfigLoader::match_wildcards] pattern which doesn't
  /// compile. An error rather than a dropped pattern: dropping it
  /// widens the filter (with none left, a directory scan loads every
  /// file in it).
  #[error("Config wildcard '{pattern}' is invalid | {message}")]
  InvalidWildcard { pattern: String, message: String },

  /// A `cicada:` config path (listed in the paths, or in an include
  /// file), without the `cicada` feature to load it. An error rather
  /// than a skipped path: the app would start with defaults where
  /// the operator expects their configuration.
  #[error(
    "Config path {path:?} is a cicada path, which needs the 'cicada' feature of mogh_config to be enabled"
  )]
  CicadaFeatureDisabled { path: PathBuf },

  /// A `cicada:` config path (listed in the paths, or in an include
  /// file) which failed to load from Cicada (Core unreachable, the
  /// device not onboarded or not granted the environments, the file
  /// missing, ...). An error rather than a skipped path, like
  /// [Error::CicadaFeatureDisabled].
  #[error("Failed to load cicada config at {path:?} | {message}")]
  CicadaLoad {
    /// The path as listed, `cicada://...`.
    path: PathBuf,
    /// The loader's error chain.
    message: String,
  },
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn redacts_quoted_values_from_serde_errors() {
    let err = serde_json::from_value::<u16>(serde_json::json!(
      "hunter2 \"quoted\" secret"
    ))
    .unwrap_err();
    let message = redact_serde_error(&err);
    assert!(!message.contains("hunter2"), "{message}");
    assert!(
      message.contains("invalid type: string [redacted]"),
      "{message}"
    );
    assert!(message.contains("expected u16"));
    // Numbers are formatted in backticks by serde.
    let err =
      serde_json::from_value::<String>(serde_json::json!(482913))
        .unwrap_err();
    let message = redact_serde_error(&err);
    assert!(!message.contains("482913"), "{message}");
    // A character is written in backticks unescaped.
    let err = <serde_json::Error as serde::de::Error>::invalid_type(
      serde::de::Unexpected::Char('`'),
      &"a secret",
    );
    let message = redact_serde_error(&err);
    assert_eq!(
      message,
      "invalid type: character [redacted], expected a secret"
    );
  }

  /// serde writes an unknown enum variant (or field name) in
  /// backticks without escaping it: a backtick in the value used to
  /// end the redacted token early, leaking the rest.
  #[test]
  fn redacts_unknown_variants_holding_backticks_and_quotes() {
    #[derive(serde::Deserialize, Debug)]
    #[serde(rename_all = "lowercase")]
    #[allow(dead_code)]
    enum Mode {
      Fast,
      Safe,
    }
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Config {
      mode: Mode,
    }
    #[derive(serde::Deserialize, Debug)]
    #[serde(deny_unknown_fields)]
    #[allow(dead_code)]
    struct Strict {
      port: u16,
    }
    for value in [
      "x`hunter2secret",
      "a`b`c`hunter2`d",
      "`hunter2`",
      "x\"hunter2\"",
      "x`, expected `hunter2",
      "x`, there are no hunter2",
      "multi\nline`hunter2",
    ] {
      let err = deserialize_final::<Config>(
        &serde_json::json!({ "mode": value }),
      )
      .unwrap_err();
      for message in [err.to_string(), format!("{err:?}")] {
        assert!(!message.contains("hunter2"), "{value:?}: {message}");
      }
      let message = err.to_string();
      assert!(
        message.ends_with(
          "unknown variant [redacted], expected `fast` or `safe`"
        ),
        "{value:?}: {message}"
      );
      // Parse errors too.
      for (file, contents) in [
        ("a.yaml", format!("mode: {}", serde_json::json!(value))),
        ("a.toml", format!("mode = {}", serde_json::json!(value))),
        (
          "a.json",
          format!("{{\"mode\": {}}}", serde_json::json!(value)),
        ),
      ] {
        let err = crate::load::parse_config_contents::<Config>(
          std::path::Path::new(file),
          &contents,
        )
        .unwrap_err();
        for message in [err.to_string(), format!("{err:?}")] {
          assert!(
            !message.contains("hunter2"),
            "{file} {value:?}: {message}"
          );
          assert!(
            message.contains(
              "unknown variant [redacted], expected `fast` or `safe`"
            ),
            "{file} {value:?}: {message}"
          );
        }
      }

      // A key is named by the path, but not by serde's message.
      let err = deserialize_final::<Strict>(
        &serde_json::json!({ "port": 1, value: 1 }),
      )
      .unwrap_err();
      let Error::ParseFinalJson { message, .. } = err else {
        panic!("{err:?}");
      };
      assert_eq!(
        message, "unknown field [redacted], expected `port`",
        "{value:?}"
      );
    }
  }

  /// Text like serde's unknown variant message inside a string
  /// value is part of the value: the string is redacted whole, not
  /// taken for serde's message, which would keep what follows it.
  #[test]
  fn redacts_strings_holding_unknown_variant_text() {
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Typed {
      port: u16,
    }
    for value in [
      "unknown variant `a`, expected hunter2",
      "x unknown variant `a`, expected `hunter2` or",
      "unknown field `a`, there are no hunter2",
      "x: unknown variant `a`, expected hunter2",
      "hunter2: unknown variant `a`, expected `b`",
    ] {
      let err = deserialize_final::<Typed>(
        &serde_json::json!({ "port": value }),
      )
      .unwrap_err();
      let Error::ParseFinalJson { message, .. } = &err else {
        panic!("{err:?}");
      };
      assert_eq!(
        message, "invalid value: string [redacted], expected u16",
        "{value:?}"
      );
      for (file, contents) in [
        (
          "a.json",
          format!("{{\"port\": {}}}", serde_json::json!(value)),
        ),
        ("a.toml", format!("port = {}", serde_json::json!(value))),
        ("a.yaml", format!("port: {}", serde_json::json!(value))),
        ("a.yaml", format!("{}", serde_json::json!(value))),
      ] {
        let err = crate::load::parse_config_contents::<Typed>(
          std::path::Path::new(file),
          &contents,
        )
        .unwrap_err();
        for message in [err.to_string(), format!("{err:?}")] {
          assert!(
            !message.contains("hunter2"),
            "{file} {value:?}: {message}"
          );
        }
        if file == "a.json" || contents.starts_with("port") {
          assert!(
            err.to_string().contains("[redacted], expected u16"),
            "{file} {value:?}: {err}"
          );
        }
      }
    }
  }

  /// yaml names the key of the failing value before serde's message:
  /// a key holding a quote or a backtick can't be told from the
  /// value, so only the text before it is kept.
  #[test]
  fn redacts_yaml_errors_under_keys_holding_quotes() {
    #[derive(serde::Deserialize, Debug)]
    #[serde(rename_all = "lowercase")]
    #[allow(dead_code)]
    enum Mode {
      Fast,
      Safe,
    }
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Typed {
      modes: std::collections::HashMap<String, Mode>,
      ports: std::collections::HashMap<String, u16>,
    }
    let parse = |contents: &str| {
      crate::load::parse_config_contents::<Typed>(
        std::path::Path::new("a.yaml"),
        contents,
      )
      .unwrap_err()
    };
    for key in ["a`b", "a\"b", "a`b\"c", "a: b`c", "a: b\"c"] {
      let key = serde_json::json!(key);
      for value in ["x`hunter2", "hunter2", "x\"hunter2"] {
        let value = serde_json::json!(value);
        let err =
          parse(&format!("ports: {{}}\nmodes: {{ {key}: {value} }}"));
        let message = err.to_string();
        assert!(
          !message.contains("hunter2"),
          "{key} {value}: {message}"
        );
        assert!(
          message.contains("at line 2"),
          "{key} {value}: {message}"
        );
      }
      let err =
        parse(&format!("modes: {{}}\nports: {{ {key}: hunter2 }}"));
      let message = err.to_string();
      assert!(!message.contains("hunter2"), "{key}: {message}");
      assert!(message.contains("at line 2"), "{key}: {message}");
    }
    // A plain key stays readable, and so does serde's message.
    let err = parse("ports: {}\nmodes: { a.b: x`hunter2 }");
    assert!(
      err.to_string().ends_with(
        "modes.a.b: unknown variant [redacted], expected `fast` or `safe` at line 2 column 15"
      ),
      "{err}"
    );
    let err = parse("modes: {}\nports: { a.b: hunter2 }");
    assert!(
      err.to_string().ends_with(
        "ports.a.b: invalid type: string [redacted], expected u16 at line 2 column 15"
      ),
      "{err}"
    );
  }

  /// Parse errors are returned to the app, which logs them: a
  /// `cicada:` file carries the secrets Core interpolated into it.
  #[test]
  fn parse_errors_redact_values_and_name_the_line() {
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Typed {
      port: u16,
    }
    let assert_redacted = |err: Error, location: Option<&str>| {
      for message in [err.to_string(), format!("{err:?}")] {
        assert!(!message.contains("hunter2"), "{message}");
        if let Some(location) = location {
          assert!(message.contains(location), "{message}");
        }
      }
    };
    let parse = |file: &str, contents: &str| {
      crate::load::parse_config_contents::<serde_json::Value>(
        std::path::Path::new(file),
        contents,
      )
      .unwrap_err()
    };
    let parse_typed = |file: &str, contents: &str| {
      crate::load::parse_config_contents::<Typed>(
        std::path::Path::new(file),
        contents,
      )
      .unwrap_err()
    };

    let err = parse("a.toml", "secret = \"hunter2\"\n[[broken");
    assert!(matches!(err, Error::ParseToml { .. }), "{err:?}");
    assert_redacted(err, Some("at line 2 column"));
    // The column counts chars, not bytes.
    let err = parse("a.toml", "k = \"é\" hunter2");
    assert_redacted(err, Some("at line 1 column 9"));
    let err = parse_typed("a.toml", "port = \"hunter2\"");
    assert_redacted(err, Some("expected u16"));

    let err = parse("a.yaml", "secret: \"hunter2\"\nbroken: [");
    assert!(matches!(err, Error::ParseYaml { .. }), "{err:?}");
    assert_redacted(err, Some("at line"));
    let err = parse_typed("a.yaml", "port: hunter2");
    assert_redacted(err, Some("expected u16"));

    let err = parse("a.json", "{\"secret\": \"hunter2\",");
    assert!(matches!(err, Error::ParseJson { .. }), "{err:?}");
    assert_redacted(err, Some("at line 1 column"));
    let err = parse_typed("a.json", "{\"port\": \"hunter2\"}");
    assert_redacted(err, Some("expected u16"));

    let err = parse_typed(".env", "PORT=hunter2");
    assert!(matches!(err, Error::ParseJson { .. }), "{err:?}");
    assert_redacted(err, Some("expected u16"));
  }

  #[test]
  fn deserialize_final_reports_path_and_type_not_value() {
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Server {
      port: u16,
    }
    #[derive(serde::Deserialize, Debug)]
    #[allow(dead_code)]
    struct Config {
      server: Server,
      pins: Vec<u8>,
    }
    let err = deserialize_final::<Config>(&serde_json::json!({
      "server": { "port": "hunter2secret" },
      "pins": [1, 2],
    }))
    .unwrap_err();
    let message = err.to_string();
    assert!(message.contains("at 'server.port'"), "{message}");
    assert!(message.contains("found string"), "{message}");
    assert!(message.contains("expected u16"), "{message}");
    assert!(!message.contains("hunter2secret"), "{message}");

    let err = deserialize_final::<Config>(&serde_json::json!({
      "server": { "port": 1 },
      "pins": [1, 70000],
    }))
    .unwrap_err();
    let message = err.to_string();
    assert!(message.contains("at 'pins[1]'"), "{message}");
    assert!(message.contains("found number"), "{message}");
    assert!(!message.contains("70000"), "{message}");
  }

  #[test]
  fn mismatch_errors_name_key_and_type_only() {
    let err = Error::ObjectFieldTypeMismatch {
      key: "field".into(),
      found: value_type(&serde_json::json!("secret")),
    };
    assert_eq!(
      err.to_string(),
      "Types on field field do not match | got string, expected object"
    );
    assert_eq!(value_type(&serde_json::json!(null)), "null");
    assert_eq!(value_type(&serde_json::json!([1])), "array");
  }
}