@@ -8,11 +8,20 @@ pub fn topological_rule_order<'a>(rules: &'a Vec<&'a Rule<'a>>) -> Vec<&'a Rule<
8
8
let ( ident, refs) = find_references ( cddl_rule) ;
9
9
adj_list. insert ( ident. ident , ( * cddl_rule, refs) ) ;
10
10
}
11
- let mut unvisited = adj_list. iter ( ) . map ( |( k, _v) | * k) . collect :: < BTreeSet < & str > > ( ) ;
11
+ let mut unvisited = adj_list
12
+ . iter ( )
13
+ . map ( |( k, _v) | * k)
14
+ . collect :: < BTreeSet < & str > > ( ) ;
12
15
let mut topo_order = Vec :: new ( ) ;
13
16
let mut processing: BTreeSet < & ' a str > = BTreeSet :: new ( ) ;
14
17
while let Some ( u) = unvisited. iter ( ) . next ( ) . map ( |u| * u) {
15
- dfs_visit ( & mut topo_order, & mut unvisited, & mut processing, & adj_list, u) ;
18
+ dfs_visit (
19
+ & mut topo_order,
20
+ & mut unvisited,
21
+ & mut processing,
22
+ & adj_list,
23
+ u,
24
+ ) ;
16
25
}
17
26
topo_order
18
27
}
@@ -22,7 +31,8 @@ fn dfs_visit<'a>(
22
31
unvisited : & mut BTreeSet < & ' a str > ,
23
32
processing : & mut BTreeSet < & ' a str > ,
24
33
adj_list : & BTreeMap < & ' a str , ( & ' a cddl:: ast:: Rule < ' a > , Vec < & ' a Identifier < ' a > > ) > ,
25
- u : & ' a str ) {
34
+ u : & ' a str ,
35
+ ) {
26
36
processing. insert ( u) ;
27
37
let ( rule, neighbors) = adj_list. get ( u) . unwrap ( ) ;
28
38
for v in neighbors. iter ( ) {
@@ -42,41 +52,68 @@ fn dfs_visit<'a>(
42
52
fn find_references < ' a > ( cddl_rule : & ' a Rule < ' a > ) -> ( & ' a Identifier , Vec < & ' a Identifier < ' a > > ) {
43
53
let mut refs = Vec :: new ( ) ;
44
54
let ident = match cddl_rule {
45
- Rule :: Type { rule, .. } => {
46
- rule. value . type_choices . iter ( ) . for_each ( |tc| find_refs_type1 ( & mut refs, & tc. type1 ) ) ;
55
+ Rule :: Type { rule, .. } => {
56
+ rule. value
57
+ . type_choices
58
+ . iter ( )
59
+ . for_each ( |tc| find_refs_type1 ( & mut refs, & tc. type1 ) ) ;
47
60
& rule. name
48
- } ,
49
- Rule :: Group { rule, .. } => {
50
- assert_eq ! ( rule. generic_params, None , "{}: Generics not supported on plain groups" , rule. name) ;
61
+ }
62
+ Rule :: Group { rule, .. } => {
63
+ assert_eq ! (
64
+ rule. generic_params, None ,
65
+ "{}: Generics not supported on plain groups" ,
66
+ rule. name
67
+ ) ;
51
68
match & rule. entry {
52
- cddl:: ast:: GroupEntry :: InlineGroup { group, .. } => find_refs_group ( & mut refs, group) ,
69
+ cddl:: ast:: GroupEntry :: InlineGroup { group, .. } => {
70
+ find_refs_group ( & mut refs, group)
71
+ }
53
72
x => panic ! ( "Group rule with non-inline group? {:?}" , x) ,
54
73
}
55
74
& rule. name
56
- } ,
75
+ }
57
76
} ;
58
77
( ident, refs)
59
78
}
60
79
61
80
fn find_refs_type1 < ' a > ( refs : & mut Vec < & ' a Identifier < ' a > > , type1 : & ' a Type1 < ' a > ) {
62
81
match & type1. type2 {
63
- Type2 :: Typename { ident, generic_args, .. } => {
82
+ Type2 :: Typename {
83
+ ident,
84
+ generic_args,
85
+ ..
86
+ } => {
64
87
refs. push ( ident) ;
65
88
find_refs_generic_args ( refs, generic_args) ;
66
- } ,
67
- Type2 :: ParenthesizedType { pt, .. } => pt. type_choices . iter ( ) . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ,
68
- Type2 :: Map { group, .. } => find_refs_group ( refs, group) ,
69
- Type2 :: Array { group, .. } => find_refs_group ( refs, group) ,
70
- Type2 :: Unwrap { ident, generic_args, .. } => {
89
+ }
90
+ Type2 :: ParenthesizedType { pt, .. } => pt
91
+ . type_choices
92
+ . iter ( )
93
+ . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ,
94
+ Type2 :: Map { group, .. } => find_refs_group ( refs, group) ,
95
+ Type2 :: Array { group, .. } => find_refs_group ( refs, group) ,
96
+ Type2 :: Unwrap {
97
+ ident,
98
+ generic_args,
99
+ ..
100
+ } => {
71
101
refs. push ( ident) ;
72
102
find_refs_generic_args ( refs, generic_args) ;
73
- } ,
74
- Type2 :: ChoiceFromInlineGroup { group, .. } => find_refs_group ( refs, group) ,
75
- Type2 :: ChoiceFromGroup { ident, generic_args, .. } => {
103
+ }
104
+ Type2 :: ChoiceFromInlineGroup { group, .. } => find_refs_group ( refs, group) ,
105
+ Type2 :: ChoiceFromGroup {
106
+ ident,
107
+ generic_args,
108
+ ..
109
+ } => {
76
110
refs. push ( ident) ;
77
111
find_refs_generic_args ( refs, generic_args) ;
78
- } ,
79
- Type2 :: TaggedData { t, .. } => t. type_choices . iter ( ) . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ,
112
+ }
113
+ Type2 :: TaggedData { t, .. } => t
114
+ . type_choices
115
+ . iter ( )
116
+ . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ,
80
117
_ => ( ) ,
81
118
}
82
119
}
@@ -85,26 +122,36 @@ fn find_refs_group<'a>(refs: &mut Vec<&'a Identifier<'a>>, group: &'a Group<'a>)
85
122
for group_choice in group. group_choices . iter ( ) {
86
123
for ( group_entry, _) in group_choice. group_entries . iter ( ) {
87
124
match group_entry {
88
- GroupEntry :: InlineGroup { group, .. } => find_refs_group ( refs, group) ,
89
- GroupEntry :: TypeGroupname { ge, .. } => {
125
+ GroupEntry :: InlineGroup { group, .. } => find_refs_group ( refs, group) ,
126
+ GroupEntry :: TypeGroupname { ge, .. } => {
90
127
refs. push ( & ge. name ) ;
91
128
find_refs_generic_args ( refs, & ge. generic_args ) ;
92
- } ,
93
- GroupEntry :: ValueMemberKey { ge, .. } => {
94
- ge. entry_type . type_choices . iter ( ) . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ;
129
+ }
130
+ GroupEntry :: ValueMemberKey { ge, .. } => {
131
+ ge. entry_type
132
+ . type_choices
133
+ . iter ( )
134
+ . for_each ( |tc| find_refs_type1 ( refs, & tc. type1 ) ) ;
95
135
match & ge. member_key {
96
- Some ( MemberKey :: Type1 { t1, .. } ) => find_refs_type1 ( refs, t1) ,
97
- Some ( MemberKey :: NonMemberKey { .. } ) => unimplemented ! ( "Please open a github issue with repro steps" ) ,
136
+ Some ( MemberKey :: Type1 { t1, .. } ) => find_refs_type1 ( refs, t1) ,
137
+ Some ( MemberKey :: NonMemberKey { .. } ) => {
138
+ unimplemented ! ( "Please open a github issue with repro steps" )
139
+ }
98
140
_ => ( ) ,
99
141
}
100
- } ,
142
+ }
101
143
}
102
144
}
103
145
}
104
146
}
105
147
106
- fn find_refs_generic_args < ' a > ( refs : & mut Vec < & ' a Identifier < ' a > > , generic_arg : & ' a Option < GenericArgs < ' a > > ) {
148
+ fn find_refs_generic_args < ' a > (
149
+ refs : & mut Vec < & ' a Identifier < ' a > > ,
150
+ generic_arg : & ' a Option < GenericArgs < ' a > > ,
151
+ ) {
107
152
if let Some ( arg) = generic_arg {
108
- arg. args . iter ( ) . for_each ( |arg| find_refs_type1 ( refs, arg. arg . as_ref ( ) ) ) ;
153
+ arg. args
154
+ . iter ( )
155
+ . for_each ( |arg| find_refs_type1 ( refs, arg. arg . as_ref ( ) ) ) ;
109
156
}
110
- }
157
+ }
0 commit comments