martin 1.13.0

Blazing fast and lightweight tile server with PostGIS, MBTiles, and PMTiles support
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::Path;

use tracing::warn;

use super::Config;
use crate::config::file::{ConfigFileError, ConfigFileResult};
use crate::config::primitives::env::Env;

/// Read config from a file
pub fn read_config(file_name: &Path, env: &impl Env) -> ConfigFileResult<Config> {
    let contents = std::fs::read_to_string(file_name)
        .map_err(|e| ConfigFileError::ConfigLoadError(e, file_name.into()))?;
    #[cfg(feature = "postgres")]
    warn_unused_pg_env_vars(env, &contents);
    parse_config(&contents, &env.as_property_map(), file_name)
}

#[cfg(feature = "postgres")]
fn warn_unused_pg_env_vars(env: &impl Env, contents: &str) {
    let set_vars: Vec<&str> = [
        "DATABASE_URL",
        "DEFAULT_SRID",
        "PGSSLCERT",
        "PGSSLKEY",
        "PGSSLROOTCERT",
    ]
    .into_iter()
    .filter(|v| env.var_os(v).is_some())
    .collect();
    if set_vars.is_empty() {
        return;
    }
    let Ok(value) = serde_saphyr::from_str::<serde_json::Value>(contents) else {
        return;
    };
    for v in set_vars {
        if !json_value_references_var(&value, v) {
            warn!(
                "Environment variable {v} is set, but will be ignored because a configuration file was loaded. Any environment variables can be used inside the config yaml file."
            );
        }
    }
}

#[cfg(feature = "postgres")]
fn json_value_references_var(value: &serde_json::Value, name: &str) -> bool {
    use serde_json::Value::{Array, Bool, Null, Number, Object, String};
    match value {
        String(s) => string_references_var(s, name),
        Array(items) => items.iter().any(|v| json_value_references_var(v, name)),
        Object(map) => map
            .iter()
            .any(|(k, v)| string_references_var(k, name) || json_value_references_var(v, name)),
        Null | Bool(_) | Number(_) => false,
    }
}

/// Whether `haystack` contains a saphyr-style substitution token referencing `name`:
/// `${name}`, `${name<op>...}` for `<op>` in `:-/-/:+/+/:?/?`, or bare `$name` not
/// followed by an identifier-continuation char. `$$` is treated as an escape, not a `$`.
#[cfg(feature = "postgres")]
fn string_references_var(haystack: &str, name: &str) -> bool {
    let mut rest = haystack;
    while let Some((_, after)) = rest.split_once('$') {
        if let Some(escaped) = after.strip_prefix('$') {
            rest = escaped;
            continue;
        }
        let (body, in_braces) = after
            .strip_prefix('{')
            .map_or((after, false), |b| (b, true));
        if let Some(tail) = body.strip_prefix(name) {
            let next = tail.bytes().next();
            let matched = if in_braces {
                matches!(next, Some(b'}' | b':' | b'-' | b'+' | b'?'))
            } else {
                next.is_none_or(|c| !(c.is_ascii_alphanumeric() || c == b'_'))
            };
            if matched {
                return true;
            }
        }
        rest = after;
    }
    false
}

pub fn parse_config(
    contents: &str,
    properties: &HashMap<String, String>,
    file_name: &Path,
) -> ConfigFileResult<Config> {
    let contents = rewrite_legacy_substitution_syntax(contents);
    let migrated = if needs_deprecated_migration(&contents) {
        match serde_saphyr::from_str::<serde_json::Value>(&contents) {
            Ok(mut value) => {
                migrate_deprecated_config(&mut value);
                serde_saphyr::to_string(&value).unwrap_or_else(|_| contents.to_string())
            }
            Err(_) => contents.to_string(),
        }
    } else {
        contents.to_string()
    };

    // `with_snippet: false` keeps saphyr's hardcoded `<input>` source name out of the
    // diagnostic -- `ConfigFileError::to_miette_report` re-attaches a snippet against
    // our own NamedSource carrying the real file path.
    let options = serde_saphyr::options! {
        with_snippet: false,
        property_syntax: serde_saphyr::options::PropertySyntax::BracedOrBare,
    }
    .with_properties(properties.clone());

    serde_saphyr::from_str_with_options::<Config>(&migrated, options)
        .map_err(|e| ConfigFileError::yaml_parse(e, migrated, file_name))
}

