Skip to content

Commit f3b83ad

Browse files
authored
Merge pull request #147 from stm32-rs/sorting
encode config for patch
2 parents 8163079 + 16d9ebc commit f3b83ad

File tree

8 files changed

+96
-51
lines changed

8 files changed

+96
-51
lines changed

CHANGELOG-rust.md

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

66
## [Unreleased]
77

8+
* update `svd-encoder`, `--format-config` and optional `out_path` for `patch`
89
* add field name in enumeratedValues derive path
910

1011
## [v0.3.0] 2023-03-27

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ edition = "2021"
2727
clap = { version = "4.1", features = ["derive", "cargo", "color"] }
2828
serde = { version = "1.0", features = ["derive"] }
2929
quick-xml = { version = "0.28", features = ["serialize"] }
30-
svd-rs = { version = "0.14.1", features = ["serde", "derive-from"] }
30+
svd-rs = { version = "0.14.2", features = ["serde", "derive-from"] }
3131
svd-parser = { version = "0.14.1", features = ["expand"] }
32-
svd-encoder = "0.14.1"
32+
svd-encoder = "0.14.3"
3333
yaml-rust = "0.4"
3434
# serde_yaml 0.9.x looks broken
3535
serde_yaml = "0.8.26"

src/cli.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ enum Command {
1414
Patch {
1515
/// Path to input YAML file
1616
yaml_file: PathBuf,
17+
18+
/// Path to output file. By default it just adds `.patched` at the end
19+
out_path: Option<PathBuf>,
20+
21+
/// Path to format config file
22+
///
23+
/// If not specified, the default format config will be used.
24+
#[clap(long)]
25+
format_config: Option<PathBuf>,
1726
},
1827
/// Generate Make dependency file listing dependencies for a YAML file.
1928
Makedeps {
@@ -82,7 +91,13 @@ impl Command {
8291
interrupts_cli::parse_device(svd_file, !no_gaps)?;
8392
}
8493
Self::Mmap { svd_file } => mmap_cli::parse_device(svd_file)?,
85-
Self::Patch { yaml_file } => patch_cli::patch(yaml_file)?,
94+
Self::Patch {
95+
yaml_file,
96+
out_path,
97+
format_config,
98+
} => {
99+
patch_cli::patch(yaml_file, out_path.as_deref(), format_config.as_deref())?;
100+
}
86101
Self::Makedeps {
87102
yaml_file,
88103
deps_file,

src/convert/convert_cli.rs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use std::io::{Read, Write};
33
use std::str::FromStr;
44
use std::{fs::File, path::Path};
55

6+
use crate::get_encoder_config;
7+
pub use crate::ConfigFormat;
8+
69
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
710
#[non_exhaustive]
811
pub enum InputFormat {
@@ -43,24 +46,6 @@ impl FromStr for OutputFormat {
4346
}
4447
}
4548

46-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47-
#[non_exhaustive]
48-
pub enum ConfigFormat {
49-
Yaml,
50-
Json,
51-
}
52-
53-
impl FromStr for ConfigFormat {
54-
type Err = anyhow::Error;
55-
fn from_str(s: &str) -> Result<Self, Self::Err> {
56-
match s {
57-
"yml" | "yaml" | "YAML" => Ok(Self::Yaml),
58-
"json" | "JSON" => Ok(Self::Json),
59-
_ => Err(anyhow!("Unknown config file format")),
60-
}
61-
}
62-
}
63-
6449
pub struct ParserConfig {
6550
pub expand: bool,
6651
pub expand_properties: bool,
@@ -110,28 +95,7 @@ pub fn convert(
11095
device
11196
};
11297

113-
let config = if let Some(format_config) = format_config {
114-
let config_format = match format_config.extension().and_then(|e| e.to_str()) {
115-
Some(s) => ConfigFormat::from_str(s)?,
116-
_ => return Err(anyhow!("Unknown output file format")),
117-
};
118-
let mut config = String::new();
119-
File::open(format_config)?.read_to_string(&mut config)?;
120-
121-
let config_map: std::collections::HashMap<String, String> = match config_format {
122-
ConfigFormat::Yaml => serde_yaml::from_str(&config)?,
123-
ConfigFormat::Json => serde_json::from_str(&config)?,
124-
};
125-
126-
let mut config = svd_encoder::Config::default();
127-
config_map
128-
.iter()
129-
.for_each(|(name, value)| config.update(name, value));
130-
131-
config
132-
} else {
133-
svd_encoder::Config::default()
134-
};
98+
let config = get_encoder_config(format_config)?;
13599

136100
let output = match output_format {
137101
OutputFormat::Xml => svd_encoder::encode_with_config(&device, &config)?,

src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
1+
use anyhow::anyhow;
2+
use std::io::Read;
3+
use std::{fs::File, path::Path, str::FromStr};
4+
15
pub mod common;
26
pub mod convert;
37
pub mod interrupts;
48
pub mod makedeps;
59
pub mod mmap;
610
pub mod patch;
711

12+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13+
#[non_exhaustive]
14+
pub enum ConfigFormat {
15+
Yaml,
16+
Json,
17+
}
18+
19+
impl FromStr for ConfigFormat {
20+
type Err = anyhow::Error;
21+
fn from_str(s: &str) -> Result<Self, Self::Err> {
22+
match s {
23+
"yml" | "yaml" | "YAML" => Ok(Self::Yaml),
24+
"json" | "JSON" => Ok(Self::Json),
25+
_ => Err(anyhow!("Unknown config file format")),
26+
}
27+
}
28+
}
29+
30+
pub(crate) fn get_encoder_config(
31+
format_config: Option<&Path>,
32+
) -> anyhow::Result<svd_encoder::Config> {
33+
Ok(if let Some(format_config) = format_config {
34+
let config_format = match format_config.extension().and_then(|e| e.to_str()) {
35+
Some(s) => ConfigFormat::from_str(s)?,
36+
_ => return Err(anyhow!("Unknown output file format")),
37+
};
38+
let mut config = String::new();
39+
File::open(format_config)?.read_to_string(&mut config)?;
40+
41+
let config_map: std::collections::HashMap<String, String> = match config_format {
42+
ConfigFormat::Yaml => serde_yaml::from_str(&config)?,
43+
ConfigFormat::Json => serde_json::from_str(&config)?,
44+
};
45+
46+
let mut config = svd_encoder::Config::default();
47+
config_map
48+
.iter()
49+
.for_each(|(name, value)| config.update(name, value));
50+
51+
config
52+
} else {
53+
svd_encoder::Config::default()
54+
})
55+
}
56+
857
#[cfg(test)]
958
mod test_utils;

src/patch/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ mod register;
2626
mod yaml_ext;
2727
use yaml_ext::{AsType, GetVal, ToYaml};
2828

29+
use crate::get_encoder_config;
30+
2931
const VAL_LVL: ValidateLevel = ValidateLevel::Weak;
3032

31-
pub fn process_file(yaml_file: &Path) -> Result<()> {
33+
pub fn process_file(
34+
yaml_file: &Path,
35+
out_path: Option<&Path>,
36+
format_config: Option<&Path>,
37+
) -> Result<()> {
3238
// Load the specified YAML root file
3339
let f = File::open(yaml_file)?;
3440
let mut contents = String::new();
@@ -45,8 +51,13 @@ pub fn process_file(yaml_file: &Path) -> Result<()> {
4551
.ok_or_else(|| anyhow!("You must have an svd key in the root YAML file"))?,
4652
),
4753
)?;
48-
let mut svdpath_out = svdpath.clone();
49-
svdpath_out.set_extension("svd.patched");
54+
let svdpath_out = if let Some(out_path) = out_path {
55+
out_path.to_owned()
56+
} else {
57+
let mut pth = svdpath.clone();
58+
pth.set_extension("svd.patched");
59+
pth
60+
};
5061
let f = File::open(svdpath)?;
5162
let mut contents = String::new();
5263
(&f).read_to_string(&mut contents)?;
@@ -62,9 +73,10 @@ pub fn process_file(yaml_file: &Path) -> Result<()> {
6273
.with_context(|| format!("Processing device `{}`", svd.name))?;
6374

6475
// SVD should now be updated, write it out
65-
let svd_out = svd_encoder::encode(&svd)?;
76+
let config = get_encoder_config(format_config)?;
77+
let svd_out = svd_encoder::encode_with_config(&svd, &config)?;
6678

67-
let mut f = File::create(&svdpath_out)?;
79+
let mut f = File::create(svdpath_out)?;
6880
f.write_all(svd_out.as_bytes())?;
6981

7082
Ok(())

src/patch/patch_cli.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use anyhow::Result;
22
use std::path::Path;
33

4-
pub fn patch(yaml_file: &Path) -> Result<()> {
5-
super::process_file(yaml_file)?;
4+
pub fn patch(
5+
yaml_file: &Path,
6+
out_path: Option<&Path>,
7+
format_config: Option<&Path>,
8+
) -> Result<()> {
9+
super::process_file(yaml_file, out_path, format_config)?;
610
Ok(())
711
}

tests/example1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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")).unwrap();
10+
patch::patch_cli::patch(&test_dir.join("patch.yaml"), None, None).unwrap();
1111
let actual_svd_path = test_dir.join("stm32l4x2.svd.patched");
1212

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

0 commit comments

Comments
 (0)