libyang 1.1.0

YANG parser in Rust
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
use crate::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug, PartialEq, Default)]
pub enum EntryKind {
    #[default]
    LeafEntry,
    DirectoryEntry,
    ChoiceEntry,
    ActionEntry,
}

#[derive(Default, Debug)]
pub struct ListAttr {
    pub min_elements: u64,
    pub max_elements: u64,
    pub ordered_by_user: bool,
}

impl ListAttr {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }
}

#[derive(Default, Debug)]
pub struct Entry {
    pub name: String,
    pub kind: EntryKind,
    pub presence: bool,
    pub mandatory: bool,
    pub dir: RefCell<Vec<Rc<Entry>>>,
    pub key: Vec<String>,
    pub extension: HashMap<String, String>,
    pub parent: RefCell<Option<Rc<Entry>>>,
    pub type_node: Option<TypeNode>,
    pub list_attr: Option<ListAttr>,
    pub choice_cases: Option<Vec<Rc<Entry>>>,

    // When this entry was introduced by a `case` inside a `choice`,
    // these record the choice and case names so consumers can reason
    // about mutual exclusion at commit time. Per RFC 7950 ยง7.9.2,
    // choice/case nodes do not themselves appear in the data tree;
    // only the case's direct children do. We flatten accordingly โ€”
    // the case's children land in the choice's parent `dir`, tagged
    // here with the choice and case names.
    pub choice: RefCell<Option<String>>,
    pub case: RefCell<Option<String>>,

    // Names of `choice` nodes defined directly under this entry. A
    // choice is otherwise invisible in the flattened tree (only its
    // cases' children appear), so recording the names lets an augment
    // target a choice โ€” including one that has no cases yet.
    pub choice_defs: RefCell<Vec<String>>,
}

impl Entry {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    pub fn new_dir(name: String) -> Self {
        Self {
            name,
            kind: EntryKind::DirectoryEntry,
            ..Default::default()
        }
    }

    pub fn new_leaf(name: String) -> Self {
        Self {
            name,
            kind: EntryKind::LeafEntry,
            ..Default::default()
        }
    }

    pub fn new_list(name: String, key: Vec<String>) -> Self {
        Self {
            name,
            kind: EntryKind::DirectoryEntry,
            key,
            ..Default::default()
        }
    }

    pub fn new_choice(name: String) -> Self {
        Self {
            name,
            kind: EntryKind::ChoiceEntry,
            choice_cases: Some(Vec::new()),
            ..Default::default()
        }
    }

    pub fn new_action(name: String) -> Self {
        Self {
            name,
            kind: EntryKind::ActionEntry,
            ..Default::default()
        }
    }

    pub fn has_key(&self) -> bool {
        !self.key.is_empty()
    }

    pub fn is_directory_entry(&self) -> bool {
        self.kind == EntryKind::DirectoryEntry
    }

    pub fn is_leaf_entry(&self) -> bool {
        self.kind == EntryKind::LeafEntry
    }

    pub fn is_container(&self) -> bool {
        self.kind == EntryKind::DirectoryEntry && self.list_attr.is_none()
    }

    pub fn is_list(&self) -> bool {
        self.kind == EntryKind::DirectoryEntry && self.list_attr.is_some()
    }

    pub fn is_leaf(&self) -> bool {
        self.kind == EntryKind::LeafEntry && self.list_attr.is_none()
    }

    pub fn is_leaflist(&self) -> bool {
        self.kind == EntryKind::LeafEntry && self.list_attr.is_some()
    }

    pub fn is_empty_leaf(&self) -> bool {
        if self.kind == EntryKind::LeafEntry {
            if let Some(n) = self.type_node.as_ref() {
                if n.kind == YangType::Empty {
                    return true;
                }
            }
        }
        false
    }

    pub fn is_choice(&self) -> bool {
        self.kind == EntryKind::ChoiceEntry
    }

    pub fn is_action(&self) -> bool {
        self.kind == EntryKind::ActionEntry
    }
}

pub fn path_split(path: String) -> (String, String) {
    let paths: Vec<_> = path.split(':').collect();
    if paths.len() > 1 {
        (String::from(paths[0]), String::from(paths[1]))
    } else {
        (String::from(""), String::from(""))
    }
}

pub fn path_module(path: &str) -> Option<(String, String)> {
    let paths: Vec<_> = path.split(':').collect();
    if paths.len() == 2 {
        Some((paths[0].to_string(), paths[1].to_string()))
    } else {
        None
    }
}