/// Rewrites the legacy single-colon default `${VAR:default}` to serde-saphyr's `${VAR:-default}`,
/// warning once when anything changes.
///
/// Martin <= 1.10 used the `subst` crate's single-colon syntax; serde-saphyr rejects it outright,
/// so without this those configs fail to load. Substitution inside quoted scalars and `subst`'s
/// backslash escaping are not restored -- both are unrelated to the single-colon default.
fn rewrite_legacy_substitution_syntax(contents: &str) -> Cow<'_, str> {
    static WARNED: std::sync::Once = std::sync::Once::new();

    if !contents.contains("${") {
        return Cow::Borrowed(contents);
    }

    // One segment per reference body, so nested `${a:x${b:y}}` splits into its own body.
    let mut segments = contents.split("${");
    let first = segments.next().unwrap_or("");
    let mut out = String::with_capacity(contents.len() + 8);
    out.push_str(first);

    let mut prev = first;
    let mut rewrites = 0usize;
    for body in segments {
        out.push_str("${");
        // A `${` after an odd run of `$` is escaped (`$$` -> literal `$`), not a reference.
        let escaped = prev.bytes().rev().take_while(|&b| b == b'$').count() % 2 == 1;
        match rewrite_reference_body(body).filter(|_| !escaped) {
            Some(rewritten) => {
                out.push_str(&rewritten);
                rewrites += 1;
            }
            None => out.push_str(body),
        }
        prev = body;
    }

    if rewrites == 0 {
        return Cow::Borrowed(contents);
    }

    WARNED.call_once(|| {
        warn!(
            deprecated_tokens = rewrites,
            "Deprecated `${{VAR:default}}` substitution syntax in config; use `${{VAR:-default}}`. Support for the single-colon form will be removed in a future release."
        );
    });
    Cow::Owned(out)
}

/// Rewrites the operator colon (the first `:` before the closing `}`) of one `${...}` body from
/// `:default` to `:-default`, returning `None` when it is already an operator or absent.
fn rewrite_reference_body(body: &str) -> Option<String> {
    let end = body.find('}').unwrap_or(body.len());
    let (name, default) = body.get(..end)?.split_once(':')?;
    if default.starts_with(['-', '+', '?']) {
        return None;
    }
    let rest = body.get(end..).unwrap_or_default();
    Some(format!("{name}:-{default}{rest}"))
}

/// Cheap pre-check: does the YAML mention any deprecated cache key?
///
/// False positives are harmless (the fast path is identical to the slow path's no-op
/// migration), so a substring search is sufficient.
fn needs_deprecated_migration(yaml: &str) -> bool {
    yaml.contains("cache_size_mb")
        || yaml.contains("tile_cache_size_mb")
        || yaml.contains("directory_cache_size_mb")
}

/// Migrates deprecated cache configuration keys in raw YAML before deserialization.
///
/// This runs on the `serde_json::Value` directly, so the `Config` struct
/// never needs to know about deprecated field names.
fn migrate_deprecated_config(value: &mut serde_json::Value) {
    let Some(root) = value.as_object_mut() else {
        return;
    };

    migrate_json_key(root, "cache_size_mb", &["cache", "size_mb"]);
    migrate_json_key(root, "tile_cache_size_mb", &["cache", "tile_size_mb"]);

    for section in ["sprites", "fonts"] {
        if let Some(mapping) = root.get_mut(section).and_then(|v| v.as_object_mut()) {
            migrate_json_key(mapping, "cache_size_mb", &["cache", "size_mb"]);
        }
    }

    if let Some(mapping) = root.get_mut("pmtiles").and_then(|v| v.as_object_mut()) {
        migrate_json_key(
            mapping,
            "directory_cache_size_mb",
            &["directory_cache", "size_mb"],
        );
    }
}

