Skip to content

Commit 91d76dc

Browse files
committed
env make
1 parent c6949c1 commit 91d76dc

File tree

4 files changed

+110
-90
lines changed

4 files changed

+110
-90
lines changed

src/patch/device.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ pub trait DeviceExt {
3333
fn modify_cpu(&mut self, cmod: &Hash) -> PatchResult;
3434

3535
/// Modify pspec inside device according to pmod
36-
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash) -> PatchResult;
36+
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash, env: &Env) -> PatchResult;
3737

3838
/// Add pname given by padd to device
39-
fn add_peripheral(&mut self, pname: &str, padd: &Hash) -> PatchResult;
39+
fn add_peripheral(&mut self, pname: &str, padd: &Hash, env: &Env) -> PatchResult;
4040

4141
/// Remove registers from pname and mark it as derivedFrom pderive.
4242
/// Update all derivedFrom referencing pname
43-
fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml) -> PatchResult;
43+
fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml, env: &Env) -> PatchResult;
4444

4545
/// Move registers from pold to pnew.
4646
/// Update all derivedFrom referencing pold
@@ -95,7 +95,7 @@ impl DeviceExt for Device {
9595
"_peripherals" => {
9696
for (pspec, pmod) in val.hash()? {
9797
let pspec = pspec.str()?;
98-
self.modify_peripheral(pspec, pmod.hash()?)
98+
self.modify_peripheral(pspec, pmod.hash()?, &env)
9999
.with_context(|| {
100100
format!("Modifying peripherals matched to `{pspec}`")
101101
})?;
@@ -119,7 +119,7 @@ impl DeviceExt for Device {
119119
}
120120

121121
_ => self
122-
.modify_peripheral(key, val.hash()?)
122+
.modify_peripheral(key, val.hash()?, &env)
123123
.with_context(|| format!("Modifying peripherals matched to `{key}`"))?,
124124
}
125125
}
@@ -134,14 +134,14 @@ impl DeviceExt for Device {
134134
// Handle any new peripherals (!)
135135
for (pname, padd) in device.hash_iter("_add") {
136136
let pname = pname.str()?;
137-
self.add_peripheral(pname, padd.hash()?)
137+
self.add_peripheral(pname, padd.hash()?, &env)
138138
.with_context(|| format!("Adding peripheral `{pname}`"))?;
139139
}
140140

141141
// Handle any derived peripherals
142142
for (pname, pderive) in device.hash_iter("_derive") {
143143
let pname = pname.str()?;
144-
self.derive_peripheral(pname, pderive)
144+
self.derive_peripheral(pname, pderive, &env)
145145
.with_context(|| format!("Deriving peripheral `{pname}` from `{pderive:?}`"))?;
146146
}
147147

@@ -222,11 +222,11 @@ impl DeviceExt for Device {
222222
Ok(())
223223
}
224224

225-
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash) -> PatchResult {
225+
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash, env: &Env) -> PatchResult {
226226
let mut modified = HashSet::new();
227227
let ptags = self.iter_peripherals(pspec).collect::<Vec<_>>();
228228
if !ptags.is_empty() {
229-
let peripheral_builder = make_peripheral(pmod, true)?;
229+
let peripheral_builder = make_peripheral(pmod, true, env)?;
230230
let dim = make_dim_element(pmod)?;
231231
for ptag in ptags {
232232
modified.insert(ptag.name.clone());
@@ -271,12 +271,12 @@ impl DeviceExt for Device {
271271
Ok(())
272272
}
273273

274-
fn add_peripheral(&mut self, pname: &str, padd: &Hash) -> PatchResult {
274+
fn add_peripheral(&mut self, pname: &str, padd: &Hash, env: &Env) -> PatchResult {
275275
if self.get_peripheral(pname).is_some() {
276276
return Err(anyhow!("device already has a peripheral {pname}"));
277277
}
278278

279-
let pnew = make_peripheral(padd, false)?
279+
let pnew = make_peripheral(padd, false, env)?
280280
.name(pname.to_string())
281281
.build(VAL_LVL)?;
282282
let pnew = if let Some(dim) = make_dim_element(padd)? {
@@ -289,7 +289,7 @@ impl DeviceExt for Device {
289289
Ok(())
290290
}
291291

292-
fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml) -> PatchResult {
292+
fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml, env: &Env) -> PatchResult {
293293
let (pderive, info) = if let Some(pderive) = pderive.as_str() {
294294
(
295295
pderive,
@@ -304,7 +304,7 @@ impl DeviceExt for Device {
304304
})?;
305305
(
306306
pderive,
307-
make_peripheral(hash, true)?.derived_from(Some(pderive.into())),
307+
make_peripheral(hash, true, env)?.derived_from(Some(pderive.into())),
308308
)
309309
} else {
310310
return Err(anyhow!("derive: incorrect syntax for {pname}"));

src/patch/mod.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ fn update_env(env: &mut Env, dict: &Hash) -> PatchResult {
4444
Ok(())
4545
}
4646

47+
fn insert_env<'a>(s: &'a str, env: &Env) -> Cow<'a, str> {
48+
let mut s = Cow::Borrowed(s);
49+
for (k, v) in env {
50+
let k = format!("`{k}`");
51+
if s.contains(&k) {
52+
s = s.replace(&k, v).into();
53+
}
54+
}
55+
s
56+
}
57+
fn insert_env_opt(s: Option<&str>, env: &Env) -> Option<String> {
58+
s.map(|s| insert_env(s, env).into_owned())
59+
}
60+
4761
#[non_exhaustive]
4862
#[derive(Clone, Debug)]
4963
pub struct Config {
@@ -435,10 +449,10 @@ fn modify_dim_element<T: Clone>(
435449
Ok(())
436450
}
437451

438-
fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
452+
fn make_field(fadd: &Hash, env: &Env) -> Result<FieldInfoBuilder> {
439453
let mut fnew = FieldInfo::builder()
440-
.description(fadd.get_string("description")?)
441-
.derived_from(fadd.get_string("derivedFrom")?)
454+
.description(insert_env_opt(fadd.get_str("description")?, env))
455+
.derived_from(insert_env_opt(fadd.get_str("derivedFrom")?, env))
442456
.access(fadd.get_str("access")?.and_then(Access::parse_str))
443457
.modified_write_values(
444458
fadd.get_str("modifiedWriteValues")?
@@ -466,11 +480,11 @@ fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
466480
Ok(fnew)
467481
}
468482

469-
fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
483+
fn make_register(radd: &Hash, env: &Env) -> Result<RegisterInfoBuilder> {
470484
let mut rnew = RegisterInfo::builder()
471485
.display_name(radd.get_string("displayName")?)
472-
.description(radd.get_string("description")?)
473-
.derived_from(radd.get_string("derivedFrom")?)
486+
.description(insert_env_opt(radd.get_str("description")?, env))
487+
.derived_from(insert_env_opt(radd.get_str("derivedFrom")?, env))
474488
.alternate_group(radd.get_string("alternateGroup")?)
475489
.alternate_register(radd.get_string("alternateRegister")?)
476490
.properties(get_register_properties(radd)?)
@@ -479,7 +493,7 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
479493
let mut fields = Vec::new();
480494
for (fname, val) in h {
481495
fields.push(
482-
make_field(val.hash()?)?
496+
make_field(val.hash()?, env)?
483497
.name(fname.str()?.into())
484498
.build(VAL_LVL)?
485499
.single(),
@@ -525,17 +539,17 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
525539
Ok(rnew)
526540
}
527541

528-
fn make_cluster(cadd: &Hash) -> Result<ClusterInfoBuilder> {
542+
fn make_cluster(cadd: &Hash, env: &Env) -> Result<ClusterInfoBuilder> {
529543
let mut cnew = ClusterInfo::builder()
530-
.description(cadd.get_string("description")?)
531-
.derived_from(cadd.get_string("derivedFrom")?)
544+
.description(insert_env_opt(cadd.get_str("description")?, env))
545+
.derived_from(insert_env_opt(cadd.get_str("derivedFrom")?, env))
532546
.default_register_properties(get_register_properties(cadd)?)
533547
.children(match cadd.get_hash("registers")? {
534548
Some(h) => {
535549
let mut ch = Vec::new();
536550
for (rname, val) in h {
537551
ch.push(RegisterCluster::Register(
538-
make_register(val.hash()?)?
552+
make_register(val.hash()?, env)?
539553
.name(rname.str()?.into())
540554
.build(VAL_LVL)?
541555
.single(),
@@ -566,12 +580,12 @@ fn make_interrupt(iadd: &Hash) -> Result<InterruptBuilder> {
566580
Ok(int)
567581
}
568582

569-
fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
583+
fn make_peripheral(padd: &Hash, modify: bool, env: &Env) -> Result<PeripheralInfoBuilder> {
570584
let mut pnew = PeripheralInfo::builder()
571585
.display_name(padd.get_string("displayName")?)
572586
.version(padd.get_string("version")?)
573-
.description(padd.get_string("description")?)
574-
.derived_from(padd.get_string("derivedFrom")?)
587+
.description(insert_env_opt(padd.get_str("description")?, env))
588+
.derived_from(insert_env_opt(padd.get_str("derivedFrom")?, env))
575589
.group_name(padd.get_string("groupName")?)
576590
.interrupt(if !modify {
577591
match padd.get_hash("interrupts")? {
@@ -619,7 +633,7 @@ fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
619633
let mut regs = Vec::new();
620634
for (rname, val) in h.iter() {
621635
regs.push(RegisterCluster::Register(
622-
make_register(val.hash()?)?
636+
make_register(val.hash()?, env)?
623637
.name(rname.str()?.into())
624638
.build(VAL_LVL)?
625639
.single(),

0 commit comments

Comments
 (0)