Skip to content

Commit ec20a00

Browse files
committed
add possibility to use config file
1 parent 43ea98a commit ec20a00

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ path = "src/main.rs"
3434
[dependencies]
3535
cast = "0.2"
3636
clap = "2.33"
37+
clap_conf = "0.1.5"
3738
env_logger = "~0.7"
3839
inflections = "1.1"
3940
log = { version = "~0.4", features = ["std"] }

src/main.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use clap::{App, Arg};
1616
use crate::util::{build_rs, Config, Target};
1717

1818
fn run() -> Result<()> {
19+
use clap_conf::prelude::*;
1920
use std::io::Read;
2021

2122
let matches =
@@ -28,6 +29,13 @@ fn run() -> Result<()> {
2829
.takes_value(true)
2930
.value_name("FILE"),
3031
)
32+
.arg(
33+
Arg::with_name("config")
34+
.help("Config TOML file")
35+
.short("c")
36+
.takes_value(true)
37+
.value_name("TOML_FILE"),
38+
)
3139
.arg(
3240
Arg::with_name("target")
3341
.long("target")
@@ -79,11 +87,6 @@ fn run() -> Result<()> {
7987

8088
setup_logging(&matches);
8189

82-
let target = matches
83-
.value_of("target")
84-
.map(|s| Target::parse(s))
85-
.unwrap_or_else(|| Ok(Target::default()))?;
86-
8790
let xml = &mut String::new();
8891
match matches.value_of("input") {
8992
Some(file) => {
@@ -103,15 +106,36 @@ fn run() -> Result<()> {
103106

104107
let device = svd::parse(xml)?;
105108

106-
let make_mod = matches.is_present("make_mod");
109+
let config_filename = matches.value_of("config").unwrap_or("");
110+
111+
let cfg = with_toml_env(&matches, &[config_filename, "svd2rust.toml"]);
112+
113+
let target = cfg
114+
.grab()
115+
.arg("target")
116+
.conf("target")
117+
.done()
118+
.map(|s| Target::parse(&s))
119+
.unwrap_or_else(|| Ok(Target::default()))?;
120+
121+
let nightly =
122+
cfg.bool_flag("nightly_features", Filter::Arg) || cfg.bool_flag("nightly", Filter::Conf);
123+
let generic_mod =
124+
cfg.bool_flag("generic_mod", Filter::Arg) || cfg.bool_flag("generic_mod", Filter::Conf);
125+
let make_mod =
126+
cfg.bool_flag("make_mod", Filter::Arg) || cfg.bool_flag("make_mod", Filter::Conf);
127+
let const_generic =
128+
cfg.bool_flag("const_generic", Filter::Arg) || cfg.bool_flag("const_generic", Filter::Conf);
129+
let ignore_groups =
130+
cfg.bool_flag("ignore_groups", Filter::Arg) || cfg.bool_flag("ignore_groups", Filter::Conf);
107131

108132
let config = Config {
109133
target,
110-
nightly: matches.is_present("nightly_features"),
111-
generic_mod: matches.is_present("generic_mod"),
134+
nightly,
135+
generic_mod,
112136
make_mod,
113-
const_generic: matches.is_present("const_generic"),
114-
ignore_groups: matches.is_present("ignore_groups"),
137+
const_generic,
138+
ignore_groups,
115139
};
116140

117141
let mut device_x = String::new();

src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub const BITS_PER_BYTE: u32 = 8;
1313
/// that are not valid in Rust ident
1414
const BLACKLIST_CHARS: &[char] = &['(', ')', '[', ']', '/', ' ', '-'];
1515

16-
#[derive(Clone, Copy, PartialEq, Default)]
16+
#[derive(Clone, Copy, PartialEq, Default, Debug)]
1717
pub struct Config {
1818
pub target: Target,
1919
pub nightly: bool,
@@ -25,7 +25,7 @@ pub struct Config {
2525

2626
#[allow(clippy::upper_case_acronyms)]
2727
#[allow(non_camel_case_types)]
28-
#[derive(Clone, Copy, PartialEq)]
28+
#[derive(Clone, Copy, PartialEq, Debug)]
2929
pub enum Target {
3030
CortexM,
3131
Msp430,

0 commit comments

Comments
 (0)