1 #[allow(unused_imports)]
2 #[allow(deprecated)]
3 use std::ascii::AsciiExt;
4
5 use proc_macro2::{Ident, Literal, Span, TokenStream};
6 use quote::ToTokens;
7
is_keyword(txt: &str) -> bool8 pub fn is_keyword(txt: &str) -> bool {
9 match txt {
10 "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | "crate"
11 | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if" | "impl" | "in"
12 | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" | "offsetof" | "override" | "priv"
13 | "proc" | "pub" | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct"
14 | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual"
15 | "where" | "while" | "yield" | "__handler" | "__object" => true,
16 _ => false,
17 }
18 }
19
is_camel_keyword(txt: &str) -> bool20 pub fn is_camel_keyword(txt: &str) -> bool {
21 match txt {
22 "Self" => true,
23 _ => false,
24 }
25 }
26
snake_to_camel(input: &str) -> String27 pub fn snake_to_camel(input: &str) -> String {
28 let result = input
29 .split('_')
30 .flat_map(|s| {
31 let mut first = true;
32 s.chars().map(move |c| {
33 if first {
34 first = false;
35 c.to_ascii_uppercase()
36 } else {
37 c
38 }
39 })
40 })
41 .collect::<String>();
42
43 if is_camel_keyword(&result) {
44 format!("_{}", &result)
45 } else {
46 result
47 }
48 }
49
dotted_to_relname(input: &str) -> TokenStream50 pub fn dotted_to_relname(input: &str) -> TokenStream {
51 let mut it = input.split('.');
52 match (it.next(), it.next()) {
53 (Some(module), Some(name)) => {
54 let module = Ident::new(module, Span::call_site());
55 let ident = Ident::new(&snake_to_camel(name), Span::call_site());
56 quote!(super::#module::#ident)
57 }
58 (Some(name), None) => Ident::new(&snake_to_camel(name), Span::call_site()).into_token_stream(),
59 _ => unreachable!(),
60 }
61 }
62
null_terminated_byte_string_literal(string: &str) -> Literal63 pub fn null_terminated_byte_string_literal(string: &str) -> Literal {
64 let mut val = Vec::with_capacity(string.len() + 1);
65 val.extend_from_slice(string.as_bytes());
66 val.push(0);
67
68 Literal::byte_string(&val)
69 }
70