Skip to content

Commit

Permalink
Merge pull request #231 from rust-embedded/info
Browse files Browse the repository at this point in the history
initial "info" tool support
  • Loading branch information
Emilgardis authored Jul 1, 2024
2 parents 8f9e019 + 61b44d0 commit df11336
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
30 changes: 29 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::{Ok, Result};
use clap::Parser;
use std::{fs::File, io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::PathBuf, str::FromStr};

use svdtools::{
convert::convert_cli,
html::html_cli,
html::htmlcompare_cli,
info,
interrupts::interrupts_cli,
makedeps::makedeps_cli,
mmap::mmap_cli,
Expand Down Expand Up @@ -123,6 +124,16 @@ enum Command {
/// Path to patched SVD files
svdfiles: Vec<PathBuf>,
},
/// Prints informetion and statistics about SVD file
Info {
/// Path to input file
in_path: PathBuf,
/// Format of input file (XML, JSON or YAML)
#[clap(long = "input-format")]
input_format: Option<convert_cli::InputFormat>,
/// Describe requested information
request: String,
},
}

impl Command {
Expand Down Expand Up @@ -198,6 +209,23 @@ impl Command {
Self::Html { htmldir, svdfiles } => {
html_cli::svd2html(htmldir, svdfiles)?;
}
Self::Info {
in_path,
input_format,
request,
} => {
let request = info::Request::from_str(request)?;
let device = convert_cli::open_svd(
in_path,
*input_format,
convert_cli::ParserConfig {
ignore_enums: true,
..Default::default()
},
)?;
let response = request.process(&device)?;
println!("{response}")
}
}
Ok(())
}
Expand Down
36 changes: 24 additions & 12 deletions src/convert/convert_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use std::io::{Read, Write};
use std::str::FromStr;
use std::{fs::File, path::Path};
use svd_rs::Device;

use crate::get_encoder_config;
pub use crate::ConfigFormat;
Expand Down Expand Up @@ -46,34 +47,25 @@ impl FromStr for OutputFormat {
}
}

#[derive(Clone, Copy, Debug, Default)]
pub struct ParserConfig {
pub expand: bool,
pub expand_properties: bool,
pub ignore_enums: bool,
}

pub fn convert(
pub fn open_svd(
in_path: &Path,
out_path: &Path,
input_format: Option<InputFormat>,
output_format: Option<OutputFormat>,
parser_config: ParserConfig,
format_config: Option<&Path>,
) -> Result<()> {
) -> Result<Device> {
let input_format = match input_format {
None => match in_path.extension().and_then(|e| e.to_str()) {
Some(s) => InputFormat::from_str(s)?,
_ => return Err(anyhow!("Unknown input file format")),
},
Some(t) => t,
};
let output_format = match output_format {
None => match out_path.extension().and_then(|e| e.to_str()) {
Some(s) => OutputFormat::from_str(s)?,
_ => return Err(anyhow!("Unknown output file format")),
},
Some(t) => t,
};

let mut input = String::new();
File::open(in_path)?.read_to_string(&mut input)?;
Expand All @@ -94,6 +86,26 @@ pub fn convert(
} else {
device
};
Ok(device)
}

pub fn convert(
in_path: &Path,
out_path: &Path,
input_format: Option<InputFormat>,
output_format: Option<OutputFormat>,
parser_config: ParserConfig,
format_config: Option<&Path>,
) -> Result<()> {
let device = open_svd(in_path, input_format, parser_config)?;

let output_format = match output_format {
None => match out_path.extension().and_then(|e| e.to_str()) {
Some(s) => OutputFormat::from_str(s)?,
_ => return Err(anyhow!("Unknown output file format")),
},
Some(t) => t,
};

let config = get_encoder_config(format_config)?;

Expand Down
28 changes: 28 additions & 0 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::str::FromStr;

use anyhow::Ok;
use svd_rs::Device;

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Request {
DeviceName,
}

impl FromStr for Request {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"device-name" => Ok(Self::DeviceName),
_ => Err(anyhow::anyhow!("Unknown info request: {s}")),
}
}
}

impl Request {
pub fn process(&self, device: &Device) -> anyhow::Result<String> {
match self {
Self::DeviceName => Ok(device.name.to_string()),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{fs::File, path::Path, str::FromStr};
pub mod common;
pub mod convert;
pub mod html;
pub mod info;
pub mod interrupts;
pub mod makedeps;
pub mod mmap;
Expand Down
4 changes: 2 additions & 2 deletions src/patch/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ impl DeviceExt for Device {
.enumerate()
.find(|(_, f)| f.name == pnew)
.ok_or_else(|| anyhow!("peripheral {pnew} not found"))?;
d.name = new.name.clone();
d.name.clone_from(&new.name);
d.base_address = new.base_address;
d.interrupt = new.interrupt.clone();
d.interrupt.clone_from(&new.interrupt);
*new = d;
for (i, p) in self.peripherals.iter_mut().enumerate() {
if p.derived_from.as_deref() == Some(pold) {
Expand Down
2 changes: 2 additions & 0 deletions src/patch/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub(crate) trait RegisterBlockExt: Name {
fn get_mut_reg(&mut self, name: &str) -> Option<&mut Register>;

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

/// Register/cluster block
Expand Down Expand Up @@ -402,6 +403,7 @@ pub(crate) trait RegisterBlockExt: Name {
}
}
/// Work through a register, handling all fields
#[allow(unused)]
fn process_register(
&mut self,
rspec: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/patch/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl RegisterExt for Register {
for f in fields.iter_mut() {
if names.contains(&f.name) {
if first {
desc = f.description.clone();
desc.clone_from(&f.description);
first = false;
}
bitwidth += f.bit_range.width;
Expand Down

0 comments on commit df11336

Please sign in to comment.