Skip to content

Commit 81ba23c

Browse files
committed
public_index
1 parent d74cf47 commit 81ba23c

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

svd-parser/src/expand.rs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Provides [expand] method to convert arrays, clusters and derived items in regular instances
22
33
use anyhow::{anyhow, Result};
4+
use std::borrow::Cow;
45
use std::collections::HashMap;
56
use std::mem::take;
67
use svd_rs::{
@@ -10,8 +11,8 @@ use svd_rs::{
1011

1112
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
1213
pub struct RegisterPath {
13-
peripheral: String,
14-
path: Vec<String>,
14+
pub peripheral: String,
15+
pub path: Vec<String>,
1516
}
1617

1718
impl RegisterPath {
@@ -21,23 +22,25 @@ impl RegisterPath {
2122
path: Vec::new(),
2223
}
2324
}
24-
pub fn new_child(&self, name: &str) -> Self {
25+
pub fn new_child(&self, name: impl Into<String>) -> Self {
2526
let mut child = self.clone();
2627
child.path.push(name.into());
2728
child
2829
}
29-
pub fn split_str(s: &str) -> (Option<Self>, String) {
30+
pub fn split_str<'a>(s: &'a str) -> (Option<Self>, Cow<'a, str>) {
3031
Self::split_vec(s.split('.').collect())
3132
}
32-
pub fn split_vec(mut v: Vec<&str>) -> (Option<Self>, String) {
33-
let name = v.pop().unwrap().to_string();
34-
if v.is_empty() {
35-
(None, name)
33+
pub fn split_vec<'a>(mut v: Vec<&'a str>) -> (Option<Self>, Cow<'a, str>) {
34+
let name = v.pop().unwrap();
35+
let mut iter = v.into_iter();
36+
let path = if let Some(p) = iter.next() {
37+
let mut rpath = Self::new(p);
38+
rpath.path = iter.map(Into::into).collect();
39+
Some(rpath)
3640
} else {
37-
let mut rpath = Self::new(v[0]);
38-
rpath.path = v[1..].iter().map(|c| c.to_string()).collect();
39-
(Some(rpath), name)
40-
}
41+
None
42+
};
43+
(path, name.into())
4144
}
4245
pub fn parent(&self) -> Self {
4346
let mut parent = self.clone();
@@ -48,8 +51,8 @@ impl RegisterPath {
4851

4952
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
5053
pub struct FieldPath {
51-
register: RegisterPath,
52-
name: String,
54+
pub register: RegisterPath,
55+
pub name: String,
5356
}
5457

5558
impl FieldPath {
@@ -61,13 +64,28 @@ impl FieldPath {
6164
}
6265
}
6366

67+
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
68+
pub struct EnumPath<'a> {
69+
pub field: FieldPath,
70+
pub name: &'a str,
71+
}
72+
73+
impl<'a> EnumPath<'a> {
74+
pub fn new(f: &FieldPath, name: &'a str) -> Self {
75+
Self {
76+
field: f.clone(),
77+
name,
78+
}
79+
}
80+
}
81+
6482
#[derive(Clone, Debug, Default)]
65-
struct Index<'a> {
66-
peripherals: HashMap<String, &'a Peripheral>,
67-
clusters: HashMap<RegisterPath, &'a Cluster>,
68-
registers: HashMap<RegisterPath, &'a Register>,
69-
fields: HashMap<FieldPath, &'a Field>,
70-
evs: HashMap<(FieldPath, String), &'a EnumeratedValues>,
83+
pub struct Index<'a> {
84+
pub peripherals: HashMap<Cow<'a, str>, &'a Peripheral>,
85+
pub clusters: HashMap<RegisterPath, &'a Cluster>,
86+
pub registers: HashMap<RegisterPath, &'a Register>,
87+
pub fields: HashMap<FieldPath, &'a Field>,
88+
pub evs: HashMap<EnumPath<'a>, &'a EnumeratedValues>,
7189
}
7290

7391
impl<'a> Index<'a> {
@@ -81,7 +99,7 @@ impl<'a> Index<'a> {
8199
for c in p.clusters() {
82100
self.add_cluster(&path, c);
83101
}
84-
self.peripherals.insert(name, p);
102+
self.peripherals.insert(name.into(), p);
85103
}
86104
}
87105
let path = RegisterPath::new(&p.name);
@@ -91,13 +109,13 @@ impl<'a> Index<'a> {
91109
for c in p.clusters() {
92110
self.add_cluster(&path, c);
93111
}
94-
self.peripherals.insert(p.name.clone(), p);
112+
self.peripherals.insert(p.name.as_str().into(), p);
95113
}
96114

97115
fn add_cluster(&mut self, path: &RegisterPath, c: &'a Cluster) {
98116
if let Cluster::Array(info, dim) = c {
99117
for name in names(info, dim) {
100-
let cpath = RegisterPath::new_child(path, &name);
118+
let cpath = RegisterPath::new_child(path, name);
101119
for r in c.registers() {
102120
self.add_register(&cpath, r);
103121
}
@@ -119,7 +137,7 @@ impl<'a> Index<'a> {
119137
fn add_register(&mut self, path: &RegisterPath, r: &'a Register) {
120138
if let Register::Array(info, dim) = r {
121139
for name in names(info, dim) {
122-
let rpath = RegisterPath::new_child(path, &name);
140+
let rpath = RegisterPath::new_child(path, name);
123141
for f in r.fields() {
124142
self.add_field(&rpath, f);
125143
}
@@ -138,7 +156,7 @@ impl<'a> Index<'a> {
138156
let fpath = FieldPath::new(path, &name);
139157
for evs in &f.enumerated_values {
140158
if let Some(name) = evs.name.as_ref() {
141-
self.evs.insert((fpath.clone(), name.to_string()), evs);
159+
self.evs.insert(EnumPath::new(&fpath, name), evs);
142160
}
143161
}
144162
self.fields.insert(fpath, f);
@@ -147,7 +165,7 @@ impl<'a> Index<'a> {
147165
let fpath = FieldPath::new(path, &f.name);
148166
for evs in &f.enumerated_values {
149167
if let Some(name) = evs.name.as_ref() {
150-
self.evs.insert((fpath.clone(), name.to_string()), evs);
168+
self.evs.insert(EnumPath::new(&fpath, name), evs);
151169
}
152170
}
153171
self.fields.insert(fpath, f);
@@ -222,11 +240,11 @@ fn derive_cluster(
222240
let rdpath;
223241
let cluster_path;
224242
let d = (if let Some(dparent) = dparent {
225-
cluster_path = RegisterPath::new_child(&dparent, &dname);
243+
cluster_path = RegisterPath::new_child(&dparent, dname);
226244
rdpath = dparent;
227245
index.clusters.get(&cluster_path)
228246
} else {
229-
cluster_path = RegisterPath::new_child(path, &dname);
247+
cluster_path = RegisterPath::new_child(path, dname);
230248
rdpath = path.clone();
231249
index.clusters.get(&cluster_path)
232250
})
@@ -253,11 +271,11 @@ fn derive_register(
253271
let rdpath;
254272
let reg_path;
255273
let d = (if let Some(dparent) = dparent {
256-
reg_path = RegisterPath::new_child(&dparent, &dname);
274+
reg_path = RegisterPath::new_child(&dparent, dname);
257275
rdpath = dparent;
258276
index.registers.get(&reg_path)
259277
} else {
260-
reg_path = RegisterPath::new_child(path, &dname);
278+
reg_path = RegisterPath::new_child(path, dname);
261279
rdpath = path.clone();
262280
index.registers.get(&reg_path)
263281
})
@@ -411,15 +429,15 @@ fn derive_enumerated_values(
411429
index: &Index,
412430
) -> Result<()> {
413431
let mut v: Vec<&str> = dpath.split('.').collect();
414-
let dname = v.pop().unwrap().to_string();
432+
let dname = v.pop().unwrap();
415433
let d = if v.is_empty() {
416434
// Only EVNAME: Must be in one of fields in same register
417435
let rdpath = &fpath.register;
418436
if let Some(r) = index.registers.get(rdpath) {
419437
let mut found = None;
420438
for f in r.fields() {
421439
let fdpath = FieldPath::new(rdpath, &f.name);
422-
if let Some(d) = index.evs.get(&(fdpath.clone(), dname.clone())) {
440+
if let Some(d) = index.evs.get(&EnumPath::new(&fdpath, dname)) {
423441
found = Some((d, fdpath));
424442
break;
425443
}
@@ -429,7 +447,7 @@ fn derive_enumerated_values(
429447
None
430448
}
431449
} else {
432-
let fdname = v.pop().unwrap().to_string();
450+
let fdname = v.pop().unwrap();
433451
let fdpath = if v.is_empty() {
434452
// FIELD.EVNAME
435453
FieldPath::new(&fpath.register, &fdname)
@@ -441,12 +459,15 @@ fn derive_enumerated_values(
441459
} else {
442460
// REG.FIELD.EVNAME
443461
let mut rdpath = fpath.register.parent();
444-
rdpath.path.push(rdname);
462+
rdpath.path.push(rdname.into());
445463
rdpath
446464
};
447465
FieldPath::new(&rdpath, &fdname)
448466
};
449-
index.evs.get(&(fdpath.clone(), dname)).map(|d| (d, fdpath))
467+
index
468+
.evs
469+
.get(&EnumPath::new(&fdpath, dname))
470+
.map(|d| (d, fdpath))
450471
};
451472

452473
if let Some((d, fdpath)) = d {

svd-parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod usage;
215215
mod writeconstraint;
216216

217217
#[cfg(feature = "expand")]
218-
mod expand;
218+
pub mod expand;
219219

220220
#[cfg(feature = "expand")]
221221
pub use expand::{expand, expand_properties};

0 commit comments

Comments
 (0)