Skip to content

Commit 7b7aa88

Browse files
committed
Make CLI optional (for library usage)
This is made by adding a "bin" feature that's enabled by default, inspired by what was done in rust-embedded/svd2rust#619. Additionally, support for JSON and YAML configs has also been made optional but default. This includes what should be the only behavior change: if neither were enabled at build-time, passing a config file is an error. The motivation came from Rahix/avr-device#201, but this should be a generally beneficial change for everybody. Since some of the features of the CLI aren't very useful when using svdtools as a library, they just vanish without the "bin" feature. This is for two reasons: one is to reduce diff size, and two is to avoid having to refactor all capabilities to have a library equivalent. There is little disadvantage to this, for instance in avr-device we only use the patching functionality (which is kept), and there's little prospect of needing more.
1 parent 59e01eb commit 7b7aa88

File tree

9 files changed

+72
-10
lines changed

9 files changed

+72
-10
lines changed

Cargo.toml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ edition = "2021"
2626
rust-version = "1.70"
2727

2828
[dependencies]
29-
clap = { version = "4.5", features = ["derive", "cargo", "color"] }
29+
clap = { version = "4.5", features = ["derive", "cargo", "color"], optional = true }
3030
serde = { version = "1.0", features = ["derive"] }
3131
quick-xml = { version = "0.37", features = ["serialize"] }
3232
svd-rs = { version = "0.14.12", features = ["serde", "derive-from"] }
3333
svd-parser = { version = "0.14.9", features = ["expand"] }
3434
svd-encoder = "0.14.7"
3535
# serde_yaml 0.9.x looks broken
36-
serde_yaml = "0.8.26"
37-
serde_json = { version = "1.0", features = ["preserve_order"] }
36+
serde_yaml = { version = "0.8.26", optional = true }
37+
serde_json = { version = "1.0", features = ["preserve_order"], optional = true }
3838
anyhow = "1.0.97"
3939
thiserror = "1.0.35"
4040
hashlink = "0.10.0"
4141
globset = "0.4.16"
4242
commands = "0.0.5"
43-
env_logger = "0.11"
43+
env_logger = { version = "0.11", optional = true }
4444
log = { version = "~0.4", features = ["std"] }
4545
normpath = "1.3.0"
46-
liquid = "0.26.11"
46+
liquid = { version = "0.26.11", optional = true }
4747
once_cell = "1.21.0"
48-
rayon = "1.7.0"
48+
rayon = { version = "1.7.0", optional = true }
4949
regex = "1.10"
5050
itertools = "0.14.0"
5151
phf = { version = "0.11", features = ["macros"] }
@@ -57,3 +57,22 @@ version = "0.10"
5757
[dev-dependencies]
5858
similar = "2.5.0"
5959
tempfile = "3.18"
60+
61+
[[bin]]
62+
doc = false
63+
name = "svdtools"
64+
path = "src/main.rs"
65+
required-features = ["bin"]
66+
67+
[features]
68+
default = ["bin", "json", "yaml"]
69+
70+
bin = [
71+
"dep:liquid",
72+
"dep:rayon",
73+
"dep:clap",
74+
"dep:env_logger",
75+
]
76+
77+
json = ["dep:serde_json"]
78+
yaml = ["dep:serde_yaml"]

src/convert/convert_cli.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ pub use crate::ConfigFormat;
1111
#[non_exhaustive]
1212
pub enum InputFormat {
1313
Xml,
14+
#[cfg(feature = "yaml")]
1415
Yaml,
16+
#[cfg(feature = "json")]
1517
Json,
1618
}
1719

@@ -20,7 +22,9 @@ impl FromStr for InputFormat {
2022
fn from_str(s: &str) -> Result<Self, Self::Err> {
2123
match s {
2224
"svd" | "SVD" | "xml" | "XML" => Ok(Self::Xml),
25+
#[cfg(feature = "yaml")]
2326
"yml" | "yaml" | "YAML" => Ok(Self::Yaml),
27+
#[cfg(feature = "json")]
2428
"json" | "JSON" => Ok(Self::Json),
2529
_ => Err(anyhow!("Unknown input file format")),
2630
}
@@ -31,7 +35,9 @@ impl FromStr for InputFormat {
3135
#[non_exhaustive]
3236
pub enum OutputFormat {
3337
Xml,
38+
#[cfg(feature = "yaml")]
3439
Yaml,
40+
#[cfg(feature = "json")]
3541
Json,
3642
}
3743

@@ -40,7 +46,9 @@ impl FromStr for OutputFormat {
4046
fn from_str(s: &str) -> Result<Self, Self::Err> {
4147
match s {
4248
"svd" | "SVD" | "xml" | "XML" => Ok(Self::Xml),
49+
#[cfg(feature = "yaml")]
4350
"yml" | "yaml" | "YAML" => Ok(Self::Yaml),
51+
#[cfg(feature = "json")]
4452
"json" | "JSON" => Ok(Self::Json),
4553
_ => Err(anyhow!("Unknown output file format")),
4654
}
@@ -75,7 +83,9 @@ pub fn open_svd(
7583
&input,
7684
&svd_parser::Config::default().ignore_enums(parser_config.ignore_enums),
7785
)?,
86+
#[cfg(feature = "yaml")]
7887
InputFormat::Yaml => serde_yaml::from_str(&input)?,
88+
#[cfg(feature = "json")]
7989
InputFormat::Json => serde_json::from_str(&input)?,
8090
};
8191
if parser_config.expand_properties {
@@ -111,7 +121,9 @@ pub fn convert(
111121

112122
let output = match output_format {
113123
OutputFormat::Xml => svd_encoder::encode_with_config(&device, &config)?,
124+
#[cfg(feature = "yaml")]
114125
OutputFormat::Yaml => serde_yaml::to_string(&device)?,
126+
#[cfg(feature = "json")]
115127
OutputFormat::Json => serde_json::to_string_pretty(&device)?,
116128
};
117129

src/convert/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#[cfg(feature = "bin")]
12
pub mod convert_cli;

src/html/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
#[cfg(feature = "bin")]
12
pub mod html_cli;
3+
#[cfg(feature = "bin")]
24
pub mod htmlcompare_cli;

src/interrupts/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "bin")]
12
mod interrupt_list;
3+
#[cfg(feature = "bin")]
24
pub mod interrupts_cli;
5+
#[cfg(feature = "bin")]
36
mod svd_reader;

src/lib.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::anyhow;
2-
use std::io::Read;
3-
use std::{fs::File, path::Path, str::FromStr};
2+
use std::path::Path;
3+
#[cfg(any(feature = "json", feature = "yaml"))]
4+
use std::{fs::File, io::Read, str::FromStr};
45

56
pub mod common;
67
pub mod convert;
@@ -14,21 +15,27 @@ pub mod patch;
1415
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1516
#[non_exhaustive]
1617
pub enum ConfigFormat {
18+
#[cfg(feature = "yaml")]
1719
Yaml,
20+
#[cfg(feature = "json")]
1821
Json,
1922
}
2023

24+
#[cfg(any(feature = "json", feature = "yaml"))]
2125
impl FromStr for ConfigFormat {
2226
type Err = anyhow::Error;
2327
fn from_str(s: &str) -> Result<Self, Self::Err> {
2428
match s {
29+
#[cfg(feature = "yaml")]
2530
"yml" | "yaml" | "YAML" => Ok(Self::Yaml),
31+
#[cfg(feature = "json")]
2632
"json" | "JSON" => Ok(Self::Json),
2733
_ => Err(anyhow!("Unknown config file format")),
2834
}
2935
}
3036
}
3137

38+
#[cfg(any(feature = "json", feature = "yaml"))]
3239
pub(crate) fn get_encoder_config(
3340
format_config: Option<&Path>,
3441
) -> anyhow::Result<svd_encoder::Config> {
@@ -41,7 +48,9 @@ pub(crate) fn get_encoder_config(
4148
File::open(format_config)?.read_to_string(&mut config)?;
4249

4350
let config_map: std::collections::HashMap<String, String> = match config_format {
51+
#[cfg(feature = "yaml")]
4452
ConfigFormat::Yaml => serde_yaml::from_str(&config)?,
53+
#[cfg(feature = "json")]
4554
ConfigFormat::Json => serde_json::from_str(&config)?,
4655
};
4756

@@ -56,5 +65,18 @@ pub(crate) fn get_encoder_config(
5665
})
5766
}
5867

68+
#[cfg(not(any(feature = "json", feature = "yaml")))]
69+
70+
pub(crate) fn get_encoder_config(
71+
format_config: Option<&Path>,
72+
) -> anyhow::Result<svd_encoder::Config> {
73+
if let Some(format_config) = format_config {
74+
Err(anyhow!("In path: {}", format_config.display())
75+
.context("Config file passed to a build of svdtools that does not support it"))
76+
} else {
77+
Ok(svd_encoder::Config::default())
78+
}
79+
}
80+
5981
#[cfg(test)]
6082
mod test_utils;

src/makedeps/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#[cfg(feature = "bin")]
12
pub mod makedeps_cli;

src/mmap/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#[cfg(feature = "bin")]
12
pub mod mmap_cli;

src/patch/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "bin")]
12
pub mod patch_cli;
23

34
use once_cell::sync::Lazy;
@@ -48,8 +49,8 @@ pub struct Config {
4849
}
4950

5051
/// Derive level when several identical enumerationValues added in a field
51-
#[derive(clap::ValueEnum)]
52-
#[value(rename_all = "lower")]
52+
#[cfg_attr(feature = "bin", derive(clap::ValueEnum))]
53+
#[cfg_attr(feature = "bin", value(rename_all = "lower"))]
5354
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5455
pub enum EnumAutoDerive {
5556
#[default]

0 commit comments

Comments
 (0)