valhalla 0.6.38

Rust bindings for Valhalla routing engine
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
use miniserde::{Deserialize, json};
use std::fs;
use std::path::Path;

fn main() {
    let build_type = match (
        std::env::var("PROFILE").as_deref(),
        std::env::var("DEBUG").as_deref(),
    ) {
        (Ok("debug"), _) => "Debug",
        (Ok("release"), Ok("true")) => "RelWithDebInfo",
        _ => "Release",
    };
    // Unity build speeds up compilation but can complicate debugging and profiling.
    // Disable with `UNITY_BUILD=OFF`, e.g. `UNITY_BUILD=OFF cargo bench`
    let unity_build = !matches!(std::env::var("UNITY_BUILD").as_deref(), Ok("OFF"));
    // LTO between Rust and C++ requires LLD:
    // https://doc.rust-lang.org/beta/rustc/linker-plugin-lto.html
    // Disable LTO for Debug builds to have reasonable compile times.
    let lto = build_type != "Debug" && has_lld();

    // Build & link required Valhalla libraries
    let dst = cmake::Config::new("valhalla")
        .define("CMAKE_BUILD_TYPE", build_type)
        .define("CMAKE_EXPORT_COMPILE_COMMANDS", "ON") // Required to extract include paths
        .define(
            "CMAKE_INTERPROCEDURAL_OPTIMIZATION",
            if lto { "ON" } else { "OFF" },
        )
        // Disable everything we don't need to reduce number of system dependencies and speed up compilation
        .define("ENABLE_TOOLS", "OFF")
        .define("ENABLE_DATA_TOOLS", "OFF")
        .define("ENABLE_SERVICES", "OFF")
        .define("ENABLE_HTTP", "OFF")
        .define("ENABLE_PYTHON_BINDINGS", "OFF")
        .define("ENABLE_TESTS", "OFF")
        .define("ENABLE_GEOTIFF", "OFF")
        .define("ENABLE_LZ4", "OFF") // LZ4 support for elevation tiles for `/height` endpoint
        .define("ENABLE_SINGLE_FILES_WERROR", "OFF")
        .define("CMAKE_UNITY_BUILD", if unity_build { "ON" } else { "OFF" })
        // Rust type system guarantees that `GraphTile` instance will be accessed only from a single thread
        .define("ENABLE_THREAD_SAFE_TILE_REF_COUNT", "OFF")
        .define("LOGGING_LEVEL", "WARN") // todo: Provide an API for setting custom loggers to Valhalla
        .build_target("valhalla")
        .build();
    // Clean up temporarily created `valhalla/third_party/tz/leapseconds` to keep source tree clean.
    // N.B.: avoid adding `println!("cargo:rerun-if-changed=valhalla");` as this file will always trigger a rebuild.
    let _ = fs::remove_file("valhalla/third_party/tz/leapseconds");

    // Include paths to the dependencies are tricky, so let cmake do its job while we can read the result.
    let valhalla_includes = extract_includes(
        &dst.join("build/compile_commands.json"),
        if unity_build {
            "/valhalla.dir/Unity/unity_0_cxx.cxx"
        } else {
            "config.cc"
        },
    );

    // Linking order is important, so valhalla-cxxbridge must come first.
    let rust_sources = ["src/config.rs", "src/lib.rs"]
        .into_iter()
        .chain(cfg!(feature = "proto").then_some("src/actor.rs"));
    cxx_build::bridges(rust_sources)
        .file("src/libvalhalla.cpp")
        .std("c++20")
        .includes(valhalla_includes)
        .flags(if lto { vec!["-flto=thin"] } else { vec![] })
        .compile("libvalhalla-cxxbridge");
    println!("cargo:rerun-if-changed=src/actor.hpp");
    println!("cargo:rerun-if-changed=src/config.hpp");
    println!("cargo:rerun-if-changed=src/costing.hpp");
    println!("cargo:rerun-if-changed=src/libvalhalla.cpp");
    println!("cargo:rerun-if-changed=src/libvalhalla.hpp");

    // pkg_config resolves all dependencies based on the libvalhalla.pc file (generated by cmake)
    // and emits the correct cargo link directives.
    let dst = dst.display().to_string();
    println!("cargo:rustc-link-search=native={dst}/build/src/");
    if let Err(err) = pkg_config::Config::new()
        .arg("--with-path")
        .arg(format!("{dst}/build"))
        .probe("libvalhalla")
    {
        // pkg_config error messages are not very descriptive, so we add some context here.
        let pc_file = format!("{}/build/libvalhalla.pc", dst);
        let pc_content = fs::read_to_string(&pc_file)
            .unwrap_or_else(|_| "Could not read libvalhalla.pc file".to_string());
        panic!("Failed to link libvalhalla: {err}\nlibvalhalla.pc:\n{pc_content}");
    }

    #[cfg(feature = "proto")]
    {
        let proto_files: Vec<_> = fs::read_dir("valhalla/proto/descriptors")
            .expect("Failed to read valhalla/proto/descriptors directory")
            .map(|entry| entry.expect("Bad fs entry").path())
            .filter(|path| path.extension().is_some_and(|ext| ext == "proto"))
            .collect();
        prost_build::compile_protos(&proto_files, &["valhalla/proto/descriptors/"])
            .expect("Failed to compile proto files");
        println!("cargo:rerun-if-changed=valhalla/proto/descriptors");
    }

    // Generate `config_builder.rs` — typed Rust structs mirroring Valhalla's Python-defined config,
    // with doc comments and `write_to_ptree` methods that populate C++ `boost::property_tree`
    // directly via FFI (no JSON roundtrip).
    let script = fs::read_to_string("valhalla/scripts/valhalla_build_config")
        .expect("Failed to read valhalla_build_config script");
    let config = parse_valhalla_config(&script);
    let help = parse_valhalla_help_text(&script);

    let code = generate_config_code(&config, &help);
    let out_dir = std::env::var("OUT_DIR").unwrap();
    fs::write(Path::new(&out_dir).join("config_builder.rs"), &code)
        .expect("Failed to write config_builder.rs");
    println!("cargo:rerun-if-changed=valhalla/scripts/valhalla_build_config");
}