pub fn to_entry(store: &YangStore, module: &ModuleNode) -> Rc<Entry> {
    let entry = Rc::new(Entry::new_dir(module.name.clone()));
    for c in module.d.container.iter() {
        container_entry(module, store, c, entry.clone());
    }
    for leaf in module.d.leaf.iter() {
        leaf_entry(module, store, leaf, entry.clone());
    }
    for list in module.d.list.iter() {
        list_entry(module, store, list, entry.clone());
    }
    for leaf_list in module.d.leaf_list.iter() {
        leaf_list_entry(module, store, leaf_list, entry.clone());
    }
    for choice in module.d.choice.iter() {
        choice_entry(module, store, choice, entry.clone());
    }

    // Apply YANG 1.1 ยง7.17 augment statements. Each augment may live
    // in the root module itself or in any loaded module โ€” typically
    // a sibling module that imports the target. Walk all modules
    // once the primary tree is built so augment targets are
    // resolvable.
    for aug in module.augment.iter() {
        apply_augment(module, store, entry.clone(), aug);
    }
    for (name, m) in store.modules.iter() {
        if name == &module.name {
            continue;
        }
        for aug in m.augment.iter() {
            apply_augment(m, store, entry.clone(), aug);
        }
    }

    entry.clone()
}

/// Resolve the name of the module whose schema tree `target` roots
/// in, as seen from the augmenting module `top`. The leading segment
/// of an absolute schema-node-identifier carries the prefix that
/// binds the rest of the path to a module (RFC 7950 ยง6.5): the
/// augmenting module's own prefix maps to its own name, an imported
/// prefix maps to the imported module's name, and a bare (prefixless)
/// first segment defaults to the augmenting module itself.
fn augment_target_module<T>(top: &T, target: &str) -> String
where
    T: ModuleCommon,
{
    let first = target.split('/').find(|s| !s.is_empty()).unwrap_or("");
    match first.split_once(':') {
        Some((prefix, _)) => {
            if Some(prefix) == top.get_prefix() {
                top.get_name().to_string()
            } else {
                // Maps an imported prefix to its module name, or
                // returns the prefix unchanged when it binds to
                // nothing โ€” in which case the caller's module-name
                // comparison fails and the augment is skipped.
                prefix_resolve(top, prefix.to_string())
            }
        }
        None => top.get_name().to_string(),
    }
}

/// Walk `target` from `root`, matching each `[prefix:]name` segment
/// by its bare name against the Entry tree (the tree carries no
/// per-node namespace, and names are effectively unique within a
/// container). Returns the resolved entry, or the first segment that
/// did not match so callers can report where the path broke.
fn resolve_target(root: Rc<Entry>, target: &str) -> Result<Rc<Entry>, String> {
    let mut current = root;
    for seg in target.split('/').filter(|s| !s.is_empty()) {
        let name = seg.rsplit(':').next().unwrap_or(seg);
        let next = current
            .dir
            .borrow()
            .iter()
            .find(|e| e.name == name)
            .cloned();
        match next {
            Some(e) => current = e,
            None => return Err(seg.to_string()),
        }
    }
    Ok(current)
}

/// Apply a top-level `augment` against the tree rooted at `root`.
///
/// `to_entry` applies every loaded module's augments against the tree
/// currently being built, so first check that this augment actually
/// targets this tree's module (`root.name`) before touching it; that
/// both prevents an augment from bleeding into an unrelated module
/// that happens to share a node name and keeps the not-found
/// diagnostic meaningful.
fn apply_augment<T>(top: &T, store: &YangStore, root: Rc<Entry>, aug: &AugmentNode)
where
    T: ModuleCommon,
{
    if augment_target_module(top, &aug.target) != root.name {
        return;
    }
    // RFC 7950 ยง7.17: a top-level augment's target is an
    // absolute-schema-nodeid (leading '/').
    if !aug.target.starts_with('/') {
        eprintln!(
            "augment: top-level target \"{}\" must use the absolute form (leading '/')",
            aug.target
        );
    }
    resolve_and_inject(top, store, root, aug, "augment");
}

