Skip to content

Commit 1d12bde

Browse files
committed
env make
1 parent f09c9f6 commit 1d12bde

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
@@ -43,6 +43,20 @@ fn update_env(env: &mut Env, dict: &Hash) -> PatchResult {
4343
Ok(())
4444
}
4545

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

445-
fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
459+
fn make_field(fadd: &Hash, env: &Env) -> Result<FieldInfoBuilder> {
446460
let mut fnew = FieldInfo::builder()
447-
.description(fadd.get_string("description")?)
448-
.derived_from(fadd.get_string("derivedFrom")?)
461+
.description(insert_env_opt(fadd.get_str("description")?, env))
462+
.derived_from(insert_env_opt(fadd.get_str("derivedFrom")?, env))
449463
.access(fadd.get_str("access")?.and_then(Access::parse_str))
450464
.modified_write_values(
451465
fadd.get_str("modifiedWriteValues")?
@@ -473,11 +487,11 @@ fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
473487
Ok(fnew)
474488
}
475489

476-
fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
490+
fn make_register(radd: &Hash, env: &Env) -> Result<RegisterInfoBuilder> {
477491
let mut rnew = RegisterInfo::builder()
478492
.display_name(radd.get_string("displayName")?)
479-
.description(radd.get_string("description")?)
480-
.derived_from(radd.get_string("derivedFrom")?)
493+
.description(insert_env_opt(radd.get_str("description")?, env))
494+
.derived_from(insert_env_opt(radd.get_str("derivedFrom")?, env))
481495
.alternate_group(radd.get_string("alternateGroup")?)
482496
.alternate_register(radd.get_string("alternateRegister")?)
483497
.properties(get_register_properties(radd)?)
@@ -486,7 +500,7 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
486500
let mut fields = Vec::new();
487501
for (fname, val) in h {
488502
fields.push(
489-
make_field(val.hash()?)?
503+
make_field(val.hash()?, env)?
490504
.name(fname.str()?.into())
491505
.build(VAL_LVL)?
492506
.single(),
@@ -532,17 +546,17 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
532546
Ok(rnew)
533547
}
534548

535-
fn make_cluster(cadd: &Hash) -> Result<ClusterInfoBuilder> {
549+
fn make_cluster(cadd: &Hash, env: &Env) -> Result<ClusterInfoBuilder> {
536550
let mut cnew = ClusterInfo::builder()
537-
.description(cadd.get_string("description")?)
538-
.derived_from(cadd.get_string("derivedFrom")?)
551+
.description(insert_env_opt(cadd.get_str("description")?, env))
552+
.derived_from(insert_env_opt(cadd.get_str("derivedFrom")?, env))
539553
.default_register_properties(get_register_properties(cadd)?)
540554
.children(match cadd.get_hash("registers")? {
541555
Some(h) => {
542556
let mut ch = Vec::new();
543557
for (rname, val) in h {
544558
ch.push(RegisterCluster::Register(
545-
make_register(val.hash()?)?
559+
make_register(val.hash()?, env)?
546560
.name(rname.str()?.into())
547561
.build(VAL_LVL)?
548562
.single(),
@@ -573,12 +587,12 @@ fn make_interrupt(iadd: &Hash) -> Result<InterruptBuilder> {
573587
Ok(int)
574588
}
575589

576-
fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
590+
fn make_peripheral(padd: &Hash, modify: bool, env: &Env) -> Result<PeripheralInfoBuilder> {
577591
let mut pnew = PeripheralInfo::builder()
578592
.display_name(padd.get_string("displayName")?)
579593
.version(padd.get_string("version")?)
580-
.description(padd.get_string("description")?)
581-
.derived_from(padd.get_string("derivedFrom")?)
594+
.description(insert_env_opt(padd.get_str("description")?, env))
595+
.derived_from(insert_env_opt(padd.get_str("derivedFrom")?, env))
582596
.group_name(padd.get_string("groupName")?)
583597
.interrupt(if !modify {
584598
match padd.get_hash("interrupts")? {
@@ -626,7 +640,7 @@ fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
626640
let mut regs = Vec::new();
627641
for (rname, val) in h.iter() {
628642
regs.push(RegisterCluster::Register(
629-
make_register(val.hash()?)?
643+
make_register(val.hash()?, env)?
630644
.name(rname.str()?.into())
631645
.build(VAL_LVL)?
632646
.single(),

0 commit comments

Comments
 (0)