byteorm 0.1.6

A lightweight ORM for Rust
Documentation
use quote::{quote, format_ident};
use proc_macro2::TokenStream;

pub fn rust_type_from_schema(type_name: &str, nullable: bool) -> TokenStream {
    let base_type = match type_name {
        "BigInt"      => quote! { i64 },
        "Int"         => quote! { i32 },
        "String"      => quote! { String },
        "JsonB"       => quote! { serde_json::Value },
        "TimestamptZ" | "Timestamp" => quote! { DateTime<Utc> },
        "Boolean"     => quote! { bool },
        "Float"       => quote! { f64 },
        "Serial"      => quote! { i32 },
        "Real"        => quote! { f32 },
        _             => quote! { String },
    };

    if nullable {
        quote! { Option<#base_type> }
    } else {
        base_type
    }
}

pub fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(ch.to_lowercase().next().unwrap_or(ch));
    }
    result
}

pub fn capitalize_first(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first) => first.to_uppercase().chain(chars).collect(),
    }
}