Skip to content

Commit

Permalink
IdentFormat::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 17, 2024
1 parent 3c7ab5f commit 20393c8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 89 deletions.
151 changes: 81 additions & 70 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ pub enum Case {
Snake,
}

impl Case {
pub fn parse(c: &str) -> Result<Option<Self>, IdentFormatError> {
Ok(match c {
"" | "unchanged" | "svd" => None,
"p" | "pascal" | "type" => Some(Case::Pascal),
"s" | "snake" | "lower" => Some(Case::Snake),
"c" | "constant" | "upper" => Some(Case::Constant),
_ => {
return Err(IdentFormatError::UnknownCase(c.into()));
}
})
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IdentFormatError {
UnknownCase(String),
Other,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
pub struct IdentFormat {
Expand Down Expand Up @@ -171,6 +191,36 @@ impl IdentFormat {
self.suffix = suffix.into();
self
}
pub fn parse(s: &str) -> Result<Self, IdentFormatError> {
let mut f = s.split(":");
match (f.next(), f.next(), f.next()) {
(Some(p), Some(c), Some(s)) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: p.into(),
suffix: s.into(),
})
}
(Some(p), Some(c), None) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: p.into(),
suffix: "".into(),
})
}
(Some(c), None, None) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: "".into(),
suffix: "".into(),
})
}
_ => Err(IdentFormatError::Other),
}
}
}

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

impl IdentFormats {
fn common() -> Self {
let snake = IdentFormat::default().snake_case();
Self(HashMap::from([
("field_accessor".into(), IdentFormat::default().snake_case()),
(
"register_accessor".into(),
IdentFormat::default().snake_case(),
),
(
"enum_value_accessor".into(),
IdentFormat::default().snake_case(),
),
("cluster".into(), IdentFormat::default().pascal_case()),
(
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
),
("register_mod".into(), IdentFormat::default().snake_case()),
("cluster_mod".into(), IdentFormat::default().snake_case()),
("peripheral_mod".into(), IdentFormat::default().snake_case()),
(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
),
("field_accessor".into(), snake.clone()),
("register_accessor".into(), snake.clone()),
("enum_value_accessor".into(), snake.clone()),
("cluster_accessor".into(), snake.clone()),
("register_mod".into(), snake.clone()),
("cluster_mod".into(), snake.clone()),
("peripheral_mod".into(), snake.clone()),
("peripheral_feature".into(), snake),
]))
}

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

let pascal = IdentFormat::default().pascal_case();
map.extend([
(
"field_reader".into(),
IdentFormat::default().pascal_case().suffix("R"),
),
(
"field_writer".into(),
IdentFormat::default().pascal_case().suffix("W"),
),
("enum_name".into(), IdentFormat::default().pascal_case()),
(
"enum_write_name".into(),
IdentFormat::default().pascal_case().suffix("WO"),
),
("enum_value".into(), IdentFormat::default().pascal_case()),
("field_reader".into(), pascal.clone().suffix("R")),
("field_writer".into(), pascal.clone().suffix("W")),
("enum_name".into(), pascal.clone()),
("enum_write_name".into(), pascal.clone().suffix("WO")),
("enum_value".into(), pascal.clone()),
("interrupt".into(), IdentFormat::default()),
("register".into(), IdentFormat::default().pascal_case()),
(
"register_spec".into(),
IdentFormat::default().pascal_case().suffix("Spec"),
),
("peripheral".into(), IdentFormat::default().pascal_case()),
("register".into(), pascal.clone()),
("cluster".into(), pascal.clone()),
("register_spec".into(), pascal.clone().suffix("Spec")),
("peripheral".into(), pascal),
(
"peripheral_singleton".into(),
IdentFormat::default().snake_case(),
Expand All @@ -240,36 +268,19 @@ impl IdentFormats {
pub fn legacy_theme() -> Self {
let mut map = Self::common();

let constant = IdentFormat::default().constant_case();
map.extend([
(
"field_reader".into(),
IdentFormat::default().constant_case().suffix("_R"),
),
(
"field_writer".into(),
IdentFormat::default().constant_case().suffix("_W"),
),
(
"enum_name".into(),
IdentFormat::default().constant_case().suffix("_A"),
),
(
"enum_write_name".into(),
IdentFormat::default().constant_case().suffix("_AW"),
),
("enum_value".into(), IdentFormat::default().constant_case()),
("interrupt".into(), IdentFormat::default().constant_case()),
("cluster".into(), IdentFormat::default().constant_case()),
("register".into(), IdentFormat::default().constant_case()),
(
"register_spec".into(),
IdentFormat::default().constant_case().suffix("_SPEC"),
),
("peripheral".into(), IdentFormat::default().constant_case()),
(
"peripheral_singleton".into(),
IdentFormat::default().constant_case(),
),
("field_reader".into(), constant.clone().suffix("_R")),
("field_writer".into(), constant.clone().suffix("_W")),
("enum_name".into(), constant.clone().suffix("_A")),
("enum_write_name".into(), constant.clone().suffix("_AW")),
("enum_value".into(), constant.clone()),
("interrupt".into(), constant.clone()),
("cluster".into(), constant.clone()),
("register".into(), constant.clone()),
("register_spec".into(), constant.clone().suffix("_SPEC")),
("peripheral".into(), constant.clone()),
("peripheral_singleton".into(), constant),
]);

map
Expand Down
35 changes: 16 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![recursion_limit = "128"]

use log::{debug, error, info, warn};
use svd2rust::config::{IdentFormats, IdentFormatsTheme};
use svd2rust::config::{IdentFormatError, IdentFormats, IdentFormatsTheme};
use svd2rust::util::{Case, IdentFormat};

use std::io::Write;
Expand Down Expand Up @@ -41,30 +41,27 @@ fn parse_configs(app: Command) -> Result<Config> {
config.ident_formats = idf;

if let Some(ident_formats) = ident_formats.get_many::<String>("ident_format") {
for f in ident_formats {
let mut f = f.split(":");
if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) {
let case = match c {
"" | "unchanged" | "svd" => None,
"p" | "pascal" | "type" => Some(Case::Pascal),
"s" | "snake" | "lower" => Some(Case::Snake),
"c" | "constant" | "upper" => Some(Case::Constant),
_ => {
warn!("Ident case `{c}` is unknown");
continue;
}
};
for fs in ident_formats {
if let Some((n, fmt)) = fs.split_once(':') {
if let std::collections::hash_map::Entry::Occupied(mut e) =
config.ident_formats.entry(n.into())
{
e.insert(IdentFormat {
case,
prefix: p.into(),
suffix: s.into(),
});
match IdentFormat::parse(fmt) {
Ok(ident_format) => {
e.insert(ident_format);
}
Err(IdentFormatError::UnknownCase(c)) => {
warn!("Ident case `{c}` is unknown")
}
Err(IdentFormatError::Other) => {
warn!("Can't parse identifier format string `{fmt}`")
}
}
} else {
warn!("Ident format name `{n}` is unknown");
}
} else {
warn!("Can't parse identifier format string `{fs}`");
}
}
}
Expand Down

0 comments on commit 20393c8

Please sign in to comment.