/// https://clang.llvm.org/docs/JSONCompilationDatabase.html
#[derive(Deserialize)]
struct CompileCommand {
    command: String,
    file: String,
}

fn extract_includes(compile_commands: &Path, cpp_source: &str) -> Vec<String> {
    assert!(compile_commands.exists(), "compile_commands.json not found");

    let content =
        fs::read_to_string(compile_commands).expect("Failed to read compile_commands.json");
    let commands: Vec<CompileCommand> =
        json::from_str(&content).expect("Failed to parse compile_commands.json");

    let command = commands
        .into_iter()
        .find(|cmd| cmd.file.ends_with(cpp_source))
        .expect("Failed to find reference cpp source file");

    // Parse -I/path/to/include and -isystem /path/to/include
    let args: Vec<&str> = command.command.split_whitespace().collect();
    let mut includes = Vec::new();

    for i in 0..args.len() {
        if args[i].starts_with("-I") {
            // Handle -I/path/to/include
            includes.push(args[i][2..].to_string());
        } else if args[i] == "-isystem" && i + 1 < args.len() {
            // Handle -isystem /path/to/include
            includes.push(args[i + 1].to_string());
        }
    }
    includes
}

/// Check whether LLD is being used as the linker.
fn has_lld() -> bool {
    if std::env::var("TARGET").is_ok_and(|t| t.contains("apple-darwin")) {
        return true;
    }
    if std::env::var("CARGO_ENCODED_RUSTFLAGS").is_ok_and(|f| f.contains("-fuse-ld=lld")) {
        return true;
    }
    false
}

/// Represents a Python literal value from `valhalla_build_config`.
/// Mirrors the subset of Python syntax used in the `config` and `help_text` dicts.
#[derive(Debug, Clone)]
enum PyValue {
    Dict(Vec<(String, PyValue)>),
    List(Vec<PyValue>),
    Str(String),
    Int(i64),
    Float(f64),
    Bool(bool),
    Optional(String),
}

struct PyParser {
    chars: Vec<char>,
    pos: usize,
}

