Skip to content

Commit

Permalink
initial "info" tool support
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jun 28, 2024
1 parent 8f9e019 commit e420b75
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 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

0 comments on commit e420b75

Please sign in to comment.