kiutils_kicad 0.3.0

Typed KiCad document model and codecs for seamless, lossless edit round-trips
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
use std::fs;
use std::path::Path;

use kiutils_sexpr::{parse_one, Atom, CstDocument, Node};

use crate::diagnostic::{Diagnostic, Severity};
use crate::sexpr_edit::{
    atom_quoted, atom_symbol, ensure_root_head_any, mutate_root_and_refresh,
    remove_property as remove_property_node, root_head, upsert_property_preserve_tail,
    upsert_scalar,
};
use crate::sexpr_utils::{
    atom_as_f64, atom_as_string, head_of, list_child_head_count, second_atom_bool, second_atom_f64,
    second_atom_i32, second_atom_string,
};
use crate::version_diag::collect_version_diagnostics;
use crate::{Error, UnknownNode, WriteMode};

// --- Footprint pad types ---

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpPadNet {
    pub code: Option<i32>,
    pub name: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpPadDrill {
    pub shape: Option<String>,
    pub diameter: Option<f64>,
    pub width: Option<f64>,
    pub offset: Option<[f64; 2]>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpPad {
    pub number: Option<String>,
    pub pad_type: Option<String>,
    pub shape: Option<String>,
    pub at: Option<[f64; 2]>,
    pub rotation: Option<f64>,
    pub size: Option<[f64; 2]>,
    pub layers: Vec<String>,
    pub net: Option<FpPadNet>,
    pub drill: Option<FpPadDrill>,
    pub uuid: Option<String>,
    pub pin_function: Option<String>,
    pub pin_type: Option<String>,
    pub locked: bool,
    pub property: Option<String>,
    pub remove_unused_layers: bool,
    pub keep_end_layers: bool,
    pub roundrect_rratio: Option<f64>,
    pub chamfer_ratio: Option<f64>,
    pub chamfer: Vec<String>,
    pub die_length: Option<f64>,
    pub solder_mask_margin: Option<f64>,
    pub solder_paste_margin: Option<f64>,
    pub solder_paste_margin_ratio: Option<f64>,
    pub clearance: Option<f64>,
    pub zone_connect: Option<i32>,
    pub thermal_width: Option<f64>,
    pub thermal_gap: Option<f64>,
    pub custom_clearance: Option<String>,
    pub custom_anchor: Option<String>,
    pub custom_primitives: usize,
}

// --- Footprint graphic types ---

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpGraphic {
    pub token: String,
    pub layer: Option<String>,
    pub text: Option<String>,
    pub start: Option<[f64; 2]>,
    pub end: Option<[f64; 2]>,
    pub center: Option<[f64; 2]>,
    pub uuid: Option<String>,
    pub locked: bool,
    pub width: Option<f64>,
    pub stroke_type: Option<String>,
    pub fill_type: Option<String>,
    pub at: Option<[f64; 2]>,
    pub angle: Option<f64>,
    pub font_size: Option<[f64; 2]>,
    pub font_thickness: Option<f64>,
}

// --- Footprint model type ---

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpModel {
    pub path: Option<String>,
    pub at: Option<[f64; 3]>,
    pub scale: Option<[f64; 3]>,
    pub rotate: Option<[f64; 3]>,
    pub hide: bool,
}

// --- Footprint zone type ---

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpZone {
    pub net: Option<i32>,
    pub net_name: Option<String>,
    pub name: Option<String>,
    pub layer: Option<String>,
    pub layers: Vec<String>,
    pub hatch: Option<String>,
    pub fill_enabled: Option<bool>,
    pub polygon_count: usize,
    pub filled_polygon_count: usize,
    pub has_keepout: bool,
}

// --- Footprint group type ---

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpGroup {
    pub name: Option<String>,
    pub group_id: Option<String>,
    pub member_count: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FpProperty {
    pub key: String,
    pub value: String,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FootprintAst {
    pub lib_id: Option<String>,
    pub version: Option<i32>,
    pub tedit: Option<String>,
    pub generator: Option<String>,
    pub generator_version: Option<String>,
    pub layer: Option<String>,
    pub descr: Option<String>,
    pub tags: Option<String>,
    pub property_count: usize,
    pub attr_present: bool,
    pub locked_present: bool,
    pub private_layers_present: bool,
    pub net_tie_pad_groups_present: bool,
    pub embedded_fonts_present: bool,
    pub has_embedded_files: bool,
    pub embedded_file_count: usize,
    pub clearance: Option<String>,
    pub solder_mask_margin: Option<String>,
    pub solder_paste_margin: Option<String>,
    pub solder_paste_margin_ratio: Option<String>,
    pub duplicate_pad_numbers_are_jumpers: Option<bool>,
    pub pad_count: usize,
    pub pads: Vec<FpPad>,
    pub model_count: usize,
    pub models: Vec<FpModel>,
    pub zone_count: usize,
    pub zones: Vec<FpZone>,
    pub group_count: usize,
    pub groups: Vec<FpGroup>,
    pub fp_line_count: usize,
    pub fp_rect_count: usize,
    pub fp_circle_count: usize,
    pub fp_arc_count: usize,
    pub fp_poly_count: usize,
    pub fp_curve_count: usize,
    pub fp_text_count: usize,
    pub fp_text_box_count: usize,
    pub dimension_count: usize,
    pub graphic_count: usize,
    pub graphics: Vec<FpGraphic>,
    pub attr: Vec<String>,
    pub locked: bool,
    pub placed: bool,
    pub private_layers: Vec<String>,
    pub net_tie_pad_groups: Vec<Vec<String>>,
    pub reference: Option<String>,
    pub value: Option<String>,
    pub properties: Vec<FpProperty>,
    pub unknown_nodes: Vec<UnknownNode>,
}
#[derive(Debug, Clone)]
pub struct FootprintDocument {
    ast: FootprintAst,
    cst: CstDocument,
    diagnostics: Vec<Diagnostic>,
    ast_dirty: bool,
}

impl FootprintDocument {
    pub fn ast(&self) -> &FootprintAst {
        &self.ast
    }

    pub fn ast_mut(&mut self) -> &mut FootprintAst {
        self.ast_dirty = true;
        &mut self.ast
    }

    pub fn set_lib_id<S: Into<String>>(&mut self, lib_id: S) -> &mut Self {
        let lib_id = lib_id.into();
        self.mutate_root_items(|items| {
            let value = atom_quoted(lib_id);
            if let Some(current) = items.get(1) {
                if *current == value {
                    false
                } else {
                    items[1] = value;
                    true
                }
            } else {
                items.push(value);
                true
            }
        })
    }

    pub fn set_version(&mut self, version: i32) -> &mut Self {
        self.mutate_root_items(|items| {
            upsert_scalar(items, "version", atom_symbol(version.to_string()), 2)
        })
    }

    pub fn set_generator<S: Into<String>>(&mut self, generator: S) -> &mut Self {
        self.mutate_root_items(|items| {
            upsert_scalar(items, "generator", atom_symbol(generator.into()), 2)
        })
    }

    pub fn set_generator_version<S: Into<String>>(&mut self, generator_version: S) -> &mut Self {
        self.mutate_root_items(|items| {
            upsert_scalar(
                items,
                "generator_version",
                atom_quoted(generator_version.into()),
                2,
            )
        })
    }

    pub fn set_layer<S: Into<String>>(&mut self, layer: S) -> &mut Self {
        self.mutate_root_items(|items| upsert_scalar(items, "layer", atom_quoted(layer.into()), 2))
    }

    pub fn set_descr<S: Into<String>>(&mut self, descr: S) -> &mut Self {
        self.mutate_root_items(|items| upsert_scalar(items, "descr", atom_quoted(descr.into()), 2))
    }

    pub fn set_tags<S: Into<String>>(&mut self, tags: S) -> &mut Self {
        self.mutate_root_items(|items| upsert_scalar(items, "tags", atom_quoted(tags.into()), 2))
    }

    pub fn set_reference<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.upsert_property("Reference", value)
    }

    pub fn set_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.upsert_property("Value", value)
    }

    pub fn upsert_property<K: Into<String>, V: Into<String>>(
        &mut self,
        key: K,
        value: V,
    ) -> &mut Self {
        let key = key.into();
        let value = value.into();
        self.mutate_root_items(|items| upsert_property_preserve_tail(items, &key, &value, 2))
    }

    pub fn remove_property(&mut self, key: &str) -> &mut Self {
        let key = key.to_string();
        self.mutate_root_items(|items| remove_property_node(items, &key, 2))
    }

    pub fn cst(&self) -> &CstDocument {
        &self.cst
    }

    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    pub fn write<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
        self.write_mode(path, WriteMode::Lossless)
    }

    pub fn write_mode<P: AsRef<Path>>(&self, path: P, mode: WriteMode) -> Result<(), Error> {
        if self.ast_dirty {
            return Err(Error::Validation(
                "ast_mut changes are not serializable; use document setter APIs".to_string(),
            ));
        }
        match mode {
            WriteMode::Lossless => fs::write(path, self.cst.to_lossless_string())?,
            WriteMode::Canonical => fs::write(path, self.cst.to_canonical_string())?,
        }
        Ok(())
    }

    fn mutate_root_items<F>(&mut self, mutate: F) -> &mut Self
    where
        F: FnOnce(&mut Vec<Node>) -> bool,
    {
        mutate_root_and_refresh(
            &mut self.cst,
            &mut self.ast,
            &mut self.diagnostics,
            mutate,
            parse_ast,
            |cst, ast| collect_diagnostics(cst, ast.version),
        );
        self.ast_dirty = false;
        self
    }
}

pub struct FootprintFile;

impl FootprintFile {
    pub fn read<P: AsRef<Path>>(path: P) -> Result<FootprintDocument, Error> {
        let raw = fs::read_to_string(path)?;
        let cst = parse_one(&raw)?;
        ensure_root_head_any(&cst, &["footprint", "module"])?;
        let ast = parse_ast(&cst);
        let diagnostics = collect_diagnostics(&cst, ast.version);
        Ok(FootprintDocument {
            ast,
            cst,
            diagnostics,
            ast_dirty: false,
        })
    }
}

fn collect_diagnostics(cst: &CstDocument, version: Option<i32>) -> Vec<Diagnostic> {
    let mut diagnostics = collect_version_diagnostics(version);
    if root_head(cst) == Some("module") {
        diagnostics.push(Diagnostic {
            severity: Severity::Warning,
            code: "legacy_root",
            message: "legacy root token `module` detected; parsing in compatibility mode"
                .to_string(),
            span: None,
            hint: Some("save from newer KiCad to normalize root token to `footprint`".to_string()),
        });
    }
    diagnostics
}

fn parse_ast(cst: &CstDocument) -> FootprintAst {
    let mut lib_id = None;
    let mut version = None;
    let mut tedit = None;
    let mut generator = None;
    let mut generator_version = None;
    let mut layer = None;
    let mut descr = None;
    let mut tags = None;
    let mut property_count = 0usize;
    let mut attr_present = false;
    let mut locked_present = false;
    let mut private_layers_present = false;
    let mut net_tie_pad_groups_present = false;
    let mut embedded_fonts_present = false;
    let mut has_embedded_files = false;
    let mut embedded_file_count = 0usize;
    let mut clearance = None;
    let mut solder_mask_margin = None;
    let mut solder_paste_margin = None;
    let mut solder_paste_margin_ratio = None;
    let mut duplicate_pad_numbers_are_jumpers = None;
    let mut pad_count = 0usize;
    let mut pads = Vec::new();
    let mut model_count = 0usize;
    let mut models = Vec::new();
    let mut zone_count = 0usize;
    let mut zones = Vec::new();
    let mut group_count = 0usize;
    let mut groups = Vec::new();
    let mut fp_line_count = 0usize;
    let mut fp_rect_count = 0usize;
    let mut fp_circle_count = 0usize;
    let mut fp_arc_count = 0usize;
    let mut fp_poly_count = 0usize;
    let mut fp_curve_count = 0usize;
    let mut fp_text_count = 0usize;
    let mut fp_text_box_count = 0usize;
    let mut dimension_count = 0usize;
    let mut graphic_count = 0usize;
    let mut graphics = Vec::new();
    let mut attr = Vec::new();
    let mut locked = false;
    let mut placed = false;
    let mut private_layers = Vec::new();
    let mut net_tie_pad_groups = Vec::new();
    let mut reference = None;
    let mut value = None;
    let mut properties = Vec::new();
    let mut unknown_nodes = Vec::new();

    if let Some(Node::List { items, .. }) = cst.nodes.first() {
        lib_id = items.get(1).and_then(atom_as_string);
        for item in items.iter().skip(2) {
            match head_of(item) {
                Some("version") => version = second_atom_i32(item),
                Some("tedit") => tedit = second_atom_string(item),
                Some("generator") => generator = second_atom_string(item),
                Some("generator_version") => generator_version = second_atom_string(item),
                Some("layer") => layer = second_atom_string(item),
                Some("descr") => descr = second_atom_string(item),
                Some("tags") => tags = second_atom_string(item),
                Some("property") => {
                    property_count += 1;
                    if let Node::List { items: props, .. } = item {
                        let key = props.get(1).and_then(atom_as_string);
                        let val = props.get(2).and_then(atom_as_string);
                        if let (Some(k), Some(v)) = (key.clone(), val.clone()) {
                            properties.push(FpProperty { key: k, value: v });
                        }
                        match key.as_deref() {
                            Some("Reference") => reference = val,
                            Some("Value") => value = val,
                            _ => {}
                        }
                    }
                }
                Some("attr") => {
                    attr_present = true;
                    if let Node::List { items: inner, .. } = item {
                        attr = inner.iter().skip(1).filter_map(atom_as_string).collect();
                    }
                }
                Some("locked") => {
                    locked_present = true;
                    locked = true;
                }
                Some("placed") => placed = true,
                Some("private_layers") => {
                    private_layers_present = true;
                    if let Node::List { items: inner, .. } = item {
                        private_layers = inner.iter().skip(1).filter_map(atom_as_string).collect();
                    }
                }
                Some("net_tie_pad_groups") => {
                    net_tie_pad_groups_present = true;
                    if let Node::List { items: inner, .. } = item {
                        for child in inner.iter().skip(1) {
                            if let Node::List { items: grp, .. } = child {
                                let group: Vec<String> =
                                    grp.iter().filter_map(atom_as_string).collect();
                                if !group.is_empty() {
                                    net_tie_pad_groups.push(group);
                                }
                            }
                        }
                    }
                }
                Some("embedded_fonts") => embedded_fonts_present = true,
                Some("embedded_files") => {
                    has_embedded_files = true;
                    embedded_file_count = list_child_head_count(item, "file");
                }
                Some("clearance") => clearance = second_atom_string(item),
                Some("solder_mask_margin") => solder_mask_margin = second_atom_string(item),
                Some("solder_paste_margin") => solder_paste_margin = second_atom_string(item),
                Some("solder_paste_margin_ratio") => {
                    solder_paste_margin_ratio = second_atom_string(item)
                }
                Some("duplicate_pad_numbers_are_jumpers") => {
                    duplicate_pad_numbers_are_jumpers =
                        second_atom_string(item).and_then(|s| match s.as_str() {
                            "yes" => Some(true),
                            "no" => Some(false),
                            _ => None,
                        })
                }
                Some("pad") => {
                    pad_count += 1;
                    pads.push(parse_fp_pad(item));
                }
                Some("model") => {
                    model_count += 1;
                    models.push(parse_fp_model(item));
                }
                Some("zone") => {
                    zone_count += 1;
                    zones.push(parse_fp_zone(item));
                }
                Some("group") => {
                    group_count += 1;
                    groups.push(parse_fp_group(item));
                }
                Some("fp_line") => {
                    fp_line_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_line"));
                }
                Some("fp_rect") => {
                    fp_rect_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_rect"));
                }
                Some("fp_circle") => {
                    fp_circle_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_circle"));
                }
                Some("fp_arc") => {
                    fp_arc_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_arc"));
                }
                Some("fp_poly") => {
                    fp_poly_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_poly"));
                }
                Some("fp_curve") => {
                    fp_curve_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_curve"));
                }
                Some("fp_text") => {
                    fp_text_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_text"));
                }
                Some("fp_text_box") => {
                    fp_text_box_count += 1;
                    graphic_count += 1;
                    graphics.push(parse_fp_graphic(item, "fp_text_box"));
                }
                Some("dimension") => dimension_count += 1,
                _ => {
                    if let Some(unknown) = UnknownNode::from_node(item) {
                        unknown_nodes.push(unknown);
                    }
                }
            }
        }
    }

    FootprintAst {
        lib_id,
        version,
        tedit,
        generator,
        generator_version,
        layer,
        descr,
        tags,
        property_count,
        attr_present,
        locked_present,
        private_layers_present,
        net_tie_pad_groups_present,
        embedded_fonts_present,
        has_embedded_files,
        embedded_file_count,
        clearance,
        solder_mask_margin,
        solder_paste_margin,
        solder_paste_margin_ratio,
        duplicate_pad_numbers_are_jumpers,
        pad_count,
        pads,
        model_count,
        models,
        zone_count,
        zones,
        group_count,
        groups,
        fp_line_count,
        fp_rect_count,
        fp_circle_count,
        fp_arc_count,
        fp_poly_count,
        fp_curve_count,
        fp_text_count,
        fp_text_box_count,
        dimension_count,
        graphic_count,
        graphics,
        attr,
        locked,
        placed,
        private_layers,
        net_tie_pad_groups,
        reference,
        value,
        properties,
        unknown_nodes,
    }
}

fn parse_fp_pad(node: &Node) -> FpPad {
    let Node::List { items, .. } = node else {
        return FpPad {
            number: None,
            pad_type: None,
            shape: None,
            at: None,
            rotation: None,
            size: None,
            layers: Vec::new(),
            net: None,
            drill: None,
            uuid: None,
            pin_function: None,
            pin_type: None,
            locked: false,
            property: None,
            remove_unused_layers: false,
            keep_end_layers: false,
            roundrect_rratio: None,
            chamfer_ratio: None,
            chamfer: Vec::new(),
            die_length: None,
            solder_mask_margin: None,
            solder_paste_margin: None,
            solder_paste_margin_ratio: None,
            clearance: None,
            zone_connect: None,
            thermal_width: None,
            thermal_gap: None,
            custom_clearance: None,
            custom_anchor: None,
            custom_primitives: 0,
        };
    };
    let number = items.get(1).and_then(atom_as_string);
    let pad_type = items.get(2).and_then(atom_as_string);
    let shape = items.get(3).and_then(atom_as_string);
    let mut at = None;
    let mut rotation = None;
    let mut size = None;
    let mut layers = Vec::new();
    let mut net = None;
    let mut drill = None;
    let mut uuid = None;
    let mut pin_function = None;
    let mut pin_type = None;
    let mut locked = items
        .iter()
        .any(|n| matches!(n, Node::Atom { atom: Atom::Symbol(s), .. } if s == "locked"));
    let mut property = None;
    let mut remove_unused_layers = false;
    let mut keep_end_layers = false;
    let mut roundrect_rratio = None;
    let mut chamfer_ratio = None;
    let mut chamfer = Vec::new();
    let mut die_length = None;
    let mut solder_mask_margin = None;
    let mut solder_paste_margin = None;
    let mut solder_paste_margin_ratio = None;
    let mut clearance = None;
    let mut zone_connect = None;
    let mut thermal_width = None;
    let mut thermal_gap = None;
    let mut custom_clearance = None;
    let mut custom_anchor = None;
    let mut custom_primitives = 0usize;

    for child in items.iter().skip(4) {
        match head_of(child) {
            Some("at") => {
                let (xy, rot) = parse_fp_xy_and_angle(child);
                at = xy;
                rotation = rot;
            }
            Some("size") => size = parse_fp_xy(child),
            Some("layers") => {
                if let Node::List { items: inner, .. } = child {
                    layers = inner.iter().skip(1).filter_map(atom_as_string).collect();
                }
            }
            Some("net") => {
                if let Node::List { items: inner, .. } = child {
                    net = Some(FpPadNet {
                        code: inner
                            .get(1)
                            .and_then(atom_as_string)
                            .and_then(|s| s.parse().ok()),
                        name: inner.get(2).and_then(atom_as_string),
                    });
                }
            }
            Some("drill") => drill = Some(parse_fp_drill(child)),
            Some("uuid") => uuid = second_atom_string(child),
            Some("pinfunction") => pin_function = second_atom_string(child),
            Some("pintype") => pin_type = second_atom_string(child),
            Some("locked") => locked = true,
            Some("property") => property = second_atom_string(child),
            Some("remove_unused_layer") | Some("remove_unused_layers") => {
                remove_unused_layers = true
            }
            Some("keep_end_layers") => keep_end_layers = true,
            Some("roundrect_rratio") => roundrect_rratio = second_atom_f64(child),
            Some("chamfer_ratio") => chamfer_ratio = second_atom_f64(child),
            Some("chamfer") => {
                if let Node::List { items: inner, .. } = child {
                    chamfer = inner.iter().skip(1).filter_map(atom_as_string).collect();
                }
            }
            Some("die_length") => die_length = second_atom_f64(child),
            Some("solder_mask_margin") => solder_mask_margin = second_atom_f64(child),
            Some("solder_paste_margin") => solder_paste_margin = second_atom_f64(child),
            Some("solder_paste_margin_ratio") => solder_paste_margin_ratio = second_atom_f64(child),
            Some("clearance") => clearance = second_atom_f64(child),
            Some("zone_connect") => {
                zone_connect = second_atom_string(child).and_then(|s| s.parse().ok())
            }
            Some("thermal_width") => thermal_width = second_atom_f64(child),
            Some("thermal_gap") => thermal_gap = second_atom_f64(child),
            Some("options") => {
                if let Node::List { items: inner, .. } = child {
                    for opt in inner.iter().skip(1) {
                        match head_of(opt) {
                            Some("clearance") => custom_clearance = second_atom_string(opt),
                            Some("anchor") => custom_anchor = second_atom_string(opt),
                            _ => {}
                        }
                    }
                }
            }
            Some("primitives") => {
                if let Node::List { items: inner, .. } = child {
                    custom_primitives = inner.len().saturating_sub(1);
                }
            }
            _ => {}
        }
    }
    FpPad {
        number,
        pad_type,
        shape,
        at,
        rotation,
        size,
        layers,
        net,
        drill,
        uuid,
        pin_function,
        pin_type,
        locked,
        property,
        remove_unused_layers,
        keep_end_layers,
        roundrect_rratio,
        chamfer_ratio,
        chamfer,
        die_length,
        solder_mask_margin,
        solder_paste_margin,
        solder_paste_margin_ratio,
        clearance,
        zone_connect,
        thermal_width,
        thermal_gap,
        custom_clearance,
        custom_anchor,
        custom_primitives,
    }
}

fn parse_fp_drill(node: &Node) -> FpPadDrill {
    let Node::List { items, .. } = node else {
        return FpPadDrill {
            shape: None,
            diameter: None,
            width: None,
            offset: None,
        };
    };
    let mut shape = None;
    let mut diameter = None;
    let mut width = None;
    let mut offset = None;
    for child in items.iter().skip(1) {
        match child {
            Node::List { .. } => {
                if matches!(head_of(child), Some("offset")) {
                    offset = parse_fp_xy(child);
                }
            }
            Node::Atom { .. } => {
                if let Some(value) = atom_as_f64(child) {
                    if diameter.is_none() {
                        diameter = Some(value);
                    } else if width.is_none() {
                        width = Some(value);
                    }
                } else if let Some(token) = atom_as_string(child) {
                    shape = Some(token);
                }
            }
        }
    }
    FpPadDrill {
        shape,
        diameter,
        width,
        offset,
    }
}

fn parse_fp_graphic(node: &Node, token: &str) -> FpGraphic {
    let mut layer = None;
    let mut text = None;
    let mut start = None;
    let mut end = None;
    let mut center = None;
    let mut uuid = None;
    let mut locked = false;
    let mut width = None;
    let mut stroke_type = None;
    let mut fill_type = None;
    let mut at = None;
    let mut angle = None;
    let mut font_size = None;
    let mut font_thickness = None;
    if let Node::List { items, .. } = node {
        if matches!(token, "fp_text" | "fp_text_box") {
            text = items.get(1).and_then(atom_as_string);
        }
        locked = items
            .iter()
            .any(|n| matches!(n, Node::Atom { atom: Atom::Symbol(s), .. } if s == "locked"));
        for child in items.iter().skip(1) {
            match head_of(child) {
                Some("layer") => layer = second_atom_string(child),
                Some("start") => start = parse_fp_xy(child),
                Some("end") => end = parse_fp_xy(child),
                Some("center") => center = parse_fp_xy(child),
                Some("uuid") => uuid = second_atom_string(child),
                Some("locked") => locked = true,
                Some("width") => width = second_atom_f64(child),
                Some("stroke") => {
                    if let Node::List { items: inner, .. } = child {
                        for s in inner.iter().skip(1) {
                            match head_of(s) {
                                Some("width") => width = second_atom_f64(s),
                                Some("type") => stroke_type = second_atom_string(s),
                                _ => {}
                            }
                        }
                    }
                }
                Some("fill") => {
                    if let Node::List { items: inner, .. } = child {
                        for f in inner.iter().skip(1) {
                            if head_of(f) == Some("type") {
                                fill_type = second_atom_string(f);
                            }
                        }
                    }
                }
                Some("at") => {
                    let (xy, rot) = parse_fp_xy_and_angle(child);
                    at = xy;
                    angle = rot;
                }
                Some("effects") => {
                    if let Node::List { items: inner, .. } = child {
                        for e in inner.iter().skip(1) {
                            if head_of(e) == Some("font") {
                                if let Node::List {
                                    items: font_items, ..
                                } = e
                                {
                                    for fi in font_items.iter().skip(1) {
                                        match head_of(fi) {
                                            Some("size") => font_size = parse_fp_xy(fi),
                                            Some("thickness") => {
                                                font_thickness = second_atom_f64(fi)
                                            }
                                            _ => {}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                _ => {}
            }
        }
    }
    FpGraphic {
        token: token.to_string(),
        layer,
        text,
        start,
        end,
        center,
        uuid,
        locked,
        width,
        stroke_type,
        fill_type,
        at,
        angle,
        font_size,
        font_thickness,
    }
}

fn parse_fp_model(node: &Node) -> FpModel {
    let Node::List { items, .. } = node else {
        return FpModel {
            path: None,
            at: None,
            scale: None,
            rotate: None,
            hide: false,
        };
    };
    let path = items.get(1).and_then(atom_as_string);
    let mut at = None;
    let mut scale = None;
    let mut rotate = None;
    let hide = items
        .iter()
        .any(|n| matches!(n, Node::Atom { atom: Atom::Symbol(s), .. } if s == "hide"));
    for child in items.iter().skip(2) {
        match head_of(child) {
            Some("at") | Some("offset") => at = parse_fp_model_xyz(child),
            Some("scale") => scale = parse_fp_model_xyz(child),
            Some("rotate") => rotate = parse_fp_model_xyz(child),
            _ => {}
        }
    }
    FpModel {
        path,
        at,
        scale,
        rotate,
        hide,
    }
}

fn parse_fp_model_xyz(node: &Node) -> Option<[f64; 3]> {
    let Node::List { items, .. } = node else {
        return None;
    };
    for child in items.iter().skip(1) {
        if head_of(child) == Some("xyz") {
            if let Node::List {
                items: xyz_items, ..
            } = child
            {
                let x = xyz_items.get(1).and_then(atom_as_f64)?;
                let y = xyz_items.get(2).and_then(atom_as_f64)?;
                let z = xyz_items.get(3).and_then(atom_as_f64)?;
                return Some([x, y, z]);
            }
        }
    }
    None
}

fn parse_fp_zone(node: &Node) -> FpZone {
    let mut net = None;
    let mut net_name = None;
    let mut name = None;
    let mut layer = None;
    let mut layers = Vec::new();
    let mut hatch = None;
    let mut fill_enabled = None;
    let mut polygon_count = 0usize;
    let mut filled_polygon_count = 0usize;
    let mut has_keepout = false;
    if let Node::List { items, .. } = node {
        for child in items.iter().skip(1) {
            match head_of(child) {
                Some("net") => net = second_atom_string(child).and_then(|s| s.parse().ok()),
                Some("net_name") => net_name = second_atom_string(child),
                Some("name") => name = second_atom_string(child),
                Some("layer") => layer = second_atom_string(child),
                Some("layers") => {
                    if let Node::List { items: inner, .. } = child {
                        layers = inner.iter().skip(1).filter_map(atom_as_string).collect();
                    }
                }
                Some("hatch") => hatch = second_atom_string(child),
                Some("fill") => fill_enabled = second_atom_bool(child),
                Some("polygon") => polygon_count += 1,
                Some("filled_polygon") => filled_polygon_count += 1,
                Some("keepout") => has_keepout = true,
                _ => {}
            }
        }
    }
    FpZone {
        net,
        net_name,
        name,
        layer,
        layers,
        hatch,
        fill_enabled,
        polygon_count,
        filled_polygon_count,
        has_keepout,
    }
}

fn parse_fp_group(node: &Node) -> FpGroup {
    let mut name = None;
    let mut group_id = None;
    let mut member_count = 0usize;
    if let Node::List { items, .. } = node {
        for child in items.iter().skip(1) {
            match head_of(child) {
                Some("name") => name = second_atom_string(child),
                Some("id") => group_id = second_atom_string(child),
                Some("members") => {
                    if let Node::List { items: inner, .. } = child {
                        member_count = inner.len().saturating_sub(1);
                    }
                }
                _ => {}
            }
        }
    }
    FpGroup {
        name,
        group_id,
        member_count,
    }
}

fn parse_fp_xy(node: &Node) -> Option<[f64; 2]> {
    let Node::List { items, .. } = node else {
        return None;
    };
    let x = items.get(1).and_then(atom_as_string)?.parse::<f64>().ok()?;
    let y = items.get(2).and_then(atom_as_string)?.parse::<f64>().ok()?;
    Some([x, y])
}

fn parse_fp_xy_and_angle(node: &Node) -> (Option<[f64; 2]>, Option<f64>) {
    let Node::List { items, .. } = node else {
        return (None, None);
    };
    let x = items.get(1).and_then(atom_as_f64);
    let y = items.get(2).and_then(atom_as_f64);
    let rot = items.get(3).and_then(atom_as_f64);
    match (x, y) {
        (Some(x), Some(y)) => (Some([x, y]), rot),
        _ => (None, rot),
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    use super::*;

    fn tmp_file(name: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock")
            .as_nanos();
        std::env::temp_dir().join(format!("{name}_{nanos}.kicad_mod"))
    }

    #[test]
    fn read_footprint_and_preserve_lossless() {
        let path = tmp_file("footprint_read_ok");
        let src = "(footprint \"R_0603\" (version 20260101) (generator pcbnew))\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.ast().lib_id.as_deref(), Some("R_0603"));
        assert_eq!(doc.ast().version, Some(20260101));
        assert_eq!(doc.ast().generator.as_deref(), Some("pcbnew"));
        assert!(doc.ast().unknown_nodes.is_empty());
        assert_eq!(doc.cst().to_lossless_string(), src);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn read_footprint_warns_on_future_version() {
        let path = tmp_file("footprint_future");
        fs::write(
            &path,
            "(footprint \"R\" (version 20270101) (generator pcbnew))\n",
        )
        .expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.diagnostics().len(), 1);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn read_footprint_warns_on_legacy_version() {
        let path = tmp_file("footprint_legacy");
        fs::write(
            &path,
            "(footprint \"R\" (version 20221018) (generator pcbnew))\n",
        )
        .expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.diagnostics().len(), 1);
        assert_eq!(doc.diagnostics()[0].code, "legacy_format");

        let _ = fs::remove_file(path);
    }

    #[test]
    fn read_footprint_accepts_legacy_module_root() {
        let path = tmp_file("footprint_module_root");
        let src = "(module R_0603 (layer F.Cu) (tedit 5F0C7995) (attr smd))\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.ast().lib_id.as_deref(), Some("R_0603"));
        assert_eq!(doc.ast().tedit.as_deref(), Some("5F0C7995"));
        assert!(doc.ast().attr_present);
        assert_eq!(doc.diagnostics().len(), 1);
        assert_eq!(doc.diagnostics()[0].code, "legacy_root");

        let _ = fs::remove_file(path);
    }

    #[test]
    fn read_footprint_captures_unknown_nodes() {
        let path = tmp_file("footprint_unknown");
        let src =
            "(footprint \"R\" (version 20260101) (generator pcbnew) (future_shape foo bar))\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.ast().unknown_nodes.len(), 1);
        assert_eq!(
            doc.ast().unknown_nodes[0].head.as_deref(),
            Some("future_shape")
        );

        let _ = fs::remove_file(path);
    }

    #[test]
    fn read_footprint_parses_top_level_counts() {
        let path = tmp_file("footprint_counts");
        let src = "(footprint \"X\" (version 20260101) (generator pcbnew) (generator_version \"10.0\") (layer \"F.Cu\")\n  (descr \"demo\")\n  (tags \"a b\")\n  (property \"Reference\" \"R?\")\n  (property \"Value\" \"X\")\n  (attr smd)\n  (private_layers \"In1.Cu\")\n  (net_tie_pad_groups \"1,2\")\n  (solder_mask_margin 0.02)\n  (solder_paste_margin -0.01)\n  (solder_paste_margin_ratio -0.2)\n  (duplicate_pad_numbers_are_jumpers yes)\n  (fp_text reference \"R1\" (at 0 0) (layer \"F.SilkS\"))\n  (fp_line (start 0 0) (end 1 1) (layer \"F.SilkS\"))\n  (pad \"1\" smd rect (at 0 0) (size 1 1) (layers \"F.Cu\" \"F.Mask\"))\n  (model \"foo.step\")\n  (zone)\n  (group (id \"g1\"))\n  (dimension)\n)\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.ast().lib_id.as_deref(), Some("X"));
        assert_eq!(doc.ast().generator_version.as_deref(), Some("10.0"));
        assert_eq!(doc.ast().layer.as_deref(), Some("F.Cu"));
        assert_eq!(doc.ast().property_count, 2);
        assert!(doc.ast().attr_present);
        assert!(!doc.ast().locked_present);
        assert!(doc.ast().private_layers_present);
        assert!(doc.ast().net_tie_pad_groups_present);
        assert!(!doc.ast().embedded_fonts_present);
        assert!(!doc.ast().has_embedded_files);
        assert_eq!(doc.ast().embedded_file_count, 0);
        assert_eq!(doc.ast().clearance, None);
        assert_eq!(doc.ast().solder_mask_margin.as_deref(), Some("0.02"));
        assert_eq!(doc.ast().solder_paste_margin.as_deref(), Some("-0.01"));
        assert_eq!(doc.ast().solder_paste_margin_ratio.as_deref(), Some("-0.2"));
        assert_eq!(doc.ast().duplicate_pad_numbers_are_jumpers, Some(true));
        assert_eq!(doc.ast().fp_text_count, 1);
        assert_eq!(doc.ast().fp_line_count, 1);
        assert_eq!(doc.ast().graphic_count, 2);
        assert_eq!(doc.ast().pad_count, 1);
        assert_eq!(doc.ast().model_count, 1);
        assert_eq!(doc.ast().zone_count, 1);
        assert_eq!(doc.ast().group_count, 1);
        assert_eq!(doc.ast().dimension_count, 1);
        assert!(doc.ast().unknown_nodes.is_empty());

        let _ = fs::remove_file(path);
    }

    #[test]
    fn parses_embedded_fonts_regression() {
        let path = tmp_file("footprint_embedded_fonts");
        let src = "(footprint \"X\" (version 20260101) (generator pcbnew) (embedded_fonts no))\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert!(doc.ast().embedded_fonts_present);
        assert!(doc.ast().unknown_nodes.is_empty());

        let _ = fs::remove_file(path);
    }

    #[test]
    fn parses_locked_regression() {
        let path = tmp_file("footprint_locked");
        let src = "(footprint \"X\" (locked) (version 20260101) (generator pcbnew))\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert!(doc.ast().locked_present);
        assert!(doc.ast().unknown_nodes.is_empty());

        let _ = fs::remove_file(path);
    }

    #[test]
    fn parses_solder_margins_and_jumpers_regression() {
        let path = tmp_file("footprint_margins_jumpers");
        let src = "(footprint \"X\" (version 20260101) (generator pcbnew)\n  (clearance 0.15)\n  (solder_mask_margin 0.03)\n  (solder_paste_margin -0.02)\n  (solder_paste_margin_ratio -0.3)\n  (duplicate_pad_numbers_are_jumpers no)\n)\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert_eq!(doc.ast().clearance.as_deref(), Some("0.15"));
        assert_eq!(doc.ast().solder_mask_margin.as_deref(), Some("0.03"));
        assert_eq!(doc.ast().solder_paste_margin.as_deref(), Some("-0.02"));
        assert_eq!(doc.ast().solder_paste_margin_ratio.as_deref(), Some("-0.3"));
        assert_eq!(doc.ast().duplicate_pad_numbers_are_jumpers, Some(false));
        assert!(doc.ast().unknown_nodes.is_empty());

        let _ = fs::remove_file(path);
    }

    #[test]
    fn parses_embedded_files_regression() {
        let path = tmp_file("footprint_embedded_files");
        let src = "(footprint \"X\" (version 20260101) (generator pcbnew)\n  (embedded_files\n    (file \"A\" \"base64\")\n    (file \"B\" \"base64\")\n  )\n)\n";
        fs::write(&path, src).expect("write fixture");

        let doc = FootprintFile::read(&path).expect("read");
        assert!(doc.ast().has_embedded_files);
        assert_eq!(doc.ast().embedded_file_count, 2);
        assert!(doc.ast().unknown_nodes.is_empty());

        let _ = fs::remove_file(path);
    }

    #[test]
    fn edit_roundtrip_updates_core_fields_and_properties() {
        let path = tmp_file("footprint_edit_input");
        let src = "(footprint \"Old\" (version 20241229) (generator pcbnew) (layer \"F.Cu\")\n  (property \"Reference\" \"R1\")\n  (property \"Value\" \"10k\")\n  (future_shape foo bar)\n)\n";
        fs::write(&path, src).expect("write fixture");

        let mut doc = FootprintFile::read(&path).expect("read");
        doc.set_lib_id("New_Footprint")
            .set_version(20260101)
            .set_generator("kiutils")
            .set_generator_version("dev")
            .set_layer("B.Cu")
            .set_descr("demo footprint")
            .set_tags("r c passives")
            .set_reference("R99")
            .set_value("22k")
            .upsert_property("LCSC", "C1234")
            .remove_property("DoesNotExist");

        let out = tmp_file("footprint_edit_output");
        doc.write(&out).expect("write");
        let written = fs::read_to_string(&out).expect("read out");
        assert!(written.contains("(future_shape foo bar)"));
        assert!(written.contains("(property \"LCSC\" \"C1234\")"));

        let reread = FootprintFile::read(&out).expect("reread");
        assert_eq!(reread.ast().lib_id.as_deref(), Some("New_Footprint"));
        assert_eq!(reread.ast().version, Some(20260101));
        assert_eq!(reread.ast().generator.as_deref(), Some("kiutils"));
        assert_eq!(reread.ast().generator_version.as_deref(), Some("dev"));
        assert_eq!(reread.ast().layer.as_deref(), Some("B.Cu"));
        assert_eq!(reread.ast().descr.as_deref(), Some("demo footprint"));
        assert_eq!(reread.ast().tags.as_deref(), Some("r c passives"));
        assert_eq!(reread.ast().property_count, 3);
        assert_eq!(reread.ast().unknown_nodes.len(), 1);

        let _ = fs::remove_file(path);
        let _ = fs::remove_file(out);
    }

    #[test]
    fn remove_property_roundtrip_removes_entry() {
        let path = tmp_file("footprint_remove_property");
        let src = "(footprint \"X\" (version 20260101) (generator pcbnew)\n  (property \"Reference\" \"R1\")\n  (property \"Value\" \"10k\")\n)\n";
        fs::write(&path, src).expect("write fixture");

        let mut doc = FootprintFile::read(&path).expect("read");
        doc.remove_property("Value");

        let out = tmp_file("footprint_remove_property_out");
        doc.write(&out).expect("write");
        let reread = FootprintFile::read(&out).expect("reread");
        assert_eq!(reread.ast().property_count, 1);

        let _ = fs::remove_file(path);
        let _ = fs::remove_file(out);
    }

    #[test]
    fn no_op_setter_keeps_lossless_raw_unchanged() {
        let path = tmp_file("footprint_noop_setter");
        let src = "(footprint \"X\" (version   20260101) (generator pcbnew))\n";
        fs::write(&path, src).expect("write fixture");

        let mut doc = FootprintFile::read(&path).expect("read");
        doc.set_version(20260101);

        let out = tmp_file("footprint_noop_setter_out");
        doc.write(&out).expect("write");
        let written = fs::read_to_string(&out).expect("read out");
        assert_eq!(written, src);

        let _ = fs::remove_file(path);
        let _ = fs::remove_file(out);
    }
}