/// Expand a `uses` into `ent`: instantiate the referenced grouping,
/// then apply any uses-substatement augments. Every site that expands
/// a `uses` (module/container/list bodies, choice cases, rpc/action
/// input and output) goes through here so uses-augments are applied
/// consistently.
fn uses_entry<T>(top: &T, store: &YangStore, uses: &UsesNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    group_resolve(top, store, &uses.name, ent.clone());
    for aug in uses.augment.iter() {
        apply_uses_augment(top, store, ent.clone(), aug);
    }
}

/// Apply a `uses`-substatement augment (RFC 7950 ยง7.17, descendant
/// form). The target path is relative to `ent`, the point where the
/// grouping was just expanded, so resolve from there directly. Unlike
/// a top-level augment there is no cross-module targeting to gate on โ€”
/// the augment only ever reaches nodes the grouping contributed.
fn apply_uses_augment<T>(top: &T, store: &YangStore, ent: Rc<Entry>, aug: &AugmentNode)
where
    T: ModuleCommon,
{
    // RFC 7950 ยง7.17: a uses-substatement augment's target is a
    // descendant-schema-nodeid (no leading '/').
    if aug.target.starts_with('/') {
        eprintln!(
            "uses augment: target \"{}\" must use the descendant form (no leading '/')",
            aug.target
        );
    }
    resolve_and_inject(top, store, ent, aug, "uses augment");
}

/// Resolve `aug.target` from `root` and inject the augment body.
/// Shared by top-level and uses augments (`kind` only labels the
/// diagnostic). If the path resolves to a data node, inject there; if
/// the final segment instead names a choice (choices are flattened and
/// not addressable as entries), add the augment's cases to that choice.
fn resolve_and_inject<T>(top: &T, store: &YangStore, root: Rc<Entry>, aug: &AugmentNode, kind: &str)
where
    T: ModuleCommon,
{
    match resolve_target(root.clone(), &aug.target) {
        Ok(current) => inject_augment_body(top, store, current, aug),
        Err(seg) => {
            if !augment_into_choice(top, store, root, aug) {
                eprintln!(
                    "{kind}: in module {}, target \"{}\" not found (no node matching \"{}\")",
                    top.get_name(),
                    aug.target,
                    seg
                );
            }
        }
    }
}

/// Inject an augment body into an already-resolved data node: its
/// data-def children (RFC 7950 ยง7.17) and any `action` substatements
/// (valid when the target is a container or list). Explicit `case`
/// substatements are handled separately by `augment_into_choice` since
/// they only apply to choice targets, which are not data nodes.
fn inject_augment_body<T>(top: &T, store: &YangStore, current: Rc<Entry>, aug: &AugmentNode)
where
    T: ModuleCommon,
{
    // RFC 7950 ยง7.17: data nodes and actions may only be added to a
    // container/list/choice/case/input/output/notification โ€” never to a
    // leaf or leaf-list. Resolution lands on a leaf only when the
    // augment is malformed.
    if current.is_leaf_entry() {
        eprintln!(
            "augment: cannot add nodes to leaf target \"{}\" ({})",
            current.name, aug.target
        );
        return;
    }

    // Snapshot existing child names so duplicates the augment introduces
    // can be rejected afterwards (RFC 7950 ยง7.17: an augment MUST NOT add
    // a node with the same name as one already present in the target).
    let existing: Vec<String> = current
        .dir
        .borrow()
        .iter()
        .map(|e| e.name.clone())
        .collect();
    let before_len = existing.len();

    datadef_entry(top, store, &aug.d, current.clone());
    for a in aug.action.iter() {
        action_entry(top, store, a, current.clone());
    }

    let mut dir = current.dir.borrow_mut();
    let mut i = before_len;
    while i < dir.len() {
        if existing.contains(&dir[i].name) {
            eprintln!(
                "augment: node \"{}\" already exists in target \"{}\"; not added",
                dir[i].name, current.name
            );
            dir.remove(i);
        } else {
            i += 1;
        }
    }
}

