Skip to content

Commit c6949c1

Browse files
committed
env
1 parent 3f9264e commit c6949c1

File tree

4 files changed

+205
-88
lines changed

4 files changed

+205
-88
lines changed

src/patch/device.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{fs::File, io::Read, path::Path};
99
use super::iterators::{MatchIter, Matched};
1010
use super::peripheral::{PeripheralExt, RegisterBlockExt};
1111
use super::yaml_ext::{AsType, GetVal};
12-
use super::{abspath, matchname, Config, PatchResult, Spec, VAL_LVL};
12+
use super::{abspath, matchname, update_env, Config, Env, PatchResult, Spec, VAL_LVL};
1313
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
1414
use super::{make_dim_element, modify_dim_element, modify_register_properties};
1515

@@ -55,6 +55,7 @@ pub trait DeviceExt {
5555
pspec: &str,
5656
peripheral: &Hash,
5757
config: &Config,
58+
env: &Env,
5859
) -> PatchResult;
5960
}
6061

@@ -64,6 +65,9 @@ impl DeviceExt for Device {
6465
}
6566

6667
fn process(&mut self, device: &Hash, config: &Config) -> PatchResult {
68+
let mut env = Env::new();
69+
update_env(&mut env, device)?;
70+
6771
// Handle any deletions
6872
for pspec in device.str_vec_iter("_delete")? {
6973
self.delete_peripheral(pspec)
@@ -154,7 +158,7 @@ impl DeviceExt for Device {
154158
let periphspec = periphspec.str()?;
155159
if !periphspec.starts_with('_') {
156160
//val["_path"] = device["_path"]; // TODO: check
157-
self.process_peripheral(periphspec, val.hash()?, config)
161+
self.process_peripheral(periphspec, val.hash()?, config, &env)
158162
.with_context(|| format!("According to `{periphspec}`"))?;
159163
}
160164
}
@@ -415,13 +419,14 @@ impl DeviceExt for Device {
415419
pspec: &str,
416420
peripheral: &Hash,
417421
config: &Config,
422+
env: &Env,
418423
) -> PatchResult {
419424
// Find all peripherals that match the spec
420425
let mut pcount = 0;
421426
let (pspec, ignore) = pspec.spec();
422427
for ptag in self.iter_peripherals(pspec) {
423428
pcount += 1;
424-
ptag.process(peripheral, config)
429+
ptag.process(peripheral, config, env.clone())
425430
.with_context(|| format!("Processing peripheral `{}`", ptag.name))?;
426431
}
427432
if !ignore && pcount == 0 {

src/patch/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod patch_cli;
22

33
use globset::Glob;
4+
use std::borrow::Cow;
5+
use std::collections::HashMap;
46
use std::fs::File;
57
use std::io::{Read, Write};
68
use std::path::{Path, PathBuf};
@@ -31,6 +33,17 @@ use crate::get_encoder_config;
3133

3234
const VAL_LVL: ValidateLevel = ValidateLevel::Weak;
3335

36+
pub type Env = HashMap<Cow<'static, str>, String>;
37+
38+
fn update_env(env: &mut Env, dict: &Hash) -> PatchResult {
39+
for (key, val) in dict.hash_iter("_env") {
40+
let key = key.str()?;
41+
let val = val.str()?;
42+
env.insert(key.to_string().into(), val.to_string());
43+
}
44+
Ok(())
45+
}
46+
3447
#[non_exhaustive]
3548
#[derive(Clone, Debug)]
3649
pub struct Config {

0 commit comments

Comments
 (0)