Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake case conversion enhancement #12

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rasn-compiler-tests/tests/parameterization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ e2e_pdu! {
#[non_exhaustive]
pub struct A2XPC5FlowBitRates {
#[rasn(identifier = "a2X-GuaranteedFlowBitRate")]
pub a2X__guaranteed_flow_bit_rate: bool,
pub a2_x__guaranteed_flow_bit_rate: bool,
#[rasn(size("1.."), identifier = "iE-Extensions")]
pub i_e__extensions: Option<SequenceOf<A2XPC5FlowBitRatesIEExtensions>>,
}
impl A2XPC5FlowBitRates {
pub fn new(
a2X__guaranteed_flow_bit_rate: bool,
a2_x__guaranteed_flow_bit_rate: bool,
i_e__extensions: Option<SequenceOf<A2XPC5FlowBitRatesIEExtensions>>,
) -> Self {
Self {
a2X__guaranteed_flow_bit_rate,
a2_x__guaranteed_flow_bit_rate,
i_e__extensions,
}
}
Expand Down
43 changes: 28 additions & 15 deletions rasn-compiler/src/intermediate/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@ const RUST_KEYWORDS: [&str; 38] = [
];

pub fn to_rust_snake_case(input: &str) -> Ident {
let mut input = input.replace('-', "_");
let input = input.drain(..).fold(String::new(), |mut acc, c| {
if acc.is_empty() && c.is_uppercase() {
acc.push(c.to_ascii_lowercase());
} else if acc.ends_with(|last: char| last.is_lowercase() || last == '_') && c.is_uppercase()
{
acc.push('_');
acc.push(c.to_ascii_lowercase());
let input = input.replace('-', "_");
let mut lowercase = String::with_capacity(input.len());

let peekable = &mut input.chars().peekable();
while let Some(c) = peekable.next() {
if c.is_lowercase() || c == '_' || c.is_numeric() {
lowercase.push(c);
if peekable.peek().map_or(false, |next| next.is_uppercase()) {
lowercase.push('_');
}
} else {
acc.push(c);
lowercase.push(c.to_ascii_lowercase());
}
acc
});
let name = if RUST_KEYWORDS.contains(&input.as_str()) {
String::from("r_") + &input
}
let name = if RUST_KEYWORDS.contains(&lowercase.as_str()) {
String::from("r_") + &lowercase
} else {
input
lowercase
};
Ident::new(&name, Span::call_site())
}
Expand Down Expand Up @@ -112,7 +113,7 @@ use quote::format_ident;

#[cfg(test)]
mod tests {
use super::int_type_token;
use super::{int_type_token, to_rust_snake_case};

#[test]
fn determines_int_type() {
Expand All @@ -126,4 +127,16 @@ mod tests {
assert_eq!(int_type_token(-67463, 23123, false), "i32");
assert_eq!(int_type_token(255, 257, false), "u16");
}

#[test]
fn converts_to_snake_case() {
assert_eq!(to_rust_snake_case("HelloWorld"), "hello_world");
assert_eq!(to_rust_snake_case("helloWorld"), "hello_world");
assert_eq!(to_rust_snake_case("hello-world"), "hello_world");
assert_eq!(to_rust_snake_case("HELLOWORLD"), "helloworld");
assert_eq!(to_rust_snake_case("HelloWORLD"), "hello_world");
assert_eq!(to_rust_snake_case("HELLO-WORLD"), "hello__world");
assert_eq!(to_rust_snake_case("struct"), "r_struct");
assert_eq!(to_rust_snake_case("STRUCT"), "r_struct");
}
}
Loading