/// Handle an augment whose target is a choice. A choice node is not an
/// addressable entry โ€” `choice_entry` flattens each case's children
/// into the choice's parent, tagged with the choice/case names โ€” so
/// resolve the parent (every segment but the last) and treat the final
/// segment as the choice name. The choice is recognised via the
/// parent's recorded `choice_defs`, which lists every choice defined
/// there whether or not it has cases. Returns true if it handled the
/// augment.
fn augment_into_choice<T>(top: &T, store: &YangStore, root: Rc<Entry>, aug: &AugmentNode) -> bool
where
    T: ModuleCommon,
{
    let segs: Vec<&str> = aug.target.split('/').filter(|s| !s.is_empty()).collect();
    let Some((last, parents)) = segs.split_last() else {
        return false;
    };
    let choice_name = last.rsplit(':').next().unwrap_or(last);

    let mut parent = root;
    for seg in parents {
        let name = seg.rsplit(':').next().unwrap_or(seg);
        let next = parent.dir.borrow().iter().find(|e| e.name == name).cloned();
        match next {
            Some(e) => parent = e,
            None => return false,
        }
    }

    let is_choice = parent.choice_defs.borrow().iter().any(|n| n == choice_name);
    if !is_choice {
        return false;
    }

    // Explicit `case` substatements.
    for case in aug.cases.iter() {
        inject_case(top, store, parent.clone(), choice_name, &case.name, &case.d);
    }
    // Shorthand cases: each direct data node forms its own case named
    // after the node (RFC 7950 ยง7.9.2).
    for c in aug.d.container.iter() {
        inject_case_node(parent.clone(), choice_name, &c.name, |e| {
            container_entry(top, store, c, e)
        });
    }
    for leaf in aug.d.leaf.iter() {
        inject_case_node(parent.clone(), choice_name, &leaf.name, |e| {
            leaf_entry(top, store, leaf, e)
        });
    }
    for list in aug.d.list.iter() {
        inject_case_node(parent.clone(), choice_name, &list.name, |e| {
            list_entry(top, store, list, e)
        });
    }
    for leaf_list in aug.d.leaf_list.iter() {
        inject_case_node(parent.clone(), choice_name, &leaf_list.name, |e| {
            leaf_list_entry(top, store, leaf_list, e)
        });
    }
    true
}

/// Inject an explicit `case`'s data-def children into `ent` (the
/// choice's parent) and tag the newly added entries with the choice
/// and case names โ€” the flattened representation used throughout for
/// choice/case membership.
fn inject_case<T>(
    top: &T,
    store: &YangStore,
    ent: Rc<Entry>,
    choice_name: &str,
    case_name: &str,
    d: &DatadefNode,
) where
    T: ModuleCommon,
{
    let before = ent.dir.borrow().len();
    datadef_entry(top, store, d, ent.clone());
    tag_case_children(&ent, before, choice_name, case_name);
}

/// Inject a single shorthand-case node via `build` and tag the entries
/// it added with the choice name and the node's own name (the implicit
/// case name for a shorthand case).
fn inject_case_node<F>(ent: Rc<Entry>, choice_name: &str, case_name: &str, build: F)
where
    F: FnOnce(Rc<Entry>),
{
    let before = ent.dir.borrow().len();
    build(ent.clone());
    tag_case_children(&ent, before, choice_name, case_name);
}

/// Tag entries appended to `ent.dir` at or after index `before` with
/// the given choice/case names, leaving any already tagged by a nested
/// choice recursion untouched (innermost wins).
fn tag_case_children(ent: &Rc<Entry>, before: usize, choice_name: &str, case_name: &str) {
    let dir = ent.dir.borrow();
    for child in dir[before..].iter() {
        if child.choice.borrow().is_none() {
            *child.choice.borrow_mut() = Some(choice_name.to_string());
            *child.case.borrow_mut() = Some(case_name.to_string());
        }
    }
}

/// Process a DatadefNode's children (uses, container, leaf, list,
/// leaf-list, choice) into `ent.dir`. Shared between groupings and
/// augments.
pub fn datadef_entry<T>(top: &T, store: &YangStore, d: &DatadefNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    for uses in d.uses.iter() {
        uses_entry(top, store, uses, ent.clone());
    }
    for c in d.container.iter() {
        container_entry(top, store, c, ent.clone());
    }
    for leaf in d.leaf.iter() {
        leaf_entry(top, store, leaf, ent.clone());
    }
    for list in d.list.iter() {
        list_entry(top, store, list, ent.clone());
    }
    for leaf_list in d.leaf_list.iter() {
        leaf_list_entry(top, store, leaf_list, ent.clone());
    }
    for choice in d.choice.iter() {
        choice_entry(top, store, choice, ent.clone());
    }
}

pub trait ModuleCommon {
    fn get_name(&self) -> &str;
    fn get_prefix(&self) -> Option<&str>;
    fn get_identity(&self) -> &Vec<IdentityNode>;
    fn get_identities_mut(&mut self) -> &mut HashMap<String, Vec<String>>;
    fn get_include(&self) -> &Vec<IncludeNode>;
    fn get_import(&self) -> &Vec<ImportNode>;
    fn get_typedef(&self) -> &Vec<TypedefNode>;
    fn get_grouping(&self) -> &Vec<GroupingNode>;
    fn get_d(&self) -> &DatadefNode;
}

