Skip to content

Commit 93eb06b

Browse files
committed
mode Config to config module
1 parent 2fd1d1f commit 93eb06b

File tree

8 files changed

+166
-160
lines changed

8 files changed

+166
-160
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
1616
Add `width`, `offset` methods
1717
- *breaking change* Always numerates field arrays from 0
1818
- Support of default value for `EnumeratedValues`
19+
- move `Config` to `config` module
1920

2021
## [v0.30.3] - 2023-11-19
2122

src/config.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
use anyhow::{bail, Result};
2+
use std::path::{Path, PathBuf};
3+
4+
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
5+
#[derive(Clone, PartialEq, Eq, Debug)]
6+
pub struct Config {
7+
#[cfg_attr(feature = "serde", serde(default))]
8+
pub target: Target,
9+
#[cfg_attr(feature = "serde", serde(default))]
10+
pub atomics: bool,
11+
#[cfg_attr(feature = "serde", serde(default))]
12+
pub atomics_feature: Option<String>,
13+
#[cfg_attr(feature = "serde", serde(default))]
14+
pub generic_mod: bool,
15+
#[cfg_attr(feature = "serde", serde(default))]
16+
pub make_mod: bool,
17+
#[cfg_attr(feature = "serde", serde(default))]
18+
pub ignore_groups: bool,
19+
#[cfg_attr(feature = "serde", serde(default))]
20+
pub keep_list: bool,
21+
#[cfg_attr(feature = "serde", serde(default))]
22+
pub strict: bool,
23+
#[cfg_attr(feature = "serde", serde(default))]
24+
pub pascal_enum_values: bool,
25+
#[cfg_attr(feature = "serde", serde(default))]
26+
pub feature_group: bool,
27+
#[cfg_attr(feature = "serde", serde(default))]
28+
pub feature_peripheral: bool,
29+
#[cfg_attr(feature = "serde", serde(default))]
30+
pub max_cluster_size: bool,
31+
#[cfg_attr(feature = "serde", serde(default))]
32+
pub impl_debug: bool,
33+
#[cfg_attr(feature = "serde", serde(default))]
34+
pub impl_debug_feature: Option<String>,
35+
#[cfg_attr(feature = "serde", serde(default = "current_dir"))]
36+
pub output_dir: PathBuf,
37+
#[cfg_attr(feature = "serde", serde(default))]
38+
pub input: Option<PathBuf>,
39+
#[cfg_attr(feature = "serde", serde(default))]
40+
pub source_type: SourceType,
41+
#[cfg_attr(feature = "serde", serde(default))]
42+
pub log_level: Option<String>,
43+
#[cfg_attr(feature = "serde", serde(default))]
44+
pub interrupt_link_section: Option<String>,
45+
#[cfg_attr(feature = "serde", serde(default))]
46+
pub reexport_core_peripherals: bool,
47+
#[cfg_attr(feature = "serde", serde(default))]
48+
pub reexport_interrupt: bool,
49+
}
50+
51+
fn current_dir() -> PathBuf {
52+
PathBuf::from(".")
53+
}
54+
55+
impl Default for Config {
56+
fn default() -> Self {
57+
Self {
58+
target: Target::default(),
59+
atomics: false,
60+
atomics_feature: None,
61+
generic_mod: false,
62+
make_mod: false,
63+
ignore_groups: false,
64+
keep_list: false,
65+
strict: false,
66+
pascal_enum_values: false,
67+
feature_group: false,
68+
feature_peripheral: false,
69+
max_cluster_size: false,
70+
impl_debug: false,
71+
impl_debug_feature: None,
72+
output_dir: current_dir(),
73+
input: None,
74+
source_type: SourceType::default(),
75+
log_level: None,
76+
interrupt_link_section: None,
77+
reexport_core_peripherals: false,
78+
reexport_interrupt: false,
79+
}
80+
}
81+
}
82+
83+
#[allow(clippy::upper_case_acronyms)]
84+
#[allow(non_camel_case_types)]
85+
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
86+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
87+
pub enum Target {
88+
#[cfg_attr(feature = "serde", serde(rename = "cortex-m"))]
89+
#[default]
90+
CortexM,
91+
#[cfg_attr(feature = "serde", serde(rename = "msp430"))]
92+
Msp430,
93+
#[cfg_attr(feature = "serde", serde(rename = "riscv"))]
94+
RISCV,
95+
#[cfg_attr(feature = "serde", serde(rename = "xtensa-lx"))]
96+
XtensaLX,
97+
#[cfg_attr(feature = "serde", serde(rename = "mips"))]
98+
Mips,
99+
#[cfg_attr(feature = "serde", serde(rename = "none"))]
100+
None,
101+
}
102+
103+
impl Target {
104+
pub fn parse(s: &str) -> Result<Self> {
105+
Ok(match s {
106+
"cortex-m" => Target::CortexM,
107+
"msp430" => Target::Msp430,
108+
"riscv" => Target::RISCV,
109+
"xtensa-lx" => Target::XtensaLX,
110+
"mips" => Target::Mips,
111+
"none" => Target::None,
112+
_ => bail!("unknown target {}", s),
113+
})
114+
}
115+
}
116+
117+
#[cfg_attr(
118+
feature = "serde",
119+
derive(serde::Deserialize),
120+
serde(rename_all = "lowercase")
121+
)]
122+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
123+
pub enum SourceType {
124+
#[default]
125+
Xml,
126+
#[cfg(feature = "yaml")]
127+
Yaml,
128+
#[cfg(feature = "json")]
129+
Json,
130+
}
131+
132+
impl SourceType {
133+
/// Make a new [`SourceType`] from a given extension.
134+
pub fn from_extension(s: &str) -> Option<Self> {
135+
match s {
136+
"svd" | "xml" => Some(Self::Xml),
137+
#[cfg(feature = "yaml")]
138+
"yml" | "yaml" => Some(Self::Yaml),
139+
#[cfg(feature = "json")]
140+
"json" => Some(Self::Json),
141+
_ => None,
142+
}
143+
}
144+
pub fn from_path(path: &Path) -> Self {
145+
path.extension()
146+
.and_then(|e| e.to_str())
147+
.and_then(Self::from_extension)
148+
.unwrap_or_default()
149+
}
150+
}

src/generate/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::borrow::Cow;
77
use std::fs::File;
88
use std::io::Write;
99

10-
use crate::util::{self, Config, ToSanitizedCase};
11-
use crate::Target;
10+
use crate::config::{Config, Target};
11+
use crate::util::{self, ToSanitizedCase};
1212
use anyhow::{Context, Result};
1313

1414
use crate::generate::{interrupt, peripheral};

src/generate/peripheral.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use svd_parser::expand::{
66
derive_cluster, derive_peripheral, derive_register, BlockPath, Index, RegisterPath,
77
};
88

9+
use crate::config::Config;
910
use crate::svd::{
1011
self, Cluster, ClusterInfo, MaybeArray, Peripheral, Register, RegisterCluster, RegisterInfo,
1112
};
@@ -15,8 +16,8 @@ use quote::{quote, ToTokens};
1516
use syn::{punctuated::Punctuated, Token};
1617

1718
use crate::util::{
18-
self, name_to_ty, path_segment, type_path, unsuffixed, zst_type, Config, FullName,
19-
ToSanitizedCase, BITS_PER_BYTE,
19+
self, name_to_ty, path_segment, type_path, unsuffixed, zst_type, FullName, ToSanitizedCase,
20+
BITS_PER_BYTE,
2021
};
2122
use anyhow::{anyhow, bail, Context, Result};
2223

src/generate/register.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use svd_parser::expand::{
1313
derive_enumerated_values, derive_field, BlockPath, EnumPath, FieldPath, Index, RegisterPath,
1414
};
1515

16+
use crate::config::Config;
1617
use crate::util::{
17-
self, ident_to_path, path_segment, replace_suffix, type_path, unsuffixed, Config, FullName,
18+
self, ident_to_path, path_segment, replace_suffix, type_path, unsuffixed, FullName,
1819
ToSanitizedCase, U32Ext,
1920
};
2021
use anyhow::{anyhow, Result};

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,11 @@
538538
use quote::quote;
539539
use svd_parser::svd;
540540

541+
pub mod config;
541542
pub mod generate;
542543
pub mod util;
543544

544-
pub use crate::util::{Config, Target};
545+
pub use config::{Config, Target};
545546

546547
#[non_exhaustive]
547548
pub struct Generation {
@@ -599,9 +600,9 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
599600
})
600601
}
601602

602-
/// Load a [Device](svd::Device) from a string slice with given [config](crate::util::Config).
603-
pub fn load_from(input: &str, config: &crate::util::Config) -> Result<svd::Device> {
604-
use self::util::SourceType;
603+
/// Load a [Device](svd::Device) from a string slice with given [config](crate::config::Config).
604+
pub fn load_from(input: &str, config: &Config) -> Result<svd::Device> {
605+
use config::SourceType;
605606
use svd_parser::ValidateLevel;
606607

607608
let mut device = match config.source_type {

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use anyhow::{Context, Result};
1010
use clap::{Arg, ArgAction, Command};
1111

1212
use svd2rust::{
13+
config::{Config, SourceType, Target},
1314
generate, load_from,
14-
util::{self, build_rs, Config, SourceType, Target},
15+
util::{self, build_rs},
1516
};
1617

1718
fn parse_configs(app: Command) -> Result<Config> {

0 commit comments

Comments
 (0)