Skip to content

Commit

Permalink
mavlink-bindgen: parser: Simple PascalCase implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
imp authored and patrickelectric committed Sep 9, 2024
1 parent 3cc4cce commit 4cc4513
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions mavlink-bindgen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,16 +1134,7 @@ pub fn parse_profile(
match stack.last() {
Some(&MavXmlElement::Enum) => {
if attr.key.into_inner() == b"name" {
mavenum.name = attr
.value
.clone()
.split(|b| *b == b'_')
.map(|x| x.to_ascii_lowercase())
.map(|mut v| {
v[0] = v[0].to_ascii_uppercase();
String::from_utf8(v).unwrap()
})
.collect();
mavenum.name = to_pascal_case(attr.value);
//mavenum.name = attr.value.clone();
}
}
Expand Down Expand Up @@ -1204,17 +1195,7 @@ pub fn parse_profile(
field.mavtype = MavType::parse_type(&r#type).unwrap();
}
b"enum" => {
field.enumtype = Some(
attr.value
.clone()
.split(|b| *b == b'_')
.map(|x| x.to_ascii_lowercase())
.map(|mut v| {
v[0] = v[0].to_ascii_uppercase();
String::from_utf8(v).unwrap()
})
.collect(),
);
field.enumtype = Some(to_pascal_case(attr.value));
//field.enumtype = Some(attr.value.clone());
}
b"display" => {
Expand Down Expand Up @@ -1484,3 +1465,19 @@ impl MavXmlFilter {
}
}
}

fn to_pascal_case(text: impl AsRef<[u8]>) -> String {
text.as_ref()
.split(|c| *c == b'_')
.map(String::from_utf8_lossy)
.map(capitalize_word)
.collect()
}

fn capitalize_word(text: impl AsRef<str>) -> String {
let mut chars = text.as_ref().chars();
match chars.next() {
None => String::new(),
Some(char) => char.to_uppercase().to_string() + &chars.as_str().to_ascii_lowercase(),
}
}

0 comments on commit 4cc4513

Please sign in to comment.