/// Moves a deprecated key in a JSON map to a new nested location.
///
/// `new_path` is a slice of keys describing the nested destination,
/// e.g. `&["cache", "size_mb"]` means `cache.size_mb`.
///
/// If the new key already exists, the old value is dropped with a warning.
/// If only the old key exists, it is moved to the new location.
fn migrate_json_key(
    mapping: &mut serde_json::Map<String, serde_json::Value>,
    old_key: &str,
    new_path: &[&str],
) {
    debug_assert!(!new_path.is_empty(), "new_path must not be empty");

    let Some(old_value) = mapping.remove(old_key) else {
        return;
    };

    let new_key_display = new_path.join(".");

    let [parents @ .., leaf] = new_path else {
        return;
    };
    let mut current = &mut *mapping;
    for &segment in parents {
        if !current.contains_key(segment) {
            current.insert(
                segment.to_string(),
                serde_json::Value::Object(serde_json::Map::default()),
            );
        }
        let Some(nested) = current.get_mut(segment).and_then(|v| v.as_object_mut()) else {
            warn!(
                "deprecated config: `{old_key}` is ignored because `{segment}` is already set. \
                 Please remove `{old_key}` from your configuration"
            );
            return;
        };
        current = nested;
    }

    if current.contains_key(*leaf) {
        warn!(
            "deprecated config: `{old_key}` is ignored in favor of `{new_key_display}`. \
             Please remove `{old_key}` from your configuration"
        );
    } else {
        current.insert((*leaf).to_string(), old_value);
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::Path;
    use std::time::Duration;

    use rstest::rstest;

    use super::*;
    #[cfg(any(feature = "sprites", feature = "fonts"))]
    use crate::config::file::FileConfigEnum;
    use crate::config::file::{CachePolicy, Config, GlobalCacheConfig};
    #[cfg(feature = "postgres")]
    use crate::config::primitives::OptOneMany;
    use crate::config::test_helpers::{render_failure, render_failure_json};

    fn parse_yaml(yaml: &str) -> Config {
        parse_config(
            yaml,
            &HashMap::<String, String>::new(),
            Path::new("test.yaml"),
        )
        .unwrap()
    }

    fn props(pairs: &[(&str, &str)]) -> HashMap<String, String> {
        pairs
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect()
    }

    fn parse_with_env(yaml: &str, env: &HashMap<String, String>) -> Config {
        parse_config(yaml, env, Path::new("test.yaml")).unwrap()
    }

    // ----- `parse_config` pipeline diagnostics: failures that don't belong to a single
    // ----- field's deserializer (raw YAML syntax, ${VAR} substitution, derive-`Deserialize`
    // ----- enums) live here next to the function under test.

    #[test]
    fn syntax_error_unbalanced_quote() {
        insta::assert_snapshot!(
            render_failure(indoc::indoc! {r#"
                srv:
                  listen_addresses: "0.0.0.0:3000
                  worker_processes: 4
            "#}),
            @r#"
        martin::config::yaml (https://maplibre.org/martin/config-file/)

          × invalid indentation in multiline quoted scalar
           ╭─[config.yaml:3:3]
         2 │   listen_addresses: "0.0.0.0:3000
         3 │   worker_processes: 4
           ·   ┬
           ·   ╰── invalid indentation in multiline quoted scalar
           ╰────
          help: Check the highlighted token in your YAML. The error usually indicates
                a mismatched type or an unexpected shape.
        "#
        );
    }

    #[test]
    fn unknown_enum_variant_in_on_invalid() {
        insta::assert_snapshot!(render_failure("on_invalid: maybe\n"), @"
        martin::config::yaml (https://maplibre.org/martin/config-file/)

          × unknown variant `maybe`, expected one of continue, ignore, warn, warning,
          │ warnings, abort
           ╭─[config.yaml:1:13]
         1 │ on_invalid: maybe
           ·             ──┬──
           ·               ╰── unknown variant `maybe`, expected one of continue, ignore, warn, warning, warnings, abort
           ╰────
          help: Check the highlighted token in your YAML. The error usually indicates
                a mismatched type or an unexpected shape.
        ");
    }

    #[test]
    fn substitution_undefined_variable() {
        insta::assert_snapshot!(render_failure("cache_size_mb: ${UNDEFINED_VAR}\n"), @"
        martin::config::substitution (https://maplibre.org/martin/config-file/)

          × missing property `UNDEFINED_VAR`
           ╭─[config.yaml:2:12]
         1 │ cache:
         2 │   size_mb: ${UNDEFINED_VAR}
           ·            ────────┬───────
           ·                    ╰── missing property `UNDEFINED_VAR`
           ╰────
          help: Make sure every ${VAR} reference resolves to an environment variable,
                or supply a default with `${VAR:-fallback}`.
        ");
    }

    #[test]
    fn cors_unsupported_scalar_renders_as_json() {
        // Mirrors what the binary emits when `RUST_LOG_FORMAT=json` is set: a structured
        // JSON document instead of the graphical snippet, suitable for editor tooling and
        // log aggregators.
        let json = render_failure_json("cors: 42\n");
        let parsed: serde_json::Value =
            serde_json::from_str(&json).unwrap_or_else(|e| panic!("not JSON: {e}\n{json}"));

        let message = parsed.get("message").and_then(|m| m.as_str()).unwrap_or("");
        assert!(
            message.contains("invalid type: integer `42`"),
            "unexpected message in JSON output: {message}"
        );
        assert_eq!(
            parsed.get("severity").and_then(|s| s.as_str()),
            Some("error")
        );
        assert_eq!(
            parsed.get("filename").and_then(|f| f.as_str()),
            Some("config.yaml")
        );
        let labels = parsed.get("labels").and_then(|l| l.as_array()).unwrap();
        assert_eq!(labels.len(), 1, "expected one label, got {labels:?}");
        let span = labels[0].get("span").unwrap();
        assert!(span.get("offset").is_some(), "label missing offset");
        assert!(span.get("length").is_some(), "label missing length");
    }

    #[test]
    fn substitution_failure_renders_as_json() {
        let json = render_failure_json("cache_size_mb: ${UNDEFINED_VAR}\n");
        let parsed: serde_json::Value =
            serde_json::from_str(&json).unwrap_or_else(|e| panic!("not JSON: {e}\n{json}"));

        let message = parsed.get("message").and_then(|m| m.as_str()).unwrap_or("");
        assert!(
            message.contains("missing property `UNDEFINED_VAR`"),
            "unexpected message in JSON output: {message}"
        );
        assert_eq!(
            parsed.get("filename").and_then(|f| f.as_str()),
            Some("config.yaml")
        );
    }

    #[test]
    fn cache_migrates_old_to_new_cache_config_key() {
        let config = parse_yaml("cache_size_mb: 512");
        assert_eq!(config.cache.size_mb, Some(512));
    }

    #[test]
    fn migrate_tile_cache_size_mb_to_cache_tile_size_mb() {
        let config = parse_yaml("tile_cache_size_mb: 256");
        assert_eq!(config.cache.tile_size_mb, Some(256));
    }

    #[test]
    fn migrate_both_old_cache_keys() {
        let config = parse_yaml("cache_size_mb: 512\ntile_cache_size_mb: 256");
        assert_eq!(config.cache.size_mb, Some(512));
        assert_eq!(config.cache.tile_size_mb, Some(256));
    }

    #[test]
    fn new_cache_key_overrides_old() {
        let config = parse_yaml("cache_size_mb: 100\ncache:\n  size_mb: 200");
        assert_eq!(config.cache.size_mb, Some(200));
    }

    #[test]
    fn new_cache_format_works_directly() {
        let config =
            parse_yaml("cache:\n  size_mb: 512\n  tile_size_mb: 256\n  minzoom: 2\n  maxzoom: 10");
        assert_eq!(config.cache.size_mb, Some(512));
        assert_eq!(config.cache.tile_size_mb, Some(256));
    }

    #[cfg(feature = "sprites")]
    #[test]
    fn migrate_sprites_cache_size_mb() {
        let config = parse_yaml("sprites:\n  cache_size_mb: 64\n  paths: /tmp");
        let FileConfigEnum::Config(cfg) = &config.sprites else {
            panic!("expected sprites config");
        };
        assert_eq!(cfg.custom.cache.size_mb, Some(64));
    }

    #[cfg(feature = "fonts")]
    #[test]
    fn migrate_fonts_cache_size_mb() {
        let config = parse_yaml("fonts:\n  cache_size_mb: 32\n  paths: /tmp");
        let FileConfigEnum::Config(cfg) = &config.fonts else {
            panic!("expected fonts config");
        };
        assert_eq!(cfg.custom.cache.size_mb, Some(32));
    }

    #[test]
    fn migrate_skips_non_mapping_intermediate() {
        // `cache: true` is not a mapping, so migration of cache_size_mb should
        // gracefully skip rather than panic, and the parse should still succeed
        // (cache will be deserialized from whatever value it has).
        let result = parse_config(
            "cache: true\ncache_size_mb: 100",
            &HashMap::<String, String>::new(),
            Path::new("test.yaml"),
        );
        // The parse may fail (cache: true is not a valid GlobalCacheConfig),
        // but it must not panic.
        let _ = result;
    }

    #[test]
    fn cache_disable_global() {
        let config = parse_yaml("cache: disable");
        assert_eq!(config.cache, GlobalCacheConfig::disabled());
        assert_eq!(config.cache.size_mb, Some(0));
        assert_eq!(config.cache.tile_size_mb, Some(0));
    }

    #[test]
    fn cache_disable_global_propagates_to_unconfigured_source() {
        let config = parse_yaml("cache: disable");
        let global_policy = config.cache.policy();
        let unconfigured_source = CachePolicy::default();
        let merged = unconfigured_source.or(global_policy);
        for zoom in 0..=u8::MAX {
            assert!(!merged.zoom().contains(zoom));
        }
    }

    #[cfg(feature = "sprites")]
    #[test]
    fn cache_disable_sprites() {
        let config = parse_yaml("sprites:\n  cache: disable\n  paths: /tmp");
        let FileConfigEnum::Config(cfg) = &config.sprites else {
            panic!("expected sprites config");
        };
        assert_eq!(cfg.custom.cache.size_mb, Some(0));
    }

    #[test]
    fn cache_expiry_global_config() {
        let config = parse_yaml("cache:\n  size_mb: 512\n  expiry: 1h\n  idle_timeout: 15m");
        assert_eq!(config.cache.size_mb, Some(512));
        assert_eq!(config.cache.expiry, Some(Duration::from_hours(1)));
        assert_eq!(config.cache.idle_timeout, Some(Duration::from_mins(15)));
    }

    #[test]
    fn cache_expiry_tile_specific() {
        let config = parse_yaml(
            "cache:\n  expiry: 1h\n  idle_timeout: 15m\n  tile_expiry: 30m\n  tile_idle_timeout: 5m",
        );
        assert_eq!(config.cache.expiry, Some(Duration::from_hours(1)));
        assert_eq!(config.cache.tile_expiry, Some(Duration::from_mins(30)));
        assert_eq!(config.cache.tile_idle_timeout, Some(Duration::from_mins(5)));
    }

    #[test]
    fn cache_expiry_none_when_unset() {
        let config = parse_yaml("cache:\n  size_mb: 512");
        assert_eq!(config.cache.expiry, None);
        assert_eq!(config.cache.idle_timeout, None);
        assert_eq!(config.cache.tile_expiry, None);
        assert_eq!(config.cache.tile_idle_timeout, None);
    }

    #[cfg(feature = "sprites")]
    #[test]
    fn cache_expiry_sprites() {
        let config = parse_yaml(
            "sprites:\n  cache:\n    size_mb: 64\n    expiry: 2h\n    idle_timeout: 30m\n  paths: /tmp",
        );
        let FileConfigEnum::Config(cfg) = &config.sprites else {
            panic!("expected sprites config");
        };
        assert_eq!(cfg.custom.cache.size_mb, Some(64));
        assert_eq!(cfg.custom.cache.expiry, Some(Duration::from_hours(2)));
        assert_eq!(cfg.custom.cache.idle_timeout, Some(Duration::from_mins(30)));
    }

    #[rstest]
    #[case::braced("${BASE}", "/my/path")]
    #[case::bare("$BASE", "/my/path")]
    #[case::dash_default_var_present("${BASE:-fallback}", "/my/path")]
    #[case::dash_default_var_unset("${UNSET:-/fallback}", "/fallback")]
    #[case::prefix_and_suffix("prefix-${BASE}-suffix", "prefix-/my/path-suffix")]
    #[case::escape_double_dollar("$$BASE", "$BASE")]
    fn substitution_accepted_forms(#[case] input: &str, #[case] expected: &str) {
        let env = props(&[("BASE", "/my/path")]);
        let yaml = format!("base_path: {input}\n");
        let config = parse_with_env(&yaml, &env);
        assert_eq!(config.srv.base_path.as_deref(), Some(expected));
    }

    #[cfg(feature = "postgres")]
    #[rstest]
    #[case::dash_default("${UNSET:-fallback}", Some("fallback"))]
    #[case::dash_default_unset_only("${UNSET-fallback}", Some("fallback"))]
    // an alternate that expands to nothing leaves an empty plain scalar, i.e. YAML null
    #[case::plus_alternate_unset("${UNSET:+set}", None)]
    #[case::plus_alternate_set("${BASE:+set}", Some("set"))]
    fn substitution_shell_operators(#[case] input: &str, #[case] expected: Option<&str>) {
        let env = props(&[("BASE", "/my/path")]);
        let yaml = format!("postgres:\n  connection_string: {input}\n");
        let config = parse_with_env(&yaml, &env);
        let pg = match config.postgres {
            OptOneMany::One(pg) => pg,
            other => panic!("expected exactly one postgres config, got: {other:?}"),
        };
        assert_eq!(pg.connection_string.as_deref(), expected);
    }

    #[rstest]
    #[case::var_set("${BASE:fallback}", "/my/path")]
    #[case::var_unset("${UNSET:fallback}", "fallback")]
    fn substitution_single_colon_default_is_translated(
        #[case] input: &str,
        #[case] expected: &str,
    ) {
        let env = props(&[("BASE", "/my/path")]);
        let config = parse_with_env(&format!("base_path: {input}\n"), &env);
        assert_eq!(config.srv.base_path.as_deref(), Some(expected));
    }

    #[rstest]
    #[case::braced("base_path: ${BASE}\n")]
    #[case::bare("base_path: $BASE\n")]
    #[case::dash_default("base_path: ${BASE:-fallback}\n")]
    #[case::plus_alternate("base_path: ${BASE:+set}\n")]
    #[case::error_if_unset("connection_string: ${DB:?required}\n")]
    #[case::bare_dash("base_path: ${BASE-fallback}\n")]
    #[case::escaped_dollar("escaped: $$BASE\n")]
    #[case::escaped_braced("escaped: $${a:b}\n")]
    #[case::plain("plain: no substitution here\n")]
    fn rewrite_legacy_substitution_syntax_borrows_when_unchanged(#[case] input: &str) {
        assert!(
            matches!(rewrite_legacy_substitution_syntax(input), Cow::Borrowed(_)),
            "should not rewrite: {input:?}"
        );
    }

    #[rstest]
    #[case::simple("${BASE:fallback}", "${BASE:-fallback}")]
    #[case::colon_in_default("${UNSET:postgres://h:5432/db}", "${UNSET:-postgres://h:5432/db}")]
    #[case::nested("${a:x${b:y}}", "${a:-x${b:-y}}")]
    #[case::prefix_and_suffix("prefix-${BASE:def}-suffix", "prefix-${BASE:-def}-suffix")]
    #[case::escaped_prefix("$$${a:b}", "$$${a:-b}")]
    fn rewrite_legacy_substitution_syntax_translates_single_colon(
        #[case] input: &str,
        #[case] expected: &str,
    ) {
        match rewrite_legacy_substitution_syntax(input) {
            Cow::Owned(s) => assert_eq!(s, expected, "input {input:?}"),
            Cow::Borrowed(_) => panic!("expected rewrite for {input:?}"),
        }
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn substitution_legacy_single_colon_connection_string() {
        let config = parse_with_env(
            "postgres:\n  connection_string: ${UNSET:postgres://postgres@localhost:5432/db}\n",
            &HashMap::new(),
        );
        let pg = match config.postgres {
            OptOneMany::One(pg) => pg,
            other => panic!("expected exactly one postgres config, got: {other:?}"),
        };
        assert_eq!(
            pg.connection_string.as_deref(),
            Some("postgres://postgres@localhost:5432/db")
        );
    }

    #[rstest]
    #[case::unquoted("base_path: ${BASE}\n", "/my/path")]
    #[case::single_quoted("base_path: '${BASE}'\n", "${BASE}")]
    #[case::double_quoted("base_path: \"${BASE}\"\n", "${BASE}")]
    fn substitution_only_in_plain_scalars(#[case] yaml: &str, #[case] expected: &str) {
        let env = props(&[("BASE", "/my/path")]);
        let config = parse_with_env(yaml, &env);
        assert_eq!(config.srv.base_path.as_deref(), Some(expected));
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn substitution_ignores_dollar_tokens_in_comments() {
        let yaml = indoc::indoc! {r"
            # Database configuration. This can also be a list of PG configs.
            postgres:
              # Database connection string. You can use env vars too, for example:
              #   $DATABASE_URL
              #   ${DATABASE_URL:-postgresql://postgres@localhost/db}
              connection_string: 'postgres://postgres:postgres@db:5432/ehrenamtskarte'
        "};
        let config = parse_config(
            yaml,
            &HashMap::<String, String>::new(),
            Path::new("config.yaml"),
        )
        .expect("comments containing ${VAR} must not trigger substitution");
        let one = match config.postgres {
            OptOneMany::One(pg) => pg,
            other => panic!("expected exactly one postgres config, got: {other:?}"),
        };
        assert_eq!(
            one.connection_string.as_deref(),
            Some("postgres://postgres:postgres@db:5432/ehrenamtskarte"),
        );
    }

    #[rstest]
    #[case::braced_in_migrated_key(
        &[("SIZE", "512")],
        "cache_size_mb: ${SIZE}\n",
        Some(512), None, None,
    )]
    #[case::sibling_to_migrated_key(
        &[("SIZE", "256"), ("BASE", "/served")],
        "tile_cache_size_mb: ${SIZE}\nbase_path: ${BASE}\n",
        None, Some(256), Some("/served"),
    )]
    #[case::bare_in_migrated_key(
        &[("SIZE", "1024"), ("BASE", "/p")],
        "cache_size_mb: $SIZE\nbase_path: $BASE\n",
        Some(1024), None, Some("/p"),
    )]
    fn substitution_survives_deprecated_migration(
        #[case] env_pairs: &[(&str, &str)],
        #[case] yaml: &str,
        #[case] expected_size_mb: Option<u64>,
        #[case] expected_tile_size_mb: Option<u64>,
        #[case] expected_base_path: Option<&str>,
    ) {
        let env = props(env_pairs);
        let config = parse_with_env(yaml, &env);
        assert_eq!(config.cache.size_mb, expected_size_mb);
        assert_eq!(config.cache.tile_size_mb, expected_tile_size_mb);
        assert_eq!(config.srv.base_path.as_deref(), expected_base_path);
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn string_references_var_matches_substitution_tokens() {
        for s in [
            "${DATABASE_URL}",
            "${DATABASE_URL:-postgres://x}",
            "${DATABASE_URL:?required}",
            "${DATABASE_URL-no-default}",
            "${DATABASE_URL+set}",
            "${DATABASE_URL?msg}",
            "prefix-${DATABASE_URL}-suffix",
            "$DATABASE_URL",
            "$DATABASE_URL/path",
            "$DATABASE_URL ",
        ] {
            assert!(
                string_references_var(s, "DATABASE_URL"),
                "expected a hit in {s:?}"
            );
        }
        for s in [
            "DATABASE_URL",
            "$$DATABASE_URL",
            "$DATABASE_URLISH",
            "${DATABASE_URL_OTHER}",
            "${OTHER_DATABASE_URL}",
            "postgres://db",
        ] {
            assert!(
                !string_references_var(s, "DATABASE_URL"),
                "expected no hit in {s:?}"
            );
        }
    }
}