From 4cc4513fdc8115c74b7fe0e0249a3421d36a0e6d Mon Sep 17 00:00:00 2001 From: Cyril Plisko Date: Mon, 9 Sep 2024 11:52:40 +0300 Subject: [PATCH] mavlink-bindgen: parser: Simple PascalCase implementation --- mavlink-bindgen/src/parser.rs | 39 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/mavlink-bindgen/src/parser.rs b/mavlink-bindgen/src/parser.rs index fc6cff305a..05398e60ac 100644 --- a/mavlink-bindgen/src/parser.rs +++ b/mavlink-bindgen/src/parser.rs @@ -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(); } } @@ -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" => { @@ -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) -> 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(), + } +}