byteorm_lib/rustgen/
utils.rs

1use quote::{quote, format_ident};
2use proc_macro2::TokenStream;
3
4pub fn rust_type_from_schema(type_name: &str, nullable: bool) -> TokenStream {
5    let base_type = match type_name {
6        "BigInt"      => quote! { i64 },
7        "Int"         => quote! { i32 },
8        "String"      => quote! { String },
9        "JsonB"       => quote! { serde_json::Value },
10        "TimestamptZ" | "Timestamp" => quote! { DateTime<Utc> },
11        "Boolean"     => quote! { bool },
12        "Float"       => quote! { f64 },
13        "Serial"      => quote! { i32 },
14        "Real"        => quote! { f32 },
15        _             => quote! { String },
16    };
17
18    if nullable {
19        quote! { Option<#base_type> }
20    } else {
21        base_type
22    }
23}
24
25pub fn to_snake_case(s: &str) -> String {
26    let mut result = String::new();
27    for (i, ch) in s.chars().enumerate() {
28        if ch.is_uppercase() && i > 0 {
29            result.push('_');
30        }
31        result.push(ch.to_lowercase().next().unwrap_or(ch));
32    }
33    result
34}
35
36pub fn capitalize_first(s: &str) -> String {
37    let mut chars = s.chars();
38    match chars.next() {
39        None => String::new(),
40        Some(first) => first.to_uppercase().chain(chars).collect(),
41    }
42}