Skip to content

Commit 882986d

Browse files
committed
patch: allow yaml and json file in _svd
1 parent 0ad6714 commit 882986d

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

src/patch/mod.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::borrow::Cow;
77
use std::fs::File;
88
use std::io::{Cursor, Read};
99
use std::path::{Path, PathBuf};
10+
use std::str::FromStr;
1011
use svd_parser::expand::{BlockPath, FieldPath, RegisterPath};
1112
use svd_parser::svd::{
1213
addressblock::AddressBlockBuilder, interrupt::InterruptBuilder, Access, AddressBlock,
@@ -17,7 +18,7 @@ use svd_parser::svd::{
1718
WriteConstraintRange,
1819
};
1920
use svd_parser::SVDError::DimIndexParse;
20-
use svd_rs::{BitRange, DimArrayIndex, DimElement, DimElementBuilder, MaybeArray};
21+
use svd_rs::{BitRange, Device, DimArrayIndex, DimElement, DimElementBuilder, MaybeArray};
2122
use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
2223

2324
use hashlink::linked_hash_map;
@@ -35,6 +36,7 @@ mod register;
3536
mod yaml_ext;
3637
use yaml_ext::{AsType, GetVal, ToYaml};
3738

39+
use crate::convert::convert_cli::InputFormat;
3840
use crate::get_encoder_config;
3941

4042
const VAL_LVL: ValidateLevel = ValidateLevel::Weak;
@@ -115,8 +117,30 @@ pub fn process_file(
115117

116118
let encoder_config = get_encoder_config(format_config)?;
117119

118-
let mut svd_out = process_reader(File::open(svdpath)?, &doc, &encoder_config, config)?;
119-
std::io::copy(&mut svd_out, &mut File::create(svdpath_out)?)?;
120+
let input_format = svdpath
121+
.extension()
122+
.map(|ext_os| ext_os.to_str().expect("_svd is str"))
123+
.and_then(|ext| InputFormat::from_str(ext).ok())
124+
.unwrap_or(InputFormat::Xml);
125+
126+
match input_format {
127+
InputFormat::Xml => {
128+
let mut svd_out = process_reader(File::open(svdpath)?, &doc, &encoder_config, config)?;
129+
std::io::copy(&mut svd_out, &mut File::create(svdpath_out)?)?;
130+
}
131+
#[cfg(feature = "yaml")]
132+
InputFormat::Yaml => {
133+
let dev = serde_yaml::from_str(&std::fs::read_to_string(svdpath)?)?;
134+
let mut svd_out = process_device(dev, &doc, &encoder_config, config)?;
135+
std::io::copy(&mut svd_out, &mut File::create(svdpath_out)?)?;
136+
}
137+
#[cfg(feature = "json")]
138+
InputFormat::Json => {
139+
let dev = serde_json::from_str(&std::fs::read_to_string(svdpath)?)?;
140+
let mut svd_out = process_device(dev, &doc, &encoder_config, config)?;
141+
std::io::copy(&mut svd_out, &mut File::create(svdpath_out)?)?;
142+
}
143+
};
120144

121145
Ok(())
122146
}
@@ -131,8 +155,16 @@ pub fn process_reader<R: Read>(
131155
svd.read_to_string(&mut contents)?;
132156
let mut parser_config = svd_parser::Config::default();
133157
parser_config.validate_level = ValidateLevel::Disabled;
134-
let mut dev = svd_parser::parse_with_config(&contents, &parser_config)?;
158+
let dev = svd_parser::parse_with_config(&contents, &parser_config)?;
159+
process_device(dev, patch, format_config, config)
160+
}
135161

162+
fn process_device(
163+
mut dev: Device,
164+
patch: &Yaml,
165+
format_config: &EncoderConfig,
166+
config: &Config,
167+
) -> Result<impl Read> {
136168
// Process device
137169
dev.process(patch.hash()?, config).with_context(|| {
138170
let name = &dev.name;

0 commit comments

Comments
 (0)