pub fn group_entry<T>(top: &T, store: &YangStore, g: &GroupingNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    datadef_entry(top, store, &g.d, ent);
}

pub fn group_resolve<T>(top: &T, store: &YangStore, name: &str, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some((m, n)) = path_module(name) {
        let prefix = prefix_resolve(top, m);
        if let Some(m) = store.find_module(&prefix) {
            for g in m.grouping.iter() {
                if g.name == n {
                    group_entry(m, store, g, ent);
                    return;
                }
            }
        }
    } else {
        for g in top.get_grouping().iter() {
            if g.name == name {
                group_entry(top, store, g, ent);
                return;
            }
        }
        for include in top.get_include().iter() {
            if let Some(submodule) = store.find_submodule(&include.name) {
                for g in submodule.grouping.iter() {
                    if g.name == *name {
                        group_entry(submodule, store, g, ent);
                        return;
                    }
                }
            }
        }
    }
}

pub fn identity_resolve<T>(top: &mut T)
where
    T: ModuleCommon,
{
    // Iterate over node.identity and collect the necessary information
    let mut identity_bases: HashMap<String, Vec<String>> = HashMap::new();
    {
        let identity = top.get_identity();
        for identity in identity.iter() {
            if !identity.base.is_empty() {
                for base in &identity.base {
                    if let Some((_module, _name)) = path_module(base) {
                        // TODO: Resolve only local identity reference
                    } else {
                        for i in top.get_identity().iter() {
                            if base == &i.name {
                                let items = identity_bases.entry(base.clone()).or_default();
                                items.push(identity.name.clone());
                            }
                        }
                    }
                }
            }
        }
    }
    for (key, value) in identity_bases.iter() {
        top.get_identities_mut().insert(key.clone(), value.to_vec());
    }
}

fn prefix_resolve<T>(node: &T, name: String) -> String
where
    T: ModuleCommon,
{
    for import in node.get_import().iter() {
        if let Some(prefix) = &import.prefix {
            if name == *prefix {
                return import.name.clone();
            }
        }
    }
    name
}

fn type_union_resolve<T>(top: &T, store: &YangStore, type_node: &TypeNode) -> Option<TypeNode>
where
    T: ModuleCommon,
{
    let mut nodes = Vec::<TypeNode>::new();
    for node in type_node.union.iter() {
        if node.kind == YangType::Path {
            if let Some(n) = type_path_resolve(top, store, node) {
                if n.kind == YangType::Union {
                    // A typedef'd arm whose base is itself a union
                    // (e.g. `type union { type some-union; โ€ฆ }`).
                    // type_path_resolve already returned its fully
                    // resolved arms, so flatten them in directly โ€”
                    // keeping every arm, not just the String ones.
                    for m in n.union.iter() {
                        nodes.push(m.clone());
                    }
                } else {
                    // Any other resolved scalar/address/string arm
                    // (e.g. `inet:as-number` โ†’ uint32, `inet:ipv4-
                    // address`, a patterned string). Previously only
                    // String arms were kept, so a union like
                    // `union { inet:as-number; enumeration { โ€ฆ } }`
                    // silently dropped the numeric arm and rejected
                    // every numeric value. Preserve the typedef name
                    // so the matcher can map it via `ytype_from_typedef`
                    // (e.g. inet:ipv4-address โ†’ Ipv4Addr).
                    let mut m = n.clone();
                    if m.typedef.is_none() {
                        m.typedef = Some(node.name.clone());
                    }
                    nodes.push(m);
                }
            }
        } else {
            // Inline union arm with a recognized kind (e.g.
            // `type string { pattern '...'; }` or `type uint32;`
            // written directly inside the union, not via a typedef
            // reference). Keep it so the matcher can dispatch on it;
            // the previous drop-on-the-floor behavior was the reason
            // inline pattern-restricted string arms in unions never
            // engaged.
            nodes.push(node.clone());
        }
    }
    let mut type_node = type_node.clone();
    type_node.union = nodes;
    Some(type_node)
}