impl PyParser {
    fn new(input: &str) -> Self {
        Self {
            chars: input.chars().collect(),
            pos: 0,
        }
    }

    fn skip_ws(&mut self) {
        while self.pos < self.chars.len() {
            match self.chars[self.pos] {
                ' ' | '\t' | '\n' | '\r' => self.pos += 1,
                '#' => {
                    while self.pos < self.chars.len() && self.chars[self.pos] != '\n' {
                        self.pos += 1;
                    }
                }
                _ => break,
            }
        }
    }

    fn peek(&self) -> char {
        self.chars[self.pos]
    }

    fn advance(&mut self) -> char {
        let c = self.chars[self.pos];
        self.pos += 1;
        c
    }

    fn expect(&mut self, expected: char) {
        self.skip_ws();
        let c = self.advance();
        assert_eq!(c, expected, "Expected '{expected}', got '{c}'");
    }

    fn consume(&mut self, word: &str) {
        for expected in word.chars() {
            let c = self.advance();
            assert_eq!(c, expected, "Expected '{expected}', got '{c}'");
        }
    }

    fn parse_value(&mut self) -> PyValue {
        self.skip_ws();
        match self.peek() {
            '{' => self.parse_dict(),
            '[' => self.parse_list(),
            '"' | '\'' => PyValue::Str(self.parse_string()),
            '(' => {
                self.advance();
                let mut result = String::new();
                loop {
                    self.skip_ws();
                    if self.peek() == ')' {
                        self.advance();
                        break;
                    }
                    result.push_str(&self.parse_string());
                }
                PyValue::Str(result)
            }
            'T' => {
                self.consume("True");
                PyValue::Bool(true)
            }
            'F' => {
                self.consume("False");
                PyValue::Bool(false)
            }
            'O' => {
                self.consume("Optional");
                self.expect('(');
                let type_name = self.parse_ident();
                self.expect(')');
                PyValue::Optional(type_name)
            }
            c if c == '-' || c.is_ascii_digit() => self.parse_number(),
            c => panic!("Unexpected character '{c}' at position {}", self.pos),
        }
    }

    fn parse_dict(&mut self) -> PyValue {
        self.expect('{');
        let mut entries = Vec::new();
        loop {
            self.skip_ws();
            if self.peek() == '}' {
                self.advance();
                break;
            }
            let key = self.parse_string();
            self.expect(':');
            let value = self.parse_value();
            entries.push((key, value));
            self.skip_ws();
            if self.peek() == ',' {
                self.advance();
            }
        }
        PyValue::Dict(entries)
    }

    fn parse_list(&mut self) -> PyValue {
        self.expect('[');
        let mut items = Vec::new();
        loop {
            self.skip_ws();
            if self.peek() == ']' {
                self.advance();
                break;
            }
            items.push(self.parse_value());
            self.skip_ws();
            if self.peek() == ',' {
                self.advance();
            }
        }
        PyValue::List(items)
    }

    fn parse_string(&mut self) -> String {
        self.skip_ws();
        let quote = self.advance();
        assert!(
            quote == '"' || quote == '\'',
            "Expected string, got '{quote}'"
        );
        let mut s = String::new();
        loop {
            let c = self.advance();
            if c == quote {
                break;
            }
            match c {
                '\\' => {
                    let next = self.advance();
                    match next {
                        '"' | '\'' | '\\' => s.push(next),
                        'n' => s.push('\n'),
                        't' => s.push('\t'),
                        'r' => s.push('\r'),
                        _ => {
                            s.push('\\');
                            s.push(next);
                        }
                    }
                }
                _ => s.push(c),
            }
        }
        s
    }

