summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2020-04-02 13:56:06 +0200
committerNeal H. Walfield <neal@pep.foundation>2020-04-02 16:42:50 +0200
commit5b48c5ee08aedc4126c7b8f566cc3181e865fa20 (patch)
tree70accfb032fefb6dd8ccadb43f772a1c97b42232
parent0e9f3b6d0010d981f116559f84ebdeb5ec653957 (diff)
ffi-macros: Handle some special cases in ident2c.
- Interpret sequences of capital letters as a single word. For instance, `UserID` should map to `user_id`. - Special case `ID` by converting it to `Id` so that `UserIDAmalgamation` maps to `user_id_amalgamation`.
-rw-r--r--ffi-macros/src/lib.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/ffi-macros/src/lib.rs b/ffi-macros/src/lib.rs
index 8b315b72..756c1bc6 100644
--- a/ffi-macros/src/lib.rs
+++ b/ffi-macros/src/lib.rs
@@ -302,9 +302,24 @@ pub fn ffi_wrapper_type(args: TokenStream, input: TokenStream) -> TokenStream {
/// Derives the C type from the Rust type.
fn ident2c(ident: &syn::Ident) -> String {
let mut s = String::new();
- for (i, c) in ident.to_string().chars().enumerate() {
+
+ let mut ident = ident.to_string().chars().collect::<Vec<_>>();
+
+ // s/ID/Id/.
+ for i in 0..ident.len() - 1 {
+ if ident[i] == 'I' && ident[i + 1] == 'D' {
+ ident[i + 1] = 'd';
+ }
+ }
+
+ let mut previous_is_uppercase = Vec::new();
+ previous_is_uppercase.push(true);
+ previous_is_uppercase.extend(ident.iter().map(|c| c.is_uppercase()));
+
+ for (c, previous_is_upper) in ident.iter().zip(previous_is_uppercase)
+ {
if c.is_uppercase() {
- if i > 0 {
+ if ! previous_is_upper {
s += "_";
}
s += &c.to_lowercase().to_string();
@@ -320,6 +335,9 @@ fn ident2c_tests() {
let span = proc_macro2::Span::call_site();
assert_eq!(&ident2c(&syn::Ident::new("Fingerprint", span)), "fingerprint");
assert_eq!(&ident2c(&syn::Ident::new("PacketPile", span)), "packet_pile");
+ assert_eq!(&ident2c(&syn::Ident::new("UserID", span)), "user_id");
+ assert_eq!(&ident2c(&syn::Ident::new("UserIDAmalgamation", span)),
+ "user_id_amalgamation");
}
/// Describes our custom derive functions.