fn type_path_resolve<T>(top: &T, store: &YangStore, type_node: &TypeNode) -> Option<TypeNode>
where
    T: ModuleCommon,
{
    if let Some((module, name)) = path_module(&type_node.name) {
        let prefix = prefix_resolve(top, module);
        let module = store.find_module(&prefix);
        if let Some(m) = module {
            for typedef in m.typedef.iter() {
                if typedef.name == name {
                    if let Some(node) = &typedef.type_node {
                        let mut node = node.clone();
                        node.typedef = Some(type_node.name.clone());
                        if node.kind == YangType::Union {
                            return type_union_resolve(top, store, &node);
                        } else {
                            return Some(node);
                        }
                    }
                }
            }
        }
    } else {
        for typedef in top.get_typedef().iter() {
            if typedef.name == type_node.name {
                if let Some(node) = &typedef.type_node {
                    let mut node = node.clone();
                    node.typedef = Some(type_node.name.clone());
                    if node.kind == YangType::Union {
                        // A local typedef whose underlying type is a
                        // union: resolve its Path arms the same way
                        // the prefixed branch does, otherwise a leaf
                        // like `type peer-id-or-all` reaches the
                        // matcher with every arm still `kind = Path`
                        // and nothing dispatches.
                        return type_union_resolve(top, store, &node);
                    }
                    return Some(node);
                }
            }
        }
    }
    None
}

fn type_resolve<T>(top: &T, store: &YangStore, type_node: &TypeNode, ent: &mut Entry)
where
    T: ModuleCommon,
{
    if type_node.kind == YangType::Path {
        if let Some(node) = type_path_resolve(top, store, type_node) {
            ent.type_node = Some(node);
        }
    } else if type_node.kind == YangType::Identityref {
        if let Some(base) = &type_node.base {
            if let Some((module, name)) = path_module(base) {
                let prefix = prefix_resolve(top, module);
                let module = store.find_module(&prefix);
                if let Some(m) = module {
                    let mut node = type_node.clone();
                    node.kind = YangType::Enumeration;
                    if let Some(identities) = m.identities.get(&name) {
                        for i in identities.iter() {
                            node.enum_stmt.push(EnumNode { name: i.clone() });
                        }
                    }
                    ent.type_node = Some(node);
                    return;
                } else {
                    // println!("XXX: module not found {}", name);
                }
            } else {
                // println!("XXX: self {}", base);
            }
        }
        ent.type_node = Some(type_node.clone());
    } else if type_node.kind == YangType::Union {
        let mut union_node = TypeNode::new(type_node.name.clone(), YangType::Union);
        for node in type_node.union.iter() {
            if node.kind == YangType::Path {
                if let Some(node) = type_path_resolve(top, store, node) {
                    union_node.union.push(node);
                }
            } else {
                // Inline arm with a recognized kind (e.g. `type uint32;`
                // or `type string { pattern '...'; }` written directly
                // inside the union, not via a typedef reference). Keep
                // it so the matcher can dispatch on it; otherwise inline
                // scalar / patterned-string arms silently disappear and
                // a union like `union { uint32; inet:ipv4-address; }`
                // only matches the ipv4-address arm.
                union_node.union.push(node.clone());
            }
        }
        ent.type_node = Some(union_node);
    } else {
        ent.type_node = Some(type_node.clone());
    }
}