    fn parse_number(&mut self) -> PyValue {
        self.skip_ws();
        let start = self.pos;
        if self.peek() == '-' {
            self.advance();
        }
        while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
            self.advance();
        }
        let mut is_float = false;
        if self.pos < self.chars.len() && self.chars[self.pos] == '.' {
            is_float = true;
            self.advance();
            while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
                self.advance();
            }
        }
        if self.pos < self.chars.len()
            && (self.chars[self.pos] == 'e' || self.chars[self.pos] == 'E')
        {
            is_float = true;
            self.advance();
            if self.pos < self.chars.len()
                && (self.chars[self.pos] == '+' || self.chars[self.pos] == '-')
            {
                self.advance();
            }
            while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
                self.advance();
            }
        }
        let text: String = self.chars[start..self.pos].iter().collect();
        if is_float {
            PyValue::Float(text.parse().expect("Invalid float"))
        } else {
            PyValue::Int(text.parse().expect("Invalid int"))
        }
    }

    fn parse_ident(&mut self) -> String {
        self.skip_ws();
        let start = self.pos;
        while self.pos < self.chars.len()
            && (self.chars[self.pos].is_alphanumeric() || self.chars[self.pos] == '_')
        {
            self.advance();
        }
        self.chars[start..self.pos].iter().collect()
    }
}

/// Extracts the `config = { ... }` dictionary from `valhalla_build_config`.
fn parse_valhalla_config(config_script: &str) -> PyValue {
    let marker = "\nconfig = {";
    let start = config_script
        .find(marker)
        .expect("No `config` dict in valhalla_build_config script");
    let dict_start = start + "\nconfig = ".len();

    let mut parser = PyParser::new(&config_script[dict_start..]);
    parser.parse_value()
}

/// Extracts the `help_text = { ... }` dictionary — used for `///` doc comments.
fn parse_valhalla_help_text(config_script: &str) -> PyValue {
    let marker = "\nhelp_text = {";
    let start = config_script
        .find(marker)
        .expect("No `nhelp_text` dict in valhalla_build_config script");
    let dict_start = start + "\nhelp_text = ".len();

    let mut parser = PyParser::new(&config_script[dict_start..]);
    parser.parse_value()
}

/// Walks the `help_text` tree by dotted path and returns the leaf string, if any.
fn lookup_help<'a>(help: &'a PyValue, path: &[&str]) -> Option<&'a str> {
    let mut current = help;
    for &key in path {
        match current {
            PyValue::Dict(entries) => {
                current = &entries.iter().find(|(k, _)| k == key)?.1;
            }
            _ => return None,
        }
    }
    match current {
        PyValue::Str(s) => Some(s.as_str()),
        _ => None,
    }
}

fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
                None => String::new(),
            }
        })
        .collect()
}

fn rust_field_name(key: &str) -> String {
    const RUST_KEYWORDS: &[&str] = &[
        "as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn",
        "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref",
        "return", "self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe",
        "use", "where", "while", "async", "await", "dyn",
    ];

    if key.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        format!("level_{key}")
    } else if RUST_KEYWORDS.contains(&key) {
        format!("r#{key}")
    } else {
        key.to_string()
    }
}

/// Escapes a Rust string literal and wraps it in quotes, e.g. `foo\nbar` -> `"foo\\nbar".to_string()`.
fn rust_string_literal(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 16);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            '\r' => out.push_str("\\r"),
            _ => out.push(c),
        }
    }
    out.push_str("\".to_string()");
    out
}

fn rust_float_literal(f: f64) -> String {
    if f.fract() == 0.0 && !f.is_infinite() && !f.is_nan() {
        format!("{f:.1}")
    } else {
        format!("{f}")
    }
}

fn struct_name_from_path(path: &[&str]) -> String {
    path.iter()
        .map(|s| to_pascal_case(s))
        .collect::<Vec<_>>()
        .join("")
}

fn infer_list_type(items: &[PyValue]) -> String {
    if items.is_empty() {
        return "Vec<String>".to_string();
    }
    match &items[0] {
        PyValue::Str(_) => "Vec<String>".to_string(),
        PyValue::Int(_) => "Vec<i64>".to_string(),
        PyValue::Float(_) => "Vec<f64>".to_string(),
        _ => "Vec<String>".to_string(),
    }
}

fn optional_rust_type(type_name: &str) -> String {
    match type_name {
        "str" => "Option<String>".to_string(),
        "int" => "Option<i64>".to_string(),
        "bool" => "Option<bool>".to_string(),
        "list" => "Option<Vec<String>>".to_string(),
        _ => panic!("Unknown Optional type: {type_name}"),
    }
}

