Skip to content

Commit 3254731

Browse files
committed
feat: convert: Add format_config option
1 parent 664c63d commit 3254731

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
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+
* `convert`: Add `format_config` option
9+
810
## [v0.2.4] 2022-05-15
911

1012
* Added action to build binaries and release for every version tag and latest commit

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ edition = "2021"
2727
clap = { version = "3.0", features = ["derive"] }
2828
serde = { version = "1.0", features = ["derive"] }
2929
quick-xml = { version = "0.18", features = ["serialize"] }
30-
svd-rs = { version = "0.13.2", features = ["serde", "derive-from"] }
31-
svd-parser = { version = "0.13.4", features = ["expand"] }
32-
svd-encoder = "0.13.1"
30+
svd-rs = { version = "0.14.0", features = ["serde", "derive-from"] }
31+
svd-parser = { version = "0.14.0", features = ["expand"] }
32+
svd-encoder = "0.14.0"
3333
yaml-rust = "0.4"
3434
serde_yaml = "0.8.23"
3535
serde_json = { version = "1.0", features = ["preserve_order"] }

src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ enum Command {
7171
/// Skip enumeratedValues and writeConstraints during parsing (XML input only)
7272
#[clap(long)]
7373
ignore_enums: bool,
74+
75+
/// Path to format config file
76+
///
77+
/// If not specified, the default format config will be used.
78+
///
79+
/// Only used for SVD output format.
80+
#[clap(long = "format-config", parse(from_os_str))]
81+
format_config: Option<PathBuf>,
7482
},
7583
}
7684

@@ -94,6 +102,7 @@ impl Command {
94102
expand,
95103
expand_properties,
96104
ignore_enums,
105+
format_config,
97106
} => convert_cli::convert(
98107
in_path,
99108
out_path,
@@ -102,6 +111,7 @@ impl Command {
102111
*expand,
103112
*expand_properties,
104113
*ignore_enums,
114+
format_config.as_ref().map(|p| p.as_path()),
105115
)?,
106116
}
107117
Ok(())

src/convert/convert_cli.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ impl FromStr for OutputFormat {
4343
}
4444
}
4545

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+
4664
pub fn convert(
4765
in_path: &Path,
4866
out_path: &Path,
@@ -51,6 +69,7 @@ pub fn convert(
5169
expand: bool,
5270
expand_properties: bool,
5371
ignore_enums: bool,
72+
format_config: Option<&Path>,
5473
) -> Result<()> {
5574
let input_format = match input_format {
5675
None => match in_path.extension().and_then(|e| e.to_str()) {
@@ -87,8 +106,31 @@ pub fn convert(
87106
device
88107
};
89108

109+
let config = if let Some(format_config) = format_config {
110+
let config_format = match format_config.extension().and_then(|e| e.to_str()) {
111+
Some(s) => ConfigFormat::from_str(s)?,
112+
_ => return Err(anyhow!("Unknown output file format")),
113+
};
114+
let mut config = String::new();
115+
File::open(format_config)?.read_to_string(&mut config)?;
116+
117+
let config_map: std::collections::HashMap<String, String> = match config_format {
118+
ConfigFormat::Yaml => serde_yaml::from_str(&config)?,
119+
ConfigFormat::Json => serde_json::from_str(&config)?,
120+
};
121+
122+
let mut config = svd_encoder::Config::default();
123+
config_map
124+
.iter()
125+
.for_each(|(name, value)| config.update(name, value));
126+
127+
config
128+
} else {
129+
svd_encoder::Config::default()
130+
};
131+
90132
let output = match output_format {
91-
OutputFormat::Xml => svd_encoder::encode(&device)?,
133+
OutputFormat::Xml => svd_encoder::encode_with_config(&device, &config)?,
92134
OutputFormat::Yaml => serde_yaml::to_string(&device)?,
93135
OutputFormat::Json => serde_json::to_string_pretty(&device)?,
94136
};

0 commit comments

Comments
 (0)