Skip to content

Commit 8f9e019

Browse files
authored
Merge pull request #229 from rust-embedded/expand-patch
expand-patch
2 parents 5dcdd0f + 6deb1f2 commit 8f9e019

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

CHANGELOG-rust.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
* Add `expand-patch` tool to show full patch rule with all includes
9+
810
## [v0.3.14] 2024-04-04
911

1012
* If there is no path to interpolate, show unmodified `description`.

src/cli.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use anyhow::Result;
1+
use anyhow::{Ok, Result};
22
use clap::Parser;
3-
use std::path::PathBuf;
3+
use std::{fs::File, io::Write, path::PathBuf};
44

55
use svdtools::{
66
convert::convert_cli,
@@ -41,6 +41,13 @@ enum Command {
4141
#[clap(long)]
4242
enum_derive: Option<EnumAutoDerive>,
4343
},
44+
ExpandPatch {
45+
/// Path to input YAML file
46+
yaml_file: PathBuf,
47+
48+
/// Path to output file. By default it prints to stdout
49+
out_path: Option<PathBuf>,
50+
},
4451
/// Generate Make dependency file listing dependencies for a YAML file.
4552
Makedeps {
4653
/// Input yaml file
@@ -148,6 +155,18 @@ impl Command {
148155
&config,
149156
)?
150157
}
158+
Self::ExpandPatch {
159+
yaml_file,
160+
out_path,
161+
} => {
162+
let yml = patch_cli::expand_patch(yaml_file)?;
163+
if let Some(out_path) = out_path.as_ref() {
164+
let mut f = File::create(out_path)?;
165+
f.write_all(yml.as_bytes())?;
166+
} else {
167+
println!("{yml}");
168+
}
169+
}
151170
Self::Makedeps {
152171
yaml_file,
153172
deps_file,

src/patch/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,29 @@ impl Default for Config {
6666
}
6767
}
6868

69+
fn load_patch(yaml_file: &Path) -> Result<Yaml> {
70+
// Load the specified YAML root file
71+
let f = File::open(yaml_file)?;
72+
let mut contents = String::new();
73+
(&f).read_to_string(&mut contents)?;
74+
let docs = YamlLoader::load_from_str(&contents)?;
75+
let mut doc = docs.into_iter().next().unwrap(); // select the first document
76+
let root = doc.hash_mut()?;
77+
root.insert("_path".to_yaml(), yaml_file.to_str().unwrap().to_yaml());
78+
79+
// Load all included YAML files
80+
yaml_includes(root)?;
81+
Ok(doc)
82+
}
83+
6984
pub fn process_file(
7085
yaml_file: &Path,
7186
out_path: Option<&Path>,
7287
format_config: Option<&Path>,
7388
config: &Config,
7489
) -> Result<()> {
75-
// Load the specified YAML root file
76-
let f = File::open(yaml_file)?;
77-
let mut contents = String::new();
78-
(&f).read_to_string(&mut contents)?;
79-
let mut docs = YamlLoader::load_from_str(&contents)?;
80-
let root = docs[0].hash_mut()?; // select the first document
81-
root.insert("_path".to_yaml(), yaml_file.to_str().unwrap().to_yaml());
90+
let mut doc = load_patch(yaml_file)?;
91+
let root = doc.hash_mut()?;
8292

8393
// Load the specified SVD file
8494
let svdpath = abspath(
@@ -102,15 +112,12 @@ pub fn process_file(
102112
parser_config.validate_level = ValidateLevel::Disabled;
103113
let mut svd = svd_parser::parse_with_config(&contents, &parser_config)?;
104114

105-
// Load all included YAML files
106-
yaml_includes(root)?;
107-
108115
// Process device
109116
svd.process(root, config).with_context(|| {
110117
let name = &svd.name;
111118
let mut out_str = String::new();
112119
let mut emitter = yaml_rust::YamlEmitter::new(&mut out_str);
113-
emitter.dump(&Yaml::Hash(root.clone())).unwrap();
120+
emitter.dump(&doc).unwrap();
114121
if config.show_patch_on_error {
115122
format!("Processing device `{name}`. Patches looks like:\n{out_str}")
116123
} else {
@@ -189,6 +196,7 @@ pub fn yaml_includes(parent: &mut Hash) -> Result<Vec<PathBuf>> {
189196
included.extend(yaml_includes(child)?);
190197
update_dict(parent, child)?;
191198
}
199+
parent.remove(&"_include".to_yaml());
192200
Ok(included)
193201
}
194202

src/patch/patch_cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ pub fn patch(
1111
super::process_file(yaml_file, out_path, format_config, config)?;
1212
Ok(())
1313
}
14+
15+
pub fn expand_patch(yaml_file: &Path) -> Result<String> {
16+
let doc = super::load_patch(yaml_file)?;
17+
let mut out_str = String::new();
18+
let mut emitter = yaml_rust::YamlEmitter::new(&mut out_str);
19+
emitter.dump(&doc).unwrap();
20+
Ok(out_str)
21+
}

0 commit comments

Comments
 (0)