fn can_derive_default(entries: &[(String, PyValue)]) -> bool {
    entries.iter().all(|(_, value)| {
        match value {
            PyValue::Dict(_) => true,         // Child structs can have Default
            PyValue::Optional(_) => true,     // None is Default for Option<T>
            PyValue::List(l) => l.is_empty(), // Empty vec is Default
            PyValue::Str(s) => s.is_empty(),  // Empty string is Default for String
            PyValue::Int(n) => *n == Default::default(),
            PyValue::Float(f) => *f == Default::default(),
            PyValue::Bool(b) => *b == Default::default(),
        }
    })
}

fn default_value_expr(value: &PyValue, parent_path: &[&str], key: &str) -> String {
    match value {
        PyValue::Dict(_) => {
            let child_path: Vec<&str> = parent_path
                .iter()
                .copied()
                .chain(std::iter::once(key))
                .collect();
            let child_type = struct_name_from_path(&child_path);
            format!("{child_type}::default()")
        }
        PyValue::Str(s) => rust_string_literal(s),
        PyValue::Int(n) => n.to_string(),
        PyValue::Float(f) => rust_float_literal(*f),
        PyValue::Bool(b) => b.to_string(),
        PyValue::List(items) => list_default_expr(items),
        PyValue::Optional(_) => "None".to_string(),
    }
}

fn list_default_expr(items: &[PyValue]) -> String {
    if items.is_empty() {
        return "vec![]".to_string();
    }
    let parts: Vec<String> = items
        .iter()
        .map(|v| match v {
            PyValue::Str(s) => rust_string_literal(s),
            PyValue::Int(n) => n.to_string(),
            PyValue::Float(f) => rust_float_literal(*f),
            PyValue::Bool(b) => b.to_string(),
            _ => panic!("Unsupported list element type in default"),
        })
        .collect();
    format!("vec![{}]", parts.join(", "))
}

/// Emits the full `config_builder.rs`: structs, Default impls, write_to_ptree
/// methods, PtreePut trait, and ConfigBuilder::build(). Self-contained — the
/// only external dependency is `ffi` and `Config` from config.rs.
fn generate_config_code(config: &PyValue, help: &PyValue) -> String {
    let mut output = String::new();
    if let PyValue::Dict(entries) = config {
        // top comment for `ConfigBuilder` struct.
        output.push_str("/// Valhalla configuration builder.\n");
        output.push_str(
            "/// Auto-generated from `valhalla/scripts/valhalla_build_config` — do not edit.\n",
        );
        generate_struct(&mut output, "ConfigBuilder", entries, &[], help);
    }
    output.push_str(CONFIG_BUILDER_IMPL);
    output
}

const CONFIG_BUILDER_IMPL: &str = r#"impl ConfigBuilder {
    /// Produces the finalized [`Config`].
    ///
    /// All fields are initialized with Valhalla's built-in defaults from
    /// `valhalla_build_config`. Modify any field before calling `build()`.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut builder = valhalla::ConfigBuilder::default();
    /// builder.mjolnir.tile_extract = "path/to/tiles.tar".to_string();
    /// let config = builder.build();
    /// ```
    pub fn build(&self) -> Config {
        let mut pt = ffi::ptree_new();
        self.write_to_ptree(pt.pin_mut(), "");
        Config(pt)
    }
}

pub(crate) trait PtreePut {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str);
}

impl PtreePut for String {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_str(pt, path, self);
    }
}

impl PtreePut for i64 {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_int(pt, path, *self);
    }
}

impl PtreePut for f64 {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_float(pt, path, *self);
    }
}

impl PtreePut for bool {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_bool(pt, path, *self);
    }
}

impl<T: PtreePut> PtreePut for Option<T> {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        if let Some(v) = self {
            v.put(pt, path);
        }
    }
}

impl PtreePut for Vec<String> {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_str_array(pt, path, self);
    }
}

impl PtreePut for Vec<i64> {
    fn put(&self, pt: std::pin::Pin<&mut ffi::ptree>, path: &str) {
        ffi::ptree_put_int_array(pt, path, self);
    }
}

fn ptree_path(prefix: &str, key: &str) -> String {
    if prefix.is_empty() {
        key.to_string()
    } else {
        format!("{prefix}.{key}")
    }
}
"#;

