Skip to content

Commit

Permalink
env make
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jan 3, 2024
1 parent c6949c1 commit 91d76dc
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 90 deletions.
26 changes: 13 additions & 13 deletions src/patch/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub trait DeviceExt {
fn modify_cpu(&mut self, cmod: &Hash) -> PatchResult;

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

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

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

/// Move registers from pold to pnew.
/// Update all derivedFrom referencing pold
Expand Down Expand Up @@ -95,7 +95,7 @@ impl DeviceExt for Device {
"_peripherals" => {
for (pspec, pmod) in val.hash()? {
let pspec = pspec.str()?;
self.modify_peripheral(pspec, pmod.hash()?)
self.modify_peripheral(pspec, pmod.hash()?, &env)
.with_context(|| {
format!("Modifying peripherals matched to `{pspec}`")
})?;
Expand All @@ -119,7 +119,7 @@ impl DeviceExt for Device {
}

_ => self
.modify_peripheral(key, val.hash()?)
.modify_peripheral(key, val.hash()?, &env)
.with_context(|| format!("Modifying peripherals matched to `{key}`"))?,
}
}
Expand All @@ -134,14 +134,14 @@ impl DeviceExt for Device {
// Handle any new peripherals (!)
for (pname, padd) in device.hash_iter("_add") {
let pname = pname.str()?;
self.add_peripheral(pname, padd.hash()?)
self.add_peripheral(pname, padd.hash()?, &env)
.with_context(|| format!("Adding peripheral `{pname}`"))?;
}

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

Expand Down Expand Up @@ -222,11 +222,11 @@ impl DeviceExt for Device {
Ok(())
}

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

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

let pnew = make_peripheral(padd, false)?
let pnew = make_peripheral(padd, false, env)?
.name(pname.to_string())
.build(VAL_LVL)?;
let pnew = if let Some(dim) = make_dim_element(padd)? {
Expand All @@ -289,7 +289,7 @@ impl DeviceExt for Device {
Ok(())
}

fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml) -> PatchResult {
fn derive_peripheral(&mut self, pname: &str, pderive: &Yaml, env: &Env) -> PatchResult {
let (pderive, info) = if let Some(pderive) = pderive.as_str() {
(
pderive,
Expand All @@ -304,7 +304,7 @@ impl DeviceExt for Device {
})?;
(
pderive,
make_peripheral(hash, true)?.derived_from(Some(pderive.into())),
make_peripheral(hash, true, env)?.derived_from(Some(pderive.into())),
)
} else {
return Err(anyhow!("derive: incorrect syntax for {pname}"));
Expand Down
44 changes: 29 additions & 15 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ fn update_env(env: &mut Env, dict: &Hash) -> PatchResult {
Ok(())
}

fn insert_env<'a>(s: &'a str, env: &Env) -> Cow<'a, str> {
let mut s = Cow::Borrowed(s);
for (k, v) in env {
let k = format!("`{k}`");
if s.contains(&k) {
s = s.replace(&k, v).into();
}
}
s
}
fn insert_env_opt(s: Option<&str>, env: &Env) -> Option<String> {
s.map(|s| insert_env(s, env).into_owned())
}

#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct Config {
Expand Down Expand Up @@ -435,10 +449,10 @@ fn modify_dim_element<T: Clone>(
Ok(())
}

fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
fn make_field(fadd: &Hash, env: &Env) -> Result<FieldInfoBuilder> {
let mut fnew = FieldInfo::builder()
.description(fadd.get_string("description")?)
.derived_from(fadd.get_string("derivedFrom")?)
.description(insert_env_opt(fadd.get_str("description")?, env))
.derived_from(insert_env_opt(fadd.get_str("derivedFrom")?, env))
.access(fadd.get_str("access")?.and_then(Access::parse_str))
.modified_write_values(
fadd.get_str("modifiedWriteValues")?
Expand Down Expand Up @@ -466,11 +480,11 @@ fn make_field(fadd: &Hash) -> Result<FieldInfoBuilder> {
Ok(fnew)
}

fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
fn make_register(radd: &Hash, env: &Env) -> Result<RegisterInfoBuilder> {
let mut rnew = RegisterInfo::builder()
.display_name(radd.get_string("displayName")?)
.description(radd.get_string("description")?)
.derived_from(radd.get_string("derivedFrom")?)
.description(insert_env_opt(radd.get_str("description")?, env))
.derived_from(insert_env_opt(radd.get_str("derivedFrom")?, env))
.alternate_group(radd.get_string("alternateGroup")?)
.alternate_register(radd.get_string("alternateRegister")?)
.properties(get_register_properties(radd)?)
Expand All @@ -479,7 +493,7 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
let mut fields = Vec::new();
for (fname, val) in h {
fields.push(
make_field(val.hash()?)?
make_field(val.hash()?, env)?
.name(fname.str()?.into())
.build(VAL_LVL)?
.single(),
Expand Down Expand Up @@ -525,17 +539,17 @@ fn make_register(radd: &Hash) -> Result<RegisterInfoBuilder> {
Ok(rnew)
}

fn make_cluster(cadd: &Hash) -> Result<ClusterInfoBuilder> {
fn make_cluster(cadd: &Hash, env: &Env) -> Result<ClusterInfoBuilder> {
let mut cnew = ClusterInfo::builder()
.description(cadd.get_string("description")?)
.derived_from(cadd.get_string("derivedFrom")?)
.description(insert_env_opt(cadd.get_str("description")?, env))
.derived_from(insert_env_opt(cadd.get_str("derivedFrom")?, env))
.default_register_properties(get_register_properties(cadd)?)
.children(match cadd.get_hash("registers")? {
Some(h) => {
let mut ch = Vec::new();
for (rname, val) in h {
ch.push(RegisterCluster::Register(
make_register(val.hash()?)?
make_register(val.hash()?, env)?
.name(rname.str()?.into())
.build(VAL_LVL)?
.single(),
Expand Down Expand Up @@ -566,12 +580,12 @@ fn make_interrupt(iadd: &Hash) -> Result<InterruptBuilder> {
Ok(int)
}

fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
fn make_peripheral(padd: &Hash, modify: bool, env: &Env) -> Result<PeripheralInfoBuilder> {
let mut pnew = PeripheralInfo::builder()
.display_name(padd.get_string("displayName")?)
.version(padd.get_string("version")?)
.description(padd.get_string("description")?)
.derived_from(padd.get_string("derivedFrom")?)
.description(insert_env_opt(padd.get_str("description")?, env))
.derived_from(insert_env_opt(padd.get_str("derivedFrom")?, env))
.group_name(padd.get_string("groupName")?)
.interrupt(if !modify {
match padd.get_hash("interrupts")? {
Expand Down Expand Up @@ -619,7 +633,7 @@ fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
let mut regs = Vec::new();
for (rname, val) in h.iter() {
regs.push(RegisterCluster::Register(
make_register(val.hash()?)?
make_register(val.hash()?, env)?
.name(rname.str()?.into())
.build(VAL_LVL)?
.single(),
Expand Down
Loading

0 comments on commit 91d76dc

Please sign in to comment.