pub fn action_entry<T>(top: &T, store: &YangStore, a: &ActionNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    let e = Entry::new_action(a.name.clone());
    let rc = Rc::new(e);

    // Process input parameters if present
    if let Some(input) = &a.input {
        let mut input_entry = Entry::new_dir("input".to_string());
        input_entry
            .extension
            .insert("input".to_string(), "true".to_string());
        let input_rc = Rc::new(input_entry);

        // Process input data definitions
        for uses in input.d.uses.iter() {
            uses_entry(top, store, uses, input_rc.clone());
        }
        for c in input.d.container.iter() {
            container_entry(top, store, c, input_rc.clone());
        }
        for leaf in input.d.leaf.iter() {
            leaf_entry(top, store, leaf, input_rc.clone());
        }
        for list in input.d.list.iter() {
            list_entry(top, store, list, input_rc.clone());
        }
        for leaf_list in input.d.leaf_list.iter() {
            leaf_list_entry(top, store, leaf_list, input_rc.clone());
        }
        for choice in input.d.choice.iter() {
            choice_entry(top, store, choice, input_rc.clone());
        }

        rc.dir.borrow_mut().push(input_rc.clone());
        input_rc.parent.replace(Some(rc.clone()));
    }

    // Process output parameters if present
    if let Some(output) = &a.output {
        let mut output_entry = Entry::new_dir("output".to_string());
        output_entry
            .extension
            .insert("output".to_string(), "true".to_string());
        let output_rc = Rc::new(output_entry);

        // Process output data definitions
        for uses in output.d.uses.iter() {
            uses_entry(top, store, uses, output_rc.clone());
        }
        for c in output.d.container.iter() {
            container_entry(top, store, c, output_rc.clone());
        }
        for leaf in output.d.leaf.iter() {
            leaf_entry(top, store, leaf, output_rc.clone());
        }
        for list in output.d.list.iter() {
            list_entry(top, store, list, output_rc.clone());
        }
        for leaf_list in output.d.leaf_list.iter() {
            leaf_list_entry(top, store, leaf_list, output_rc.clone());
        }
        for choice in output.d.choice.iter() {
            choice_entry(top, store, choice, output_rc.clone());
        }

        rc.dir.borrow_mut().push(output_rc.clone());
        output_rc.parent.replace(Some(rc.clone()));
    }

    ent.dir.borrow_mut().push(rc.clone());
    rc.parent.replace(Some(ent.clone()));
}

pub fn choice_entry<T>(top: &T, store: &YangStore, c: &ChoiceNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some(config) = &c.config {
        if !config.config {
            return;
        }
    }

    // Record the choice name on its parent so it stays addressable for
    // augments even before (or without) any case contributing children.
    ent.choice_defs.borrow_mut().push(c.name.clone());

    // Per RFC 7950 ยง7.9.2, neither the `choice` node nor its `case`
    // nodes appear in the data tree โ€” only the case's direct data
    // children do. We flatten each case's children into the choice's
    // parent `ent.dir` and tag each added entry with (choice, case)
    // metadata so consumers can enforce mutual exclusion later. The
    // same flattening is reused by `augment_into_choice`.
    for case in c.cases.iter() {
        inject_case(top, store, ent.clone(), &c.name, &case.name, &case.d);
    }
}

pub fn container_entry<T>(top: &T, store: &YangStore, c: &ContainerNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some(config) = &c.config {
        if !config.config {
            return;
        }
    }
    let mut e = Entry::new_dir(c.name.clone());
    for u in c.unknown.iter() {
        e.extension.insert(u.name.clone(), u.argument.clone());
    }
    e.presence = c.presence.is_some();
    let rc = Rc::new(e);

    for uses in c.d.uses.iter() {
        uses_entry(top, store, uses, rc.clone());
    }
    for c in c.d.container.iter() {
        container_entry(top, store, c, rc.clone());
    }
    for leaf in c.d.leaf.iter() {
        leaf_entry(top, store, leaf, rc.clone());
    }
    for list in c.d.list.iter() {
        list_entry(top, store, list, rc.clone());
    }
    for leaf_list in c.d.leaf_list.iter() {
        leaf_list_entry(top, store, leaf_list, rc.clone());
    }
    for choice in c.d.choice.iter() {
        choice_entry(top, store, choice, rc.clone());
    }
    for action in c.action.iter() {
        action_entry(top, store, action, rc.clone());
    }

    ent.dir.borrow_mut().push(rc.clone());
    rc.parent.replace(Some(ent.clone()));
}

fn list_entry<T>(top: &T, store: &YangStore, l: &ListNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some(config) = &l.config {
        if !config.config {
            return;
        }
    }
    let mut e = Entry::new_list(l.name.clone(), l.key.keys.clone());
    for u in l.unknown.iter() {
        if u.name == "ext:presence" {
            e.presence = true;
        }
        e.extension.insert(u.name.clone(), u.argument.clone());
    }
    let list_attr = ListAttr::new();
    e.list_attr = Some(list_attr);
    let rc = Rc::new(e);

    for uses in l.d.uses.iter() {
        uses_entry(top, store, uses, rc.clone());
    }
    for c in l.d.container.iter() {
        container_entry(top, store, c, rc.clone());
    }
    for leaf in l.d.leaf.iter() {
        leaf_entry(top, store, leaf, rc.clone());
    }
    for list in l.d.list.iter() {
        list_entry(top, store, list, rc.clone());
    }
    for leaf_list in l.d.leaf_list.iter() {
        leaf_list_entry(top, store, leaf_list, rc.clone());
    }
    for choice in l.d.choice.iter() {
        choice_entry(top, store, choice, rc.clone());
    }

    ent.dir.borrow_mut().push(rc.clone());
    rc.parent.replace(Some(ent.clone()));
}

fn leaf_entry<T>(top: &T, store: &YangStore, leaf: &LeafNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some(config) = &leaf.config {
        if !config.config {
            return;
        }
    }
    let mut e = Entry::new_leaf(leaf.name.to_owned());
    e.mandatory = leaf.is_mandatory();
    for u in leaf.unknown.iter() {
        e.extension.insert(u.name.clone(), u.argument.clone());
    }
    if let Some(t) = leaf.type_stmt.as_ref() {
        type_resolve(top, store, t, &mut e);
    }
    let rc = Rc::new(e);
    ent.dir.borrow_mut().push(rc.clone());
    rc.parent.replace(Some(ent.clone()));
}

fn leaf_list_entry<T>(top: &T, store: &YangStore, leaf: &LeafListNode, ent: Rc<Entry>)
where
    T: ModuleCommon,
{
    if let Some(config) = &leaf.config {
        if !config.config {
            return;
        }
    }
    let mut e = Entry::new_leaf(leaf.name.clone());
    for u in leaf.unknown.iter() {
        e.extension.insert(u.name.clone(), u.argument.clone());
    }
    if let Some(t) = leaf.type_stmt.as_ref() {
        type_resolve(top, store, t, &mut e);
    }
    let list_attr = ListAttr::new();
    e.list_attr = Some(list_attr);

    let rc = Rc::new(e);
    ent.dir.borrow_mut().push(rc.clone());
    rc.parent.replace(Some(ent.clone()));
}

impl ModuleCommon for ModuleNode {
    fn get_name(&self) -> &str {
        &self.name
    }

    fn get_prefix(&self) -> Option<&str> {
        self.prefix.as_deref()
    }

    fn get_identity(&self) -> &Vec<IdentityNode> {
        &self.identity
    }

    fn get_identities_mut(&mut self) -> &mut HashMap<String, Vec<String>> {
        &mut self.identities
    }

    fn get_include(&self) -> &Vec<IncludeNode> {
        &self.include
    }

    fn get_import(&self) -> &Vec<ImportNode> {
        &self.import
    }

    fn get_typedef(&self) -> &Vec<TypedefNode> {
        &self.typedef
    }

    fn get_grouping(&self) -> &Vec<GroupingNode> {
        &self.grouping
    }

    fn get_d(&self) -> &DatadefNode {
        &self.d
    }
}

impl ModuleCommon for SubmoduleNode {
    fn get_name(&self) -> &str {
        &self.name
    }

    fn get_prefix(&self) -> Option<&str> {
        // A submodule has no prefix of its own; per RFC 7950 ยง7.2.2 it
        // shares the prefix of the module it belongs to.
        self.belongs_to.as_ref().and_then(|b| b.prefix.as_deref())
    }

    fn get_identity(&self) -> &Vec<IdentityNode> {
        &self.identity
    }

    fn get_identities_mut(&mut self) -> &mut HashMap<String, Vec<String>> {
        &mut self.identities
    }

    fn get_include(&self) -> &Vec<IncludeNode> {
        &self.include
    }

    fn get_import(&self) -> &Vec<ImportNode> {
        &self.import
    }

    fn get_typedef(&self) -> &Vec<TypedefNode> {
        &self.typedef
    }

    fn get_grouping(&self) -> &Vec<GroupingNode> {
        &self.grouping
    }

    fn get_d(&self) -> &DatadefNode {
        &self.d
    }
}

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

    fn dir(name: &str) -> Rc<Entry> {
        Rc::new(Entry::new_dir(name.to_string()))
    }

    #[test]
    fn resolve_target_walks_prefixed_path_by_bare_name() {
        let root = dir("m");
        let top = dir("top");
        let item = dir("item");
        top.dir.borrow_mut().push(item);
        root.dir.borrow_mut().push(top);

        // Each `prefix:name` segment is matched on its bare name.
        let found = resolve_target(root, "/a:top/a:item").expect("resolved");
        assert_eq!(found.name, "item");
    }

    #[test]
    fn resolve_target_reports_first_missing_segment() {
        let root = dir("m");
        root.dir.borrow_mut().push(dir("top"));

        let err = resolve_target(root, "/a:top/a:missing").unwrap_err();
        assert_eq!(err, "a:missing");
    }
}