/// Emits one struct definition + Default impl + `write_to_ptree` method,
/// recursing into child dicts first so nested types are defined before use.
fn generate_struct(
    output: &mut String,
    struct_name: &str,
    entries: &[(String, PyValue)],
    path: &[&str],
    help: &PyValue,
) {
    // Struct-level doc from __note__ in help_text
    let mut note_path: Vec<&str> = path.to_vec();
    note_path.push("__note__");
    if let Some(note) = lookup_help(help, &note_path) {
        for line in note.lines() {
            output.push_str(&format!("/// {line}\n"));
        }
    }

    let can_derive = can_derive_default(entries);
    if can_derive {
        output.push_str("#[derive(Debug, Clone, Default)]\n");
    } else {
        output.push_str("#[derive(Debug, Clone)]\n");
    }
    output.push_str(&format!("pub struct {struct_name} {{\n"));

    for (key, value) in entries {
        let field = rust_field_name(key);

        // Field doc comment from help_text
        let mut field_path: Vec<&str> = path.to_vec();
        field_path.push(key.as_str());
        if let Some(doc) = lookup_help(help, &field_path) {
            for line in doc.lines() {
                output.push_str(&format!("    /// {line}\n"));
            }
        }

        match value {
            PyValue::Dict(_) => {
                let child_path: Vec<&str> = path
                    .iter()
                    .copied()
                    .chain(std::iter::once(key.as_str()))
                    .collect();
                let child_type = struct_name_from_path(&child_path);
                output.push_str(&format!("    pub {field}: {child_type},\n"));
            }
            PyValue::Str(_) => output.push_str(&format!("    pub {field}: String,\n")),
            PyValue::Int(_) => output.push_str(&format!("    pub {field}: i64,\n")),
            PyValue::Float(_) => output.push_str(&format!("    pub {field}: f64,\n")),
            PyValue::Bool(_) => output.push_str(&format!("    pub {field}: bool,\n")),
            PyValue::List(items) => {
                let list_type = infer_list_type(items);
                output.push_str(&format!("    pub {field}: {list_type},\n"));
            }
            PyValue::Optional(type_name) => {
                let rust_type = optional_rust_type(type_name);
                output.push_str(&format!("    pub {field}: {rust_type},\n"));
            }
        }
    }
    output.push_str("}\n\n");

    // Default impl - only generate if we can't derive it
    if !can_derive {
        output.push_str(&format!("impl Default for {struct_name} {{\n"));
        output.push_str("    fn default() -> Self {\n");
        output.push_str("        Self {\n");
        for (key, value) in entries {
            let field = rust_field_name(key);
            let default_expr = default_value_expr(value, path, key);
            output.push_str(&format!("            {field}: {default_expr},\n"));
        }
        output.push_str("        }\n");
        output.push_str("    }\n");
        output.push_str("}\n\n");
    }

    // write_to_ptree impl
    output.push_str(&format!("impl {struct_name} {{\n"));
    output.push_str("    pub(crate) fn write_to_ptree(&self, mut pt: std::pin::Pin<&mut ffi::ptree>, prefix: &str) {\n");
    for (key, value) in entries {
        let field = rust_field_name(key);
        if matches!(value, PyValue::Dict(_)) {
            output.push_str(&format!(
                "        self.{field}.write_to_ptree(pt.as_mut(), &ptree_path(prefix, \"{key}\"));\n"
            ));
        } else {
            output.push_str(&format!(
                "        self.{field}.put(pt.as_mut(), &ptree_path(prefix, \"{key}\"));\n"
            ));
        }
    }
    output.push_str("    }\n");
    output.push_str("}\n\n");

    // Recurse into child dicts after emitting self, so ConfigBuilder appears first.
    for (key, value) in entries {
        if let PyValue::Dict(child_entries) = value {
            let child_path: Vec<&str> = path
                .iter()
                .copied()
                .chain(std::iter::once(key.as_str()))
                .collect();
            let child_name = struct_name_from_path(&child_path);
            generate_struct(output, &child_name, child_entries, &child_path, help);
        }
    }
}