Skip to content

Commit 689b89f

Browse files
committed
IdentFormat::parse
1 parent 3c7ab5f commit 689b89f

File tree

2 files changed

+98
-90
lines changed

2 files changed

+98
-90
lines changed

src/config.rs

+81-70
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ pub enum Case {
137137
Snake,
138138
}
139139

140+
impl Case {
141+
pub fn parse(c: &str) -> Result<Option<Self>, IdentFormatError> {
142+
Ok(match c {
143+
"" | "unchanged" | "svd" => None,
144+
"p" | "pascal" | "type" => Some(Case::Pascal),
145+
"s" | "snake" | "lower" => Some(Case::Snake),
146+
"c" | "constant" | "upper" => Some(Case::Constant),
147+
_ => {
148+
return Err(IdentFormatError::UnknownCase(c.into()));
149+
}
150+
})
151+
}
152+
}
153+
154+
#[derive(Clone, Debug, PartialEq, Eq)]
155+
pub enum IdentFormatError {
156+
UnknownCase(String),
157+
Other,
158+
}
159+
140160
#[derive(Clone, Debug, Default, PartialEq, Eq)]
141161
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
142162
pub struct IdentFormat {
@@ -171,6 +191,36 @@ impl IdentFormat {
171191
self.suffix = suffix.into();
172192
self
173193
}
194+
pub fn parse(s: &str) -> Result<Self, IdentFormatError> {
195+
let mut f = s.split(":");
196+
match (f.next(), f.next(), f.next()) {
197+
(Some(p), Some(c), Some(s)) => {
198+
let case = Case::parse(c)?;
199+
Ok(Self {
200+
case,
201+
prefix: p.into(),
202+
suffix: s.into(),
203+
})
204+
}
205+
(Some(p), Some(c), None) => {
206+
let case = Case::parse(c)?;
207+
Ok(Self {
208+
case,
209+
prefix: p.into(),
210+
suffix: "".into(),
211+
})
212+
}
213+
(Some(c), None, None) => {
214+
let case = Case::parse(c)?;
215+
Ok(Self {
216+
case,
217+
prefix: "".into(),
218+
suffix: "".into(),
219+
})
220+
}
221+
_ => Err(IdentFormatError::Other),
222+
}
223+
}
174224
}
175225

176226
#[derive(Clone, Debug, Default, PartialEq, Eq)]
@@ -179,56 +229,34 @@ pub struct IdentFormats(HashMap<String, IdentFormat>);
179229

180230
impl IdentFormats {
181231
fn common() -> Self {
232+
let snake = IdentFormat::default().snake_case();
182233
Self(HashMap::from([
183-
("field_accessor".into(), IdentFormat::default().snake_case()),
184-
(
185-
"register_accessor".into(),
186-
IdentFormat::default().snake_case(),
187-
),
188-
(
189-
"enum_value_accessor".into(),
190-
IdentFormat::default().snake_case(),
191-
),
192-
("cluster".into(), IdentFormat::default().pascal_case()),
193-
(
194-
"cluster_accessor".into(),
195-
IdentFormat::default().snake_case(),
196-
),
197-
("register_mod".into(), IdentFormat::default().snake_case()),
198-
("cluster_mod".into(), IdentFormat::default().snake_case()),
199-
("peripheral_mod".into(), IdentFormat::default().snake_case()),
200-
(
201-
"peripheral_feature".into(),
202-
IdentFormat::default().snake_case(),
203-
),
234+
("field_accessor".into(), snake.clone()),
235+
("register_accessor".into(), snake.clone()),
236+
("enum_value_accessor".into(), snake.clone()),
237+
("cluster_accessor".into(), snake.clone()),
238+
("register_mod".into(), snake.clone()),
239+
("cluster_mod".into(), snake.clone()),
240+
("peripheral_mod".into(), snake.clone()),
241+
("peripheral_feature".into(), snake),
204242
]))
205243
}
206244

207245
pub fn new_theme() -> Self {
208246
let mut map = Self::common();
209247

248+
let pascal = IdentFormat::default().pascal_case();
210249
map.extend([
211-
(
212-
"field_reader".into(),
213-
IdentFormat::default().pascal_case().suffix("R"),
214-
),
215-
(
216-
"field_writer".into(),
217-
IdentFormat::default().pascal_case().suffix("W"),
218-
),
219-
("enum_name".into(), IdentFormat::default().pascal_case()),
220-
(
221-
"enum_write_name".into(),
222-
IdentFormat::default().pascal_case().suffix("WO"),
223-
),
224-
("enum_value".into(), IdentFormat::default().pascal_case()),
250+
("field_reader".into(), pascal.clone().suffix("R")),
251+
("field_writer".into(), pascal.clone().suffix("W")),
252+
("enum_name".into(), pascal.clone()),
253+
("enum_write_name".into(), pascal.clone().suffix("WO")),
254+
("enum_value".into(), pascal.clone()),
225255
("interrupt".into(), IdentFormat::default()),
226-
("register".into(), IdentFormat::default().pascal_case()),
227-
(
228-
"register_spec".into(),
229-
IdentFormat::default().pascal_case().suffix("Spec"),
230-
),
231-
("peripheral".into(), IdentFormat::default().pascal_case()),
256+
("register".into(), pascal.clone()),
257+
("cluster".into(), pascal.clone()),
258+
("register_spec".into(), pascal.clone().suffix("Spec")),
259+
("peripheral".into(), pascal),
232260
(
233261
"peripheral_singleton".into(),
234262
IdentFormat::default().snake_case(),
@@ -240,36 +268,19 @@ impl IdentFormats {
240268
pub fn legacy_theme() -> Self {
241269
let mut map = Self::common();
242270

271+
let constant = IdentFormat::default().constant_case();
243272
map.extend([
244-
(
245-
"field_reader".into(),
246-
IdentFormat::default().constant_case().suffix("_R"),
247-
),
248-
(
249-
"field_writer".into(),
250-
IdentFormat::default().constant_case().suffix("_W"),
251-
),
252-
(
253-
"enum_name".into(),
254-
IdentFormat::default().constant_case().suffix("_A"),
255-
),
256-
(
257-
"enum_write_name".into(),
258-
IdentFormat::default().constant_case().suffix("_AW"),
259-
),
260-
("enum_value".into(), IdentFormat::default().constant_case()),
261-
("interrupt".into(), IdentFormat::default().constant_case()),
262-
("cluster".into(), IdentFormat::default().constant_case()),
263-
("register".into(), IdentFormat::default().constant_case()),
264-
(
265-
"register_spec".into(),
266-
IdentFormat::default().constant_case().suffix("_SPEC"),
267-
),
268-
("peripheral".into(), IdentFormat::default().constant_case()),
269-
(
270-
"peripheral_singleton".into(),
271-
IdentFormat::default().constant_case(),
272-
),
273+
("field_reader".into(), constant.clone().suffix("_R")),
274+
("field_writer".into(), constant.clone().suffix("_W")),
275+
("enum_name".into(), constant.clone().suffix("_A")),
276+
("enum_write_name".into(), constant.clone().suffix("_AW")),
277+
("enum_value".into(), constant.clone()),
278+
("interrupt".into(), constant.clone()),
279+
("cluster".into(), constant.clone()),
280+
("register".into(), constant.clone()),
281+
("register_spec".into(), constant.clone().suffix("_SPEC")),
282+
("peripheral".into(), constant.clone()),
283+
("peripheral_singleton".into(), constant),
273284
]);
274285

275286
map

src/main.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![recursion_limit = "128"]
22

33
use log::{debug, error, info, warn};
4-
use svd2rust::config::{IdentFormats, IdentFormatsTheme};
5-
use svd2rust::util::{Case, IdentFormat};
4+
use svd2rust::config::{IdentFormatError, IdentFormats, IdentFormatsTheme};
5+
use svd2rust::util::IdentFormat;
66

77
use std::io::Write;
88
use std::process;
@@ -41,30 +41,27 @@ fn parse_configs(app: Command) -> Result<Config> {
4141
config.ident_formats = idf;
4242

4343
if let Some(ident_formats) = ident_formats.get_many::<String>("ident_format") {
44-
for f in ident_formats {
45-
let mut f = f.split(":");
46-
if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) {
47-
let case = match c {
48-
"" | "unchanged" | "svd" => None,
49-
"p" | "pascal" | "type" => Some(Case::Pascal),
50-
"s" | "snake" | "lower" => Some(Case::Snake),
51-
"c" | "constant" | "upper" => Some(Case::Constant),
52-
_ => {
53-
warn!("Ident case `{c}` is unknown");
54-
continue;
55-
}
56-
};
44+
for fs in ident_formats {
45+
if let Some((n, fmt)) = fs.split_once(':') {
5746
if let std::collections::hash_map::Entry::Occupied(mut e) =
5847
config.ident_formats.entry(n.into())
5948
{
60-
e.insert(IdentFormat {
61-
case,
62-
prefix: p.into(),
63-
suffix: s.into(),
64-
});
49+
match IdentFormat::parse(fmt) {
50+
Ok(ident_format) => {
51+
e.insert(ident_format);
52+
}
53+
Err(IdentFormatError::UnknownCase(c)) => {
54+
warn!("Ident case `{c}` is unknown")
55+
}
56+
Err(IdentFormatError::Other) => {
57+
warn!("Can't parse identifier format string `{fmt}`")
58+
}
59+
}
6560
} else {
6661
warn!("Ident format name `{n}` is unknown");
6762
}
63+
} else {
64+
warn!("Can't parse identifier format string `{fs}`");
6865
}
6966
}
7067
}

0 commit comments

Comments
 (0)