Skip to content

Commit df11336

Browse files
authored
Merge pull request #231 from rust-embedded/info
initial "info" tool support
2 parents 8f9e019 + 61b44d0 commit df11336

File tree

7 files changed

+87
-16
lines changed

7 files changed

+87
-16
lines changed

src/cli.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use anyhow::{Ok, Result};
22
use clap::Parser;
3-
use std::{fs::File, io::Write, path::PathBuf};
3+
use std::{fs::File, io::Write, path::PathBuf, str::FromStr};
44

55
use svdtools::{
66
convert::convert_cli,
77
html::html_cli,
88
html::htmlcompare_cli,
9+
info,
910
interrupts::interrupts_cli,
1011
makedeps::makedeps_cli,
1112
mmap::mmap_cli,
@@ -123,6 +124,16 @@ enum Command {
123124
/// Path to patched SVD files
124125
svdfiles: Vec<PathBuf>,
125126
},
127+
/// Prints informetion and statistics about SVD file
128+
Info {
129+
/// Path to input file
130+
in_path: PathBuf,
131+
/// Format of input file (XML, JSON or YAML)
132+
#[clap(long = "input-format")]
133+
input_format: Option<convert_cli::InputFormat>,
134+
/// Describe requested information
135+
request: String,
136+
},
126137
}
127138

128139
impl Command {
@@ -198,6 +209,23 @@ impl Command {
198209
Self::Html { htmldir, svdfiles } => {
199210
html_cli::svd2html(htmldir, svdfiles)?;
200211
}
212+
Self::Info {
213+
in_path,
214+
input_format,
215+
request,
216+
} => {
217+
let request = info::Request::from_str(request)?;
218+
let device = convert_cli::open_svd(
219+
in_path,
220+
*input_format,
221+
convert_cli::ParserConfig {
222+
ignore_enums: true,
223+
..Default::default()
224+
},
225+
)?;
226+
let response = request.process(&device)?;
227+
println!("{response}")
228+
}
201229
}
202230
Ok(())
203231
}

src/convert/convert_cli.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
22
use std::io::{Read, Write};
33
use std::str::FromStr;
44
use std::{fs::File, path::Path};
5+
use svd_rs::Device;
56

67
use crate::get_encoder_config;
78
pub use crate::ConfigFormat;
@@ -46,34 +47,25 @@ impl FromStr for OutputFormat {
4647
}
4748
}
4849

50+
#[derive(Clone, Copy, Debug, Default)]
4951
pub struct ParserConfig {
5052
pub expand: bool,
5153
pub expand_properties: bool,
5254
pub ignore_enums: bool,
5355
}
5456

55-
pub fn convert(
57+
pub fn open_svd(
5658
in_path: &Path,
57-
out_path: &Path,
5859
input_format: Option<InputFormat>,
59-
output_format: Option<OutputFormat>,
6060
parser_config: ParserConfig,
61-
format_config: Option<&Path>,
62-
) -> Result<()> {
61+
) -> Result<Device> {
6362
let input_format = match input_format {
6463
None => match in_path.extension().and_then(|e| e.to_str()) {
6564
Some(s) => InputFormat::from_str(s)?,
6665
_ => return Err(anyhow!("Unknown input file format")),
6766
},
6867
Some(t) => t,
6968
};
70-
let output_format = match output_format {
71-
None => match out_path.extension().and_then(|e| e.to_str()) {
72-
Some(s) => OutputFormat::from_str(s)?,
73-
_ => return Err(anyhow!("Unknown output file format")),
74-
},
75-
Some(t) => t,
76-
};
7769

7870
let mut input = String::new();
7971
File::open(in_path)?.read_to_string(&mut input)?;
@@ -94,6 +86,26 @@ pub fn convert(
9486
} else {
9587
device
9688
};
89+
Ok(device)
90+
}
91+
92+
pub fn convert(
93+
in_path: &Path,
94+
out_path: &Path,
95+
input_format: Option<InputFormat>,
96+
output_format: Option<OutputFormat>,
97+
parser_config: ParserConfig,
98+
format_config: Option<&Path>,
99+
) -> Result<()> {
100+
let device = open_svd(in_path, input_format, parser_config)?;
101+
102+
let output_format = match output_format {
103+
None => match out_path.extension().and_then(|e| e.to_str()) {
104+
Some(s) => OutputFormat::from_str(s)?,
105+
_ => return Err(anyhow!("Unknown output file format")),
106+
},
107+
Some(t) => t,
108+
};
97109

98110
let config = get_encoder_config(format_config)?;
99111

src/info/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::str::FromStr;
2+
3+
use anyhow::Ok;
4+
use svd_rs::Device;
5+
6+
#[derive(Clone, Debug)]
7+
#[non_exhaustive]
8+
pub enum Request {
9+
DeviceName,
10+
}
11+
12+
impl FromStr for Request {
13+
type Err = anyhow::Error;
14+
fn from_str(s: &str) -> Result<Self, Self::Err> {
15+
match s {
16+
"device-name" => Ok(Self::DeviceName),
17+
_ => Err(anyhow::anyhow!("Unknown info request: {s}")),
18+
}
19+
}
20+
}
21+
22+
impl Request {
23+
pub fn process(&self, device: &Device) -> anyhow::Result<String> {
24+
match self {
25+
Self::DeviceName => Ok(device.name.to_string()),
26+
}
27+
}
28+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{fs::File, path::Path, str::FromStr};
55
pub mod common;
66
pub mod convert;
77
pub mod html;
8+
pub mod info;
89
pub mod interrupts;
910
pub mod makedeps;
1011
pub mod mmap;

src/patch/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ impl DeviceExt for Device {
410410
.enumerate()
411411
.find(|(_, f)| f.name == pnew)
412412
.ok_or_else(|| anyhow!("peripheral {pnew} not found"))?;
413-
d.name = new.name.clone();
413+
d.name.clone_from(&new.name);
414414
d.base_address = new.base_address;
415-
d.interrupt = new.interrupt.clone();
415+
d.interrupt.clone_from(&new.interrupt);
416416
*new = d;
417417
for (i, p) in self.peripherals.iter_mut().enumerate() {
418418
if p.derived_from.as_deref() == Some(pold) {

src/patch/peripheral.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub(crate) trait RegisterBlockExt: Name {
132132
fn get_mut_reg(&mut self, name: &str) -> Option<&mut Register>;
133133

134134
/// Register/cluster block
135+
#[allow(unused)]
135136
fn children(&self) -> Option<&Vec<RegisterCluster>>;
136137

137138
/// Register/cluster block
@@ -402,6 +403,7 @@ pub(crate) trait RegisterBlockExt: Name {
402403
}
403404
}
404405
/// Work through a register, handling all fields
406+
#[allow(unused)]
405407
fn process_register(
406408
&mut self,
407409
rspec: &str,

src/patch/register.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl RegisterExt for Register {
389389
for f in fields.iter_mut() {
390390
if names.contains(&f.name) {
391391
if first {
392-
desc = f.description.clone();
392+
desc.clone_from(&f.description);
393393
first = false;
394394
}
395395
bitwidth += f.bit_range.width;

0 commit comments

Comments
 (0)