altium-format 0.1.7

Core altium-cli library for reading and writing Altium Designer files.
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
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Alexander Kiselev <alex@akiselev.com>
//
//! Schematic document editing commands.
//!
//! This module contains editing-related commands for schematic documents that
//! modify the document content. Extracted from schdoc.rs for modularity.

use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::edit::{EditSession, Orientation};
use crate::ops::output::*;
use crate::records::sch::{PortIoType, PowerObjectStyle, SchRecord, TextOrientations};
use crate::types::{Coord, CoordPoint, Unit};

/// Parse a unit value or interpret as mils.
fn parse_unit_value_or_mil(s: &str) -> Result<f64, String> {
    let s = s.trim();

    // Try parsing with unit suffix first
    if let Ok((coord, unit)) = Unit::parse_with_unit(s) {
        // If the parser detected a unit suffix (not DxpDefault which means no suffix), use it
        if unit != Unit::DxpDefault {
            return Ok(coord.to_mils());
        }
    }

    // Plain number (no unit suffix) - interpret as mils
    s.parse::<f64>().map_err(|_| {
        format!(
            "Invalid value '{}': expected number with optional unit (e.g., '100mil', '2.54mm')",
            s
        )
    })
}

/// Create a new empty schematic document.
pub fn cmd_new(output: Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
    let session = EditSession::new();

    match output {
        Some(path) => {
            session.doc.save_to_file(&path)?;
            println!("Created new schematic: {}", path.display());
        }
        None => {
            println!("Created new empty schematic in memory.");
            println!("Use --output to save to a file.");
        }
    }

    Ok(())
}

/// Validate a schematic document.
pub fn cmd_validate(path: &Path) -> Result<SchDocValidationResult, Box<dyn std::error::Error>> {
    let session = EditSession::open(path)?;

    let validation_errors = session.validate();

    let errors = validation_errors
        .iter()
        .map(|e| ValidationError {
            kind: format!("{:?}", e.kind),
            message: e.message.clone(),
            location: e.location.map(|l| (l.x.to_mils(), l.y.to_mils())),
            components: e.components.clone(),
        })
        .collect();

    Ok(SchDocValidationResult {
        path: path.display().to_string(),
        is_valid: validation_errors.is_empty(),
        errors,
    })
}

/// Add a component from a library to the schematic.
#[allow(clippy::too_many_arguments)]
pub fn cmd_add_component(
    path: &Path,
    library: &Path,
    component: &str,
    x: &str,
    y: &str,
    designator: Option<&str>,
    rotation: i32,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    session.load_library(library)?;

    let location = CoordPoint::from_mils(x_mils, y_mils);
    let orientation = match rotation {
        0 => Orientation::Normal,
        90 => Orientation::Rotated90,
        180 => Orientation::Rotated180,
        270 => Orientation::Rotated270,
        _ => return Err(format!("Invalid rotation: {}. Use 0, 90, 180, or 270.", rotation).into()),
    };

    let _index = session.add_component(component, location, orientation, designator)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    let des = designator.unwrap_or("(auto)");
    println!(
        "Added component {} at ({:.0}, {:.0}) mils as {}",
        component, x_mils, y_mils, des
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Suggest placement locations for a component.
pub fn cmd_suggest_placement(
    path: &Path,
    library: &Path,
    component: &str,
    near: Option<String>,
    json: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    session.load_library(library)?;

    let suggestions = session.suggest_component_placement(component, near.as_deref());

    if json {
        #[derive(Serialize)]
        struct Suggestion {
            x: f64,
            y: f64,
            rotation: i32,
            score: f64,
            reason: String,
        }

        let result: Vec<_> = suggestions
            .iter()
            .map(|s| Suggestion {
                x: s.location.x.to_mils(),
                y: s.location.y.to_mils(),
                rotation: (s.orientation.rotation_degrees() as i32) % 360,
                score: s.score,
                reason: s.reason.clone(),
            })
            .collect();

        println!(
            "{}",
            serde_json::to_string_pretty(&result)
                .map_err(|e| format!("JSON serialization error: {}", e))?
        );
    } else {
        println!("Placement suggestions for '{}':", component);
        if suggestions.is_empty() {
            println!("  No suitable locations found.");
        } else {
            for (i, s) in suggestions.iter().enumerate() {
                println!(
                    "  {}. ({:.0}, {:.0}) mils - score: {:.2} - {}",
                    i + 1,
                    s.location.x.to_mils(),
                    s.location.y.to_mils(),
                    s.score,
                    s.reason
                );
            }
        }
    }

    Ok(())
}

/// Move a component to a new location.
pub fn cmd_move_component(
    path: &Path,
    designator: &str,
    x: &str,
    y: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    // Find component by designator
    let components = session
        .layout()
        .get_placed_components(&session.doc.primitives);
    let comp = components
        .iter()
        .find(|c| c.designator == designator)
        .ok_or_else(|| format!("Component not found: {}", designator))?;

    let index = comp.index;
    let new_location = CoordPoint::from_mils(x_mils, y_mils);

    session.move_component(index, new_location)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!(
        "Moved {} to ({:.0}, {:.0}) mils",
        designator, x_mils, y_mils
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Delete a component from the schematic.
pub fn cmd_delete_component(
    path: &Path,
    designator: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    // Find component by designator
    let components = session
        .layout()
        .get_placed_components(&session.doc.primitives);
    let comp = components
        .iter()
        .find(|c| c.designator == designator)
        .ok_or_else(|| format!("Component not found: {}", designator))?;

    let index = comp.index;

    session.delete_component(index)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Deleted component {}", designator);
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Add a wire with specified vertices.
pub fn cmd_add_wire(
    path: &Path,
    vertices_str: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    // Parse vertices from string
    let values: Vec<f64> = vertices_str
        .split(',')
        .map(|s| s.trim().parse::<f64>())
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| format!("Invalid vertex format: {}", e))?;

    if values.len() < 4 || values.len() % 2 != 0 {
        return Err(
            "Vertices must be pairs of X,Y coordinates (at least 2 points)"
                .to_string()
                .into(),
        );
    }

    let vertices: Vec<CoordPoint> = values
        .chunks(2)
        .map(|chunk| CoordPoint::from_mils(chunk[0], chunk[1]))
        .collect();

    session.add_wire(&vertices)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Added wire with {} vertices", vertices.len());
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Parse a point specification for routing targets.
fn parse_point_spec(spec: &str, session: &EditSession) -> Result<(CoordPoint, String), String> {
    // Try parsing as coordinates first (x,y format)
    if let Some((x_str, y_str)) = spec.split_once(',') {
        let x: f64 = x_str.trim().parse().map_err(|_| {
            format!(
                "Invalid x coordinate in '{}'. Use: Component.Pin, :Port, %NetLabel, @Power, or x,y",
                spec
            )
        })?;
        let y: f64 = y_str.trim().parse().map_err(|_| {
            format!(
                "Invalid y coordinate in '{}'. Use: Component.Pin, :Port, %NetLabel, @Power, or x,y",
                spec
            )
        })?;
        let point = CoordPoint::from_mils(x, y);
        return Ok((point, format!("({:.0}, {:.0})", x, y)));
    }

    // Try parsing as port reference (:PortName format)
    if let Some(port_name) = spec.strip_prefix(':') {
        let ports: Vec<_> = session
            .doc
            .primitives
            .iter()
            .filter_map(|p| match p {
                SchRecord::Port(port) => Some(port),
                _ => None,
            })
            .collect();

        let port = ports.iter().find(|p| p.name == port_name).ok_or_else(|| {
            format!(
                "Port not found: '{}'. Available ports: {:?}",
                port_name,
                ports.iter().map(|p| &p.name).collect::<Vec<_>>()
            )
        })?;

        let point = CoordPoint::new(
            Coord::from_raw(port.graphical.location_x),
            Coord::from_raw(port.graphical.location_y),
        );
        return Ok((point, format!(":{}", port_name)));
    }

    // Try parsing as net label reference (%NetLabel format)
    if let Some(label_name) = spec.strip_prefix('%') {
        let labels: Vec<_> = session
            .doc
            .primitives
            .iter()
            .filter_map(|p| match p {
                SchRecord::NetLabel(label) => Some(label),
                _ => None,
            })
            .collect();

        let label = labels
            .iter()
            .find(|l| l.label.text == label_name)
            .ok_or_else(|| {
                format!(
                    "Net label not found: '{}'. Available net labels: {:?}",
                    label_name,
                    labels.iter().map(|l| &l.label.text).collect::<Vec<_>>()
                )
            })?;

        let point = CoordPoint::new(
            Coord::from_raw(label.label.graphical.location_x),
            Coord::from_raw(label.label.graphical.location_y),
        );
        return Ok((point, format!("%{}", label_name)));
    }

    // Try parsing as power port reference (@PowerPort format)
    if let Some(power_name) = spec.strip_prefix('@') {
        let powers: Vec<_> = session
            .doc
            .primitives
            .iter()
            .filter_map(|p| match p {
                SchRecord::PowerObject(power) => Some(power),
                _ => None,
            })
            .collect();

        let power = powers
            .iter()
            .find(|p| p.text == power_name)
            .ok_or_else(|| {
                format!(
                    "Power port not found: '{}'. Available power ports: {:?}",
                    power_name,
                    powers.iter().map(|p| &p.text).collect::<Vec<_>>()
                )
            })?;

        let point = CoordPoint::new(
            Coord::from_raw(power.graphical.location_x),
            Coord::from_raw(power.graphical.location_y),
        );
        return Ok((point, format!("@{}", power_name)));
    }

    // Try parsing as Component.Pin reference
    if let Some((component, pin)) = spec.split_once('.') {
        let components = session
            .layout()
            .get_placed_components(&session.doc.primitives);
        let comp = components
            .iter()
            .find(|c| c.designator == component)
            .ok_or_else(|| {
                format!(
                    "Component not found: '{}'. Available components: {:?}",
                    component,
                    components.iter().map(|c| &c.designator).collect::<Vec<_>>()
                )
            })?;

        let pin_loc = comp
            .pin_locations
            .iter()
            .find(|p| p.designator == pin || p.name == pin)
            .ok_or_else(|| {
                format!(
                    "Pin '{}' not found on component '{}'. Available pins: {:?}",
                    pin,
                    component,
                    comp.pin_locations
                        .iter()
                        .map(|p| format!("{}/{}", p.designator, p.name))
                        .collect::<Vec<_>>()
                )
            })?;

        return Ok((pin_loc.location, format!("{}.{}", component, pin)));
    }

    Err(format!(
        "Invalid point specification: '{}'. Expected: Component.Pin, :Port, %NetLabel, @Power, or x,y",
        spec
    ))
}

/// Route a wire between two points.
pub fn cmd_route_wire(
    path: &Path,
    from: &str,
    to: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    let (start, from_desc) = parse_point_spec(from, &session)?;
    let (end, to_desc) = parse_point_spec(to, &session)?;

    session.route_wire(start, end)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Routed wire from {} to {}", from_desc, to_desc);
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Connect two pins with a wire.
pub fn cmd_connect_pins(
    path: &Path,
    from_component: &str,
    from_pin: &str,
    to_component: &str,
    to_pin: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    session.connect_pins(from_component, from_pin, to_component, to_pin)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!(
        "Connected {}.{} to {}.{}",
        from_component, from_pin, to_component, to_pin
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Delete a wire by index.
pub fn cmd_delete_wire(
    path: &Path,
    index: usize,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    session.delete_wire(index)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Deleted wire at index {}", index);
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Add a net label at a location.
pub fn cmd_add_net_label(
    path: &Path,
    name: &str,
    x: &str,
    y: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    let location = CoordPoint::from_mils(x_mils, y_mils);

    session.add_net_label(name, location)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!(
        "Added net label '{}' at ({:.0}, {:.0}) mils",
        name, x_mils, y_mils
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Smart-wire: create a wire stub from a pin and attach a net label or power port.
pub fn cmd_smart_wire(
    path: &Path,
    component: &str,
    pin: &str,
    net: &str,
    power_style: Option<&str>,
    wire_length_mils: f64,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    let style = match power_style {
        Some(s) => {
            let parsed = match s.to_lowercase().as_str() {
                "bar" | "power_bar" => PowerObjectStyle::Bar,
                "arrow" => PowerObjectStyle::Arrow,
                "wave" => PowerObjectStyle::Wave,
                "ground" | "gnd" => PowerObjectStyle::Ground,
                "power_ground" | "pgnd" => PowerObjectStyle::PowerGround,
                "signal_ground" | "sgnd" => PowerObjectStyle::SignalGround,
                "earth_ground" | "earth" => PowerObjectStyle::EarthGround,
                "circle" => PowerObjectStyle::Circle,
                _ => {
                    return Err(format!(
                        "Unknown power style: {}. Use: bar, arrow, wave, ground, power_ground, signal_ground, earth_ground, circle",
                        s
                    ).into());
                }
            };
            Some(parsed)
        }
        None => None,
    };

    let (wire_idx, label_idx) = session.smart_wire_pin(
        component,
        pin,
        net,
        style,
        wire_length_mils,
    )?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    let kind = if power_style.is_some() { "power port" } else { "net label" };
    println!(
        "Smart-wired {}.{} -> {} '{}' (wire #{}, {} #{})",
        component, pin, kind, net, wire_idx, kind, label_idx
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Batch smart-wire: wire multiple pins from a mapping string.
///
/// Format: "COMP.PIN=NET,COMP.PIN=NET:power_style,..."
/// Examples: "U1.3=VCC:bar,U1.4=GND:ground,U1.5=SDA,U1.6=SCL"
pub fn cmd_smart_wire_batch(
    path: &Path,
    mappings: &str,
    wire_length_mils: f64,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    let mut count = 0;
    for mapping in mappings.split(',') {
        let mapping = mapping.trim();
        if mapping.is_empty() {
            continue;
        }

        // Parse "COMP.PIN=NET" or "COMP.PIN=NET:power_style"
        let (pin_spec, net_spec) = mapping
            .split_once('=')
            .ok_or_else(|| format!("Invalid mapping '{}': expected COMP.PIN=NET", mapping))?;

        let (component, pin) = pin_spec
            .split_once('.')
            .ok_or_else(|| format!("Invalid pin spec '{}': expected COMP.PIN", pin_spec))?;

        let (net, power_style) = if let Some((n, s)) = net_spec.split_once(':') {
            let parsed = match s.to_lowercase().as_str() {
                "bar" | "power_bar" => PowerObjectStyle::Bar,
                "arrow" => PowerObjectStyle::Arrow,
                "wave" => PowerObjectStyle::Wave,
                "ground" | "gnd" => PowerObjectStyle::Ground,
                "power_ground" | "pgnd" => PowerObjectStyle::PowerGround,
                "signal_ground" | "sgnd" => PowerObjectStyle::SignalGround,
                "earth_ground" | "earth" => PowerObjectStyle::EarthGround,
                "circle" => PowerObjectStyle::Circle,
                _ => {
                    return Err(format!(
                        "Unknown power style '{}' in mapping '{}'. Use: bar, arrow, wave, ground, power_ground, signal_ground, earth_ground, circle",
                        s, mapping
                    ).into());
                }
            };
            (n, Some(parsed))
        } else {
            (net_spec, None)
        };

        session.smart_wire_pin(component, pin, net, power_style, wire_length_mils)?;
        count += 1;

        let kind = if power_style.is_some() { "power" } else { "net" };
        println!("  {}.{} -> {} '{}'", component, pin, kind, net);
    }

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Smart-wired {} pins. Saved to: {}", count, output_path.display());

    Ok(())
}

/// Add a power port.
pub fn cmd_add_power(
    path: &Path,
    name: &str,
    x: &str,
    y: &str,
    style: &str,
    orientation: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    let location = CoordPoint::from_mils(x_mils, y_mils);

    let power_style = match style.to_lowercase().as_str() {
        "bar" | "power_bar" => PowerObjectStyle::Bar,
        "arrow" => PowerObjectStyle::Arrow,
        "wave" => PowerObjectStyle::Wave,
        "ground" | "gnd" => PowerObjectStyle::Ground,
        "power_ground" | "pgnd" => PowerObjectStyle::PowerGround,
        "signal_ground" | "sgnd" => PowerObjectStyle::SignalGround,
        "earth_ground" | "earth" => PowerObjectStyle::EarthGround,
        "circle" => PowerObjectStyle::Circle,
        _ => {
            return Err(format!(
                "Unknown power style: {}. Use: bar, arrow, wave, ground, power_ground, signal_ground, earth_ground, circle",
                style
            )
            .into())
        }
    };

    // TextOrientations is a bitflags struct: NONE=0, ROTATED=1, FLIPPED=2
    let orient = match orientation.to_lowercase().as_str() {
        "up" | "0" => TextOrientations::NONE,
        "left" | "90" => TextOrientations::ROTATED,
        "down" | "180" => TextOrientations::FLIPPED,
        "right" | "270" => TextOrientations::ROTATED | TextOrientations::FLIPPED,
        _ => {
            return Err(format!(
                "Unknown orientation: {}. Use: up, down, left, right",
                orientation
            )
            .into());
        }
    };

    session.add_power_port(name, location, power_style, orient)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!(
        "Added power port '{}' ({}) at ({:.0}, {:.0}) mils",
        name, style, x_mils, y_mils
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Add a junction at a location.
pub fn cmd_add_junction(
    path: &Path,
    x: &str,
    y: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    let location = CoordPoint::from_mils(x_mils, y_mils);

    session.add_junction(location)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Added junction at ({:.0}, {:.0}) mils", x_mils, y_mils);
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Add missing junctions where wires cross.
pub fn cmd_add_missing_junctions(
    path: &Path,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut session = EditSession::open(path)?;

    let count = session.add_missing_junctions()?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!("Added {} missing junction(s)", count);
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Add a port at a location.
pub fn cmd_add_port(
    path: &Path,
    name: &str,
    x: &str,
    y: &str,
    io_type: &str,
    output: Option<PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    let x_mils = parse_unit_value_or_mil(x)?;
    let y_mils = parse_unit_value_or_mil(y)?;

    let mut session = EditSession::open(path)?;

    let location = CoordPoint::from_mils(x_mils, y_mils);

    let port_io_type = match io_type.to_lowercase().as_str() {
        "input" | "in" => PortIoType::Input,
        "output" | "out" => PortIoType::Output,
        "bidirectional" | "bidir" | "inout" => PortIoType::Bidirectional,
        "unspecified" | "none" => PortIoType::Unspecified,
        _ => {
            return Err(format!(
                "Unknown I/O type: {}. Use: input, output, bidirectional, unspecified",
                io_type
            )
            .into());
        }
    };

    session.add_port(name, location, port_io_type)?;

    let output_path = output.as_deref().unwrap_or(path);
    session.save(output_path)?;

    println!(
        "Added port '{}' ({}) at ({:.0}, {:.0}) mils",
        name, io_type, x_mils, y_mils
    );
    println!("Saved to: {}", output_path.display());

    Ok(())
}

/// Show the netlist for a schematic.
pub fn cmd_show_netlist(path: &Path, filter_net: Option<&str>, json: bool) -> Result<(), String> {
    let session = EditSession::open(path).map_err(|e| format!("Failed to open: {:?}", e))?;

    let netlist = session.build_netlist();

    if json {
        #[derive(Serialize)]
        struct NetJson {
            name: String,
            named: bool,
            pins: Vec<(String, String)>,
        }

        let nets: Vec<_> = netlist
            .nets
            .iter()
            .filter(|n| filter_net.map(|f| n.name.contains(f)).unwrap_or(true))
            .map(|n| NetJson {
                name: n.name.clone(),
                named: n.named,
                pins: n
                    .pins()
                    .iter()
                    .map(|(c, p)| (c.to_string(), p.to_string()))
                    .collect(),
            })
            .collect();

        println!(
            "{}",
            serde_json::to_string_pretty(&nets)
                .map_err(|e| format!("JSON serialization error: {}", e))?
        );
    } else {
        println!("Netlist ({} nets):", netlist.nets.len());
        println!();

        for net in &netlist.nets {
            if let Some(filter) = filter_net {
                if !net.name.contains(filter) {
                    continue;
                }
            }

            let pins = net.pins();
            let named_str = if net.named { "" } else { " (auto)" };
            println!("  {} [{}]{}", net.name, pins.len(), named_str);

            for (comp, pin) in pins {
                println!("    - {}.{}", comp, pin);
            }
        }
    }

    Ok(())
}

/// Find unconnected pins in the schematic.
pub fn cmd_find_unconnected(
    path: &Path,
) -> Result<SchDocUnconnectedPins, Box<dyn std::error::Error>> {
    let session = EditSession::open(path)?;

    let unconnected = session.find_unconnected_pins();

    let pins = unconnected
        .iter()
        .map(|(c, p, l)| UnconnectedPin {
            component: c.clone(),
            pin: p.clone(),
            x: l.0 as f64 / 10000.0,
            y: l.1 as f64 / 10000.0,
        })
        .collect();

    Ok(SchDocUnconnectedPins {
        path: path.display().to_string(),
        total_unconnected: unconnected.len(),
        pins,
    })
}

/// Find missing junctions in the schematic.
pub fn cmd_find_missing_junctions(
    path: &Path,
) -> Result<SchDocMissingJunctions, Box<dyn std::error::Error>> {
    let session = EditSession::open(path)?;

    let missing = session.find_missing_junctions();

    let locations: Vec<(f64, f64)> = missing
        .iter()
        .map(|l| (l.0 as f64 / 10000.0, l.1 as f64 / 10000.0))
        .collect();

    Ok(SchDocMissingJunctions {
        path: path.display().to_string(),
        total_missing: missing.len(),
        locations,
    })
}

/// Search a schematic library for components matching a pattern.
pub fn cmd_search_library(
    library: &Path,
    pattern: &str,
) -> Result<SchDocLibrarySearchResults, Box<dyn std::error::Error>> {
    use crate::io::SchLib;

    let lib = SchLib::open_file(library)?;

    let pattern_lower = pattern.to_lowercase();
    let matches: Vec<_> = lib
        .iter()
        .filter(|c| {
            c.name().to_lowercase().contains(&pattern_lower)
                || c.description().to_lowercase().contains(&pattern_lower)
        })
        .collect();

    let match_results: Vec<LibraryComponentMatch> = matches
        .iter()
        .map(|c| LibraryComponentMatch {
            name: c.name().to_string(),
            description: c.description().to_string(),
            pins: c.pin_count(),
        })
        .collect();

    Ok(SchDocLibrarySearchResults {
        library: library.display().to_string(),
        pattern: pattern.to_string(),
        total_matches: matches.len(),
        matches: match_results,
    })
}

/// List components in a schematic library.
pub fn cmd_list_library(
    library: &Path,
    verbose: bool,
) -> Result<SchDocLibraryList, Box<dyn std::error::Error>> {
    use crate::io::SchLib;

    let lib = SchLib::open_file(library)?;

    let components: Vec<LibraryComponentInfo> = lib
        .iter()
        .map(|c| LibraryComponentInfo {
            name: c.name().to_string(),
            description: c.description().to_string(),
            pins: c.pin_count(),
            primitives: if verbose {
                Some(c.primitive_count())
            } else {
                None
            },
        })
        .collect();

    Ok(SchDocLibraryList {
        library: library.display().to_string(),
        total_components: lib.component_count(),
        components,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ops::schlib::{cmd_create as schlib_create, cmd_gen_ic};
    use std::path::PathBuf;

    fn temp_path(ext: &str) -> PathBuf {
        let id = uuid::Uuid::new_v4();
        std::env::temp_dir().join(format!("test_{}.{}", id, ext))
    }

    /// Helper: create a SchLib with a simple 3-pin IC.
    fn create_test_library() -> PathBuf {
        let lib_path = temp_path("SchLib");
        schlib_create(&lib_path).unwrap();
        cmd_gen_ic(
            &lib_path,
            "LDO_3PIN",
            "1:VIN:power:left,2:VOUT:power:right,3:GND:power:bottom",
            Some("Test LDO".to_string()),
            "600mil",
            "200mil",
            "100mil",
        )
        .unwrap();
        cmd_gen_ic(
            &lib_path,
            "IC_4PIN",
            "1:VCC:power:top,2:IN:input:left,3:OUT:output:right,4:GND:power:bottom",
            Some("Test IC".to_string()),
            "400mil",
            "200mil",
            "100mil",
        )
        .unwrap();
        lib_path
    }

    /// E2E: Create schematic, add component, verify designator survives save/reload.
    #[test]
    fn test_add_component_designator_roundtrip() {
        let lib_path = create_test_library();
        let sch_path = temp_path("SchDoc");

        // Create empty SchDoc
        crate::ops::schdoc::cmd_create(&sch_path, None).unwrap();

        // Add component with explicit designator
        cmd_add_component(
            &sch_path,
            &lib_path,
            "LDO_3PIN",
            "1000",
            "2000",
            Some("U1"),
            0,
            None,
        )
        .unwrap();

        // Reload and check designator
        let doc = crate::io::SchDoc::open_file(&sch_path).unwrap();

        // Must have exactly 1 component
        let components: Vec<_> = doc
            .primitives
            .iter()
            .filter(|r| matches!(r, SchRecord::Component(_)))
            .collect();
        assert_eq!(components.len(), 1, "Expected 1 component");

        // Must have exactly 1 Designator record (not Parameter)
        let designators: Vec<_> = doc
            .primitives
            .iter()
            .filter(|r| matches!(r, SchRecord::Designator(_)))
            .collect();
        assert_eq!(
            designators.len(),
            1,
            "Expected 1 Designator record, found {}. \
             Designator may be serializing as Parameter (RECORD=41 vs 34).",
            designators.len()
        );

        if let SchRecord::Designator(d) = &designators[0] {
            assert_eq!(d.text(), "U1", "Designator text must be U1");
        }

        std::fs::remove_file(&sch_path).ok();
        std::fs::remove_file(&lib_path).ok();
    }

    /// E2E: Multiple components get distinct designators.
    #[test]
    fn test_multiple_components_distinct_designators() {
        let lib_path = create_test_library();
        let sch_path = temp_path("SchDoc");

        crate::ops::schdoc::cmd_create(&sch_path, None).unwrap();

        cmd_add_component(
            &sch_path, &lib_path, "LDO_3PIN", "1000", "2000",
            Some("U1"), 0, None,
        ).unwrap();
        cmd_add_component(
            &sch_path, &lib_path, "IC_4PIN", "3000", "2000",
            Some("U2"), 0, None,
        ).unwrap();
        cmd_add_component(
            &sch_path, &lib_path, "LDO_3PIN", "5000", "2000",
            Some("U3"), 0, None,
        ).unwrap();

        let doc = crate::io::SchDoc::open_file(&sch_path).unwrap();

        let designators: Vec<String> = doc
            .primitives
            .iter()
            .filter_map(|r| {
                if let SchRecord::Designator(d) = r {
                    Some(d.text().to_string())
                } else {
                    None
                }
            })
            .collect();

        assert_eq!(designators.len(), 3, "Expected 3 designators");
        assert!(designators.contains(&"U1".to_string()));
        assert!(designators.contains(&"U2".to_string()));
        assert!(designators.contains(&"U3".to_string()));

        std::fs::remove_file(&sch_path).ok();
        std::fs::remove_file(&lib_path).ok();
    }

    /// E2E: Components with top/bottom pins have correct pin count after placement.
    #[test]
    fn test_placed_component_preserves_all_pins() {
        let lib_path = create_test_library();
        let sch_path = temp_path("SchDoc");

        crate::ops::schdoc::cmd_create(&sch_path, None).unwrap();

        // IC_4PIN has pins on all 4 sides
        cmd_add_component(
            &sch_path, &lib_path, "IC_4PIN", "2000", "2000",
            Some("U1"), 0, None,
        ).unwrap();

        let doc = crate::io::SchDoc::open_file(&sch_path).unwrap();

        // Count pins owned by the component (owner_index = component index)
        let comp_index = doc
            .primitives
            .iter()
            .position(|r| matches!(r, SchRecord::Component(_)))
            .unwrap();

        let pin_count = doc
            .primitives
            .iter()
            .filter(|r| {
                if let SchRecord::Pin(p) = r {
                    p.graphical.base.owner_index == comp_index as i32
                } else {
                    false
                }
            })
            .count();

        assert_eq!(
            pin_count, 4,
            "IC_4PIN has 4 pins (top/bottom/left/right), but only {} survived placement",
            pin_count
        );

        std::fs::remove_file(&sch_path).ok();
        std::fs::remove_file(&lib_path).ok();
    }

    /// E2E: Power ports and net labels survive save/reload.
    #[test]
    fn test_power_and_netlabel_roundtrip() {
        let sch_path = temp_path("SchDoc");
        crate::ops::schdoc::cmd_create(&sch_path, None).unwrap();

        // Add power ports
        cmd_add_power(
            &sch_path, "3V3", "1000", "2000", "bar", "up", None,
        ).unwrap();
        cmd_add_power(
            &sch_path, "GND", "1000", "1000", "ground", "down", None,
        ).unwrap();

        // Add net label
        cmd_add_net_label(
            &sch_path, "SDA", "2000", "2000", None,
        ).unwrap();

        let doc = crate::io::SchDoc::open_file(&sch_path).unwrap();

        let power_count = doc
            .primitives
            .iter()
            .filter(|r| matches!(r, SchRecord::PowerObject(_)))
            .count();
        assert_eq!(power_count, 2, "Expected 2 power ports");

        let net_labels: Vec<_> = doc
            .primitives
            .iter()
            .filter_map(|r| {
                if let SchRecord::NetLabel(nl) = r {
                    Some(nl.label.text.clone())
                } else {
                    None
                }
            })
            .collect();
        assert_eq!(net_labels.len(), 1);
        assert_eq!(net_labels[0], "SDA");

        std::fs::remove_file(&sch_path).ok();
    }

    /// E2E: Full workflow — library creation, component placement, designator lookup.
    #[test]
    fn test_full_schematic_capture_workflow() {
        let lib_path = create_test_library();
        let sch_path = temp_path("SchDoc");

        // Step 1: Create schematic
        crate::ops::schdoc::cmd_create(&sch_path, None).unwrap();

        // Step 2: Place components
        cmd_add_component(
            &sch_path, &lib_path, "LDO_3PIN", "1000", "3000",
            Some("U1"), 0, None,
        ).unwrap();
        cmd_add_component(
            &sch_path, &lib_path, "IC_4PIN", "3000", "3000",
            Some("U2"), 0, None,
        ).unwrap();

        // Step 3: Add power ports
        cmd_add_power(
            &sch_path, "3V3", "2000", "4000", "bar", "up", None,
        ).unwrap();
        cmd_add_power(
            &sch_path, "GND", "2000", "2000", "ground", "down", None,
        ).unwrap();

        // Step 4: Add net labels
        cmd_add_net_label(
            &sch_path, "VIN_3V3", "500", "3000", None,
        ).unwrap();

        // Step 5: Validate — reload and verify structure
        let doc = crate::io::SchDoc::open_file(&sch_path).unwrap();

        let comp_count = doc.primitives.iter()
            .filter(|r| matches!(r, SchRecord::Component(_))).count();
        let des_count = doc.primitives.iter()
            .filter(|r| matches!(r, SchRecord::Designator(_))).count();
        let power_count = doc.primitives.iter()
            .filter(|r| matches!(r, SchRecord::PowerObject(_))).count();
        let net_count = doc.primitives.iter()
            .filter(|r| matches!(r, SchRecord::NetLabel(_))).count();

        assert_eq!(comp_count, 2, "2 components");
        assert_eq!(des_count, 2, "2 designators (one per component)");
        assert_eq!(power_count, 2, "2 power ports");
        assert_eq!(net_count, 1, "1 net label");

        // Verify no Designator records were misclassified as Parameter
        let misclassified = doc.primitives.iter()
            .filter(|r| {
                if let SchRecord::Parameter(p) = r {
                    p.name == "Designator"
                } else {
                    false
                }
            })
            .count();
        assert_eq!(misclassified, 0, "No designators should be misclassified as Parameter");

        std::fs::remove_file(&sch_path).ok();
        std::fs::remove_file(&lib_path).ok();
    }
}