eliminate an instance of clap

This commit is contained in:
bunnie 2022-09-29 22:12:58 +08:00
parent 8af0ef7961
commit 7226b57a2f
4 changed files with 10 additions and 92 deletions

2
Cargo.lock generated
View file

@ -3699,8 +3699,6 @@ dependencies = [
name = "svd2utra"
version = "0.1.0"
dependencies = [
"anyhow",
"clap 2.34.0",
"convert_case",
"quick-xml",
]

View file

@ -11,6 +11,4 @@ homepage = "https://betrusted.io/xous-book/"
# Dependency versions enforced by Cargo.lock.
[dependencies]
quick-xml = "0.19.0"
clap = "2.33.3"
anyhow = "1.0.43"
convert_case = "0.4.0"

View file

@ -1,51 +0,0 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
struct IgnoredError {}
impl<E> From<E> for IgnoredError
where
E: Error,
{
fn from(_: E) -> IgnoredError {
IgnoredError {}
}
}
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
}
fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim(), date.trim()),
_ => String::new(),
}
}
fn commit_hash() -> Result<String, IgnoredError> {
Ok(String::from_utf8(
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()?
.stdout,
)?)
}
fn commit_date() -> Result<String, IgnoredError> {
Ok(String::from_utf8(
Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()?
.stdout,
)?)
}

View file

@ -1,44 +1,17 @@
mod generate;
use anyhow::Context;
use clap::{App, Arg};
use std::fs::File;
use std::io::{Read, Write};
fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::args().count() != 3 {
println!("Usage: svd2utra <input SVD> <output utra>");
return Ok(())
}
let svd_filename = std::env::args().nth(1).ok_or("Must specify SVD input filename")?;
let generated_filename = std::env::args().nth(2).ok_or("Must specify destination utralib filename")?;
fn main() -> anyhow::Result<()> {
let matches = App::new("svd2utra")
.about("Generate a UTRA Rust API from SVD files")
.arg(
Arg::with_name("input")
.help("Input SVD file")
.short("i")
.takes_value(true)
.value_name("FILE"),
)
.arg(
Arg::with_name("output")
.help("Output .rs file or crate directory")
.short("o")
.takes_value(true)
.value_name("FILE"),
)
.version(concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
))
.get_matches();
let src_file = std::fs::File::open(svd_filename).expect("couldn't open src file");
let mut dest_file = std::fs::File::create(generated_filename).expect("couldn't open dest file");
let src: Box<dyn Read> = match matches.value_of("input") {
Some(file) => Box::new(File::open(file).context("Cannot open the SVD file")?),
None => Box::new(std::io::stdin()),
};
let mut dest: Box<dyn Write> = match matches.value_of("output") {
None => Box::new(std::io::stdout()),
Some(path) => Box::new(File::open(path).context("Cannot open destination file")?),
};
generate::generate(src, &mut dest).context("Cannot generate output file")?;
generate::generate(src_file, &mut dest_file)?;
Ok(())
}