Skip to content

Commit a99a3ad

Browse files
committed
do not merge rules
1 parent 8070792 commit a99a3ad

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

src/patch/device.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl DeviceExt for Device {
171171

172172
// Now process all peripherals
173173
for (periphspec, val) in device {
174-
let periphspec = periphspec.str()?;
174+
let periphspec = periphspec.key()?;
175175
if !Self::KEYWORDS.contains(&periphspec) {
176176
//val["_path"] = device["_path"]; // TODO: check
177177
self.process_peripheral(periphspec, val.hash()?, config)

src/patch/mod.rs

+32-5
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ fn update_dict(parent: &mut Hash, child: &Hash) -> Result<()> {
221221
for (key, val) in child.iter() {
222222
match key {
223223
Yaml::String(key) if key == "_path" || key == "_include" => continue,
224-
key if parent.contains_key(key) => {
224+
Yaml::String(k) if parent.contains_key(key) && k.starts_with('_') => {
225225
if let Entry::Occupied(mut e) = parent.entry(key.clone()) {
226226
match e.get_mut() {
227227
el if el == val => {
228-
println!("In {key:?}: dublicate rule {val:?}, ignored");
228+
println!("In {k}: dublicate rule {val:?}, ignored");
229229
}
230230
Yaml::Array(a) => match val {
231231
Yaml::Array(val) => {
@@ -235,7 +235,7 @@ fn update_dict(parent: &mut Hash, child: &Hash) -> Result<()> {
235235
if !a.contains(val) {
236236
a.push(val.clone());
237237
} else {
238-
println!("In {key:?}: dublicate rule {val:?}, ignored");
238+
println!("In {k}: dublicate rule {val:?}, ignored");
239239
}
240240
}
241241
_ => {}
@@ -250,18 +250,45 @@ fn update_dict(parent: &mut Hash, child: &Hash) -> Result<()> {
250250
a.insert(0, s.clone());
251251
e.insert(Yaml::Array(a));
252252
} else {
253-
println!("In {key:?}: dublicate rule {s:?}, ignored");
253+
println!("In {k}: dublicate rule {s:?}, ignored");
254254
}
255255
}
256256
s2 if matches!(s2, Yaml::String(_)) => {
257-
println!("In {key:?}: conflicting rules {s:?} and {s2:?}, ignored");
257+
println!("In {k}: conflicting rules {s:?} and {s2:?}, ignored");
258258
}
259259
_ => {}
260260
},
261261
_ => {}
262262
}
263263
}
264264
}
265+
Yaml::String(_) if parent.contains_key(key) => {
266+
let mut i = 0;
267+
loop {
268+
let key = Yaml::Array(vec![key.clone(), Yaml::Integer(i)]);
269+
if !parent.contains_key(&key) {
270+
parent.insert(key, val.clone());
271+
break;
272+
}
273+
i += 1;
274+
}
275+
}
276+
Yaml::Array(a)
277+
if parent.contains_key(key)
278+
&& matches!(a.as_slice(), [Yaml::String(_), Yaml::Integer(_)]) =>
279+
{
280+
if let [k @ Yaml::String(_), Yaml::Integer(_)] = a.as_slice() {
281+
let mut i = 0;
282+
loop {
283+
let key = Yaml::Array(vec![k.clone(), Yaml::Integer(i)]);
284+
if !parent.contains_key(&key) {
285+
parent.insert(key, val.clone());
286+
break;
287+
}
288+
i += 1;
289+
}
290+
}
291+
}
265292
_ => {
266293
parent.insert(key.clone(), val.clone());
267294
}

src/patch/peripheral.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ impl PeripheralExt for Peripheral {
12511251

12521252
// Handle registers or clusters
12531253
for (rcspec, rcmod) in pmod {
1254-
let rcspec = rcspec.str()?;
1254+
let rcspec = rcspec.key()?;
12551255
if Self::KEYWORDS.contains(&rcspec) {
12561256
continue;
12571257
}
@@ -1281,7 +1281,7 @@ impl PeripheralExt for Peripheral {
12811281

12821282
// Handle clusters
12831283
for (cspec, cluster) in pmod.hash_iter("_clusters") {
1284-
let cspec = cspec.str()?;
1284+
let cspec = cspec.key()?;
12851285
self.process_cluster(cspec, cluster.hash()?, &ppath, config)
12861286
.with_context(|| format!("According to `{cspec}`"))?;
12871287
}
@@ -1561,14 +1561,14 @@ impl ClusterExt for Cluster {
15611561

15621562
// Handle clusters
15631563
for (cspec, cluster) in cmod.hash_iter("_clusters") {
1564-
let cspec = cspec.str()?;
1564+
let cspec = cspec.key()?;
15651565
self.process_cluster(cspec, cluster.hash()?, &cpath, config)
15661566
.with_context(|| format!("According to `{cspec}`"))?;
15671567
}
15681568

15691569
// Handle registers or clusters
15701570
for (rcspec, rcmod) in cmod {
1571-
let rcspec = rcspec.str()?;
1571+
let rcspec = rcspec.key()?;
15721572
if Self::KEYWORDS.contains(&rcspec) {
15731573
continue;
15741574
}

src/patch/register.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl RegisterExt for Register {
263263
// Handle fields
264264
if config.update_fields {
265265
for (fspec, field) in rmod {
266-
let fspec = fspec.str()?;
266+
let fspec = fspec.key()?;
267267
if Self::KEYWORDS.contains(&fspec) {
268268
continue;
269269
}

src/patch/yaml_ext.rs

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub enum YamlError {
1212
NotVec(Yaml),
1313
#[error("Value is not a string: {0:?}")]
1414
NotStr(Yaml),
15+
#[error("Value is not a supported hash key: {0:?}")]
16+
NotKey(Yaml),
1517
#[error("Value is not integer: {0:?}")]
1618
NotInt(Yaml),
1719
#[error("Value is not boolean: {0:?}")]
@@ -23,6 +25,7 @@ pub trait AsType {
2325
fn hash(&self) -> Result<&Hash, YamlError>;
2426
fn vec(&self) -> Result<&Vec<Yaml>, YamlError>;
2527
fn str(&self) -> Result<&str, YamlError>;
28+
fn key(&self) -> Result<&str, YamlError>;
2629
fn i64(&self) -> Result<i64, YamlError>;
2730
fn bool(&self) -> Result<bool, YamlError>;
2831
}
@@ -44,6 +47,19 @@ impl AsType for Yaml {
4447
fn str(&self) -> Result<&str, YamlError> {
4548
self.as_str().ok_or_else(|| YamlError::NotStr(self.clone()))
4649
}
50+
fn key(&self) -> Result<&str, YamlError> {
51+
match self {
52+
Yaml::String(k) => Ok(k),
53+
Yaml::Array(a) if matches!(a.as_slice(), [Yaml::String(_), Yaml::Integer(_)]) => {
54+
if let [Yaml::String(k), Yaml::Integer(_)] = a.as_slice() {
55+
Ok(k)
56+
} else {
57+
unreachable!()
58+
}
59+
}
60+
_ => Err(YamlError::NotKey(self.clone())),
61+
}
62+
}
4763
fn i64(&self) -> Result<i64, YamlError> {
4864
parse_i64(self).ok_or_else(|| YamlError::NotInt(self.clone()))
4965
}

0 commit comments

Comments
 (0)