Skip to content

Commit 3f9f951

Browse files
committed
patch: allow yaml and json file in _copy
1 parent ae7b978 commit 3f9f951

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/patch/device.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ use svd_parser::svd::{Device, Peripheral, PeripheralInfo};
44
use yaml_rust::{yaml::Hash, Yaml};
55

66
use std::collections::HashSet;
7-
use std::{fs::File, io::Read, path::Path};
7+
use std::str::FromStr;
8+
use std::{fs, path::Path};
89

910
use super::iterators::{MatchIter, Matched};
1011
use super::peripheral::{PeripheralExt, RegisterBlockExt};
1112
use super::yaml_ext::{AsType, GetVal};
1213
use super::{abspath, adding_pos, matchname, Config, PatchResult, Spec, VAL_LVL};
1314
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
1415
use super::{make_dim_element, modify_dim_element, modify_register_properties};
16+
use crate::common::input_format::InputFormat;
1517

1618
pub type PerMatchIterMut<'a, 'b> = MatchIter<'b, std::slice::IterMut<'a, Peripheral>>;
1719

@@ -195,11 +197,24 @@ impl DeviceExt for Device {
195197
.collect::<Vec<_>>();
196198
let mut new = match pcopysrc.as_slice() {
197199
[ppath, pcopyname] => {
198-
let f = File::open(abspath(path, Path::new(ppath))?)?;
199-
let mut contents = String::new();
200-
(&f).read_to_string(&mut contents).unwrap();
201-
let filedev = svd_parser::parse(&contents)
202-
.with_context(|| format!("Parsing file {contents}"))?;
200+
let ppath = Path::new(ppath);
201+
let input_format = ppath
202+
.extension()
203+
.map(|ext_os| ext_os.to_str().expect("ppath is str"))
204+
.and_then(|ext| InputFormat::from_str(ext).ok())
205+
.unwrap_or(InputFormat::Xml);
206+
let filepath = abspath(path, ppath)?;
207+
let contents = fs::read_to_string(filepath)?;
208+
let filedev = match input_format {
209+
InputFormat::Xml => svd_parser::parse(&contents)
210+
.with_context(|| format!("Parsing svd file {contents}"))?,
211+
#[cfg(feature = "yaml")]
212+
InputFormat::Yaml => serde_yaml::from_str(&contents)
213+
.with_context(|| format!("Parsing yaml file {contents}"))?,
214+
#[cfg(feature = "json")]
215+
InputFormat::Json => serde_json::from_str(&contents)
216+
.with_context(|| format!("Parsing json file {contents}"))?,
217+
};
203218
filedev
204219
.get_peripheral(pcopyname)
205220
.ok_or_else(|| {

0 commit comments

Comments
 (0)