Skip to content

Commit 7010c72

Browse files
authored
Merge pull request #183 from rust-embedded/config
show patch on error
2 parents 83d5f10 + cc50355 commit 7010c72

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

CHANGELOG-rust.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This changelog tracks the Rust `svdtools` project. See
99
* `-1` for default enum value
1010
* Update `displayName` during collect, improve searching common `description`
1111
* mmaps: peripheral arrays, bump `svd` crates
12+
* patch: `--show-patch-on-error` prints yaml patches on error
1213

1314
## [v0.3.4] 2023-10-14
1415

src/cli.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ enum Command {
2323
/// If not specified, the default format config will be used.
2424
#[clap(long)]
2525
format_config: Option<PathBuf>,
26+
27+
/// When a patch error happens print formatted yaml with all rules included
28+
#[clap(long)]
29+
show_patch_on_error: bool,
2630
},
2731
/// Generate Make dependency file listing dependencies for a YAML file.
2832
Makedeps {
@@ -112,8 +116,16 @@ impl Command {
112116
yaml_file,
113117
out_path,
114118
format_config,
119+
show_patch_on_error,
115120
} => {
116-
patch_cli::patch(yaml_file, out_path.as_deref(), format_config.as_deref())?;
121+
let mut config = svdtools::patch::Config::default();
122+
config.show_patch_on_error = *show_patch_on_error;
123+
patch_cli::patch(
124+
yaml_file,
125+
out_path.as_deref(),
126+
format_config.as_deref(),
127+
&config,
128+
)?
117129
}
118130
Self::Makedeps {
119131
yaml_file,

src/patch/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ use crate::get_encoder_config;
3030

3131
const VAL_LVL: ValidateLevel = ValidateLevel::Weak;
3232

33+
#[non_exhaustive]
34+
#[derive(Clone, Debug, Default)]
35+
pub struct Config {
36+
pub show_patch_on_error: bool,
37+
}
38+
3339
pub fn process_file(
3440
yaml_file: &Path,
3541
out_path: Option<&Path>,
3642
format_config: Option<&Path>,
43+
config: &Config,
3744
) -> Result<()> {
3845
// Load the specified YAML root file
3946
let f = File::open(yaml_file)?;
@@ -61,16 +68,25 @@ pub fn process_file(
6168
let f = File::open(svdpath)?;
6269
let mut contents = String::new();
6370
(&f).read_to_string(&mut contents)?;
64-
let mut config = svd_parser::Config::default();
65-
config.validate_level = ValidateLevel::Disabled;
66-
let mut svd = svd_parser::parse_with_config(&contents, &config)?;
71+
let mut parser_config = svd_parser::Config::default();
72+
parser_config.validate_level = ValidateLevel::Disabled;
73+
let mut svd = svd_parser::parse_with_config(&contents, &parser_config)?;
6774

6875
// Load all included YAML files
6976
yaml_includes(root)?;
7077

7178
// Process device
72-
svd.process(root, true)
73-
.with_context(|| format!("Processing device `{}`", svd.name))?;
79+
svd.process(root, true).with_context(|| {
80+
let name = &svd.name;
81+
let mut out_str = String::new();
82+
let mut emitter = yaml_rust::YamlEmitter::new(&mut out_str);
83+
emitter.dump(&Yaml::Hash(root.clone())).unwrap();
84+
if config.show_patch_on_error {
85+
format!("Processing device `{name}`. Patches looks like:\n{out_str}")
86+
} else {
87+
format!("Processing device `{name}`")
88+
}
89+
})?;
7490

7591
// SVD should now be updated, write it out
7692
let config = get_encoder_config(format_config)?;

src/patch/patch_cli.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use super::Config;
12
use anyhow::Result;
23
use std::path::Path;
34

45
pub fn patch(
56
yaml_file: &Path,
67
out_path: Option<&Path>,
78
format_config: Option<&Path>,
9+
config: &Config,
810
) -> Result<()> {
9-
super::process_file(yaml_file, out_path, format_config)?;
11+
super::process_file(yaml_file, out_path, format_config, config)?;
1012
Ok(())
1113
}

tests/example1.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ fn example1() {
77
let expected_svd_path = test_dir.join("expected.svd");
88
let expected_svd = svd_reader::device(&expected_svd_path).unwrap();
99

10-
patch::patch_cli::patch(&test_dir.join("patch.yaml"), None, None).unwrap();
10+
patch::patch_cli::patch(
11+
&test_dir.join("patch.yaml"),
12+
None,
13+
None,
14+
&Default::default(),
15+
)
16+
.unwrap();
1117
let actual_svd_path = test_dir.join("stm32l4x2.svd.patched");
1218

1319
let actual_svd = svd_reader::device(&actual_svd_path).unwrap();

0 commit comments

Comments
 (0)