pub trait CompositeTypeDdl {
fn generate_create_ddl() -> String;
fn generate_drop_ddl() -> String;
fn pg_type_name() -> &'static str;
}
pub mod utils {
use syn::Type;
pub fn rust_type_to_sql_type(ty: &Type) -> String {
match ty {
Type::Path(type_path) => {
let path = &type_path.path;
if let Some(segment) = path.segments.last() {
match segment.ident.to_string().as_str() {
"i16" => "SMALLINT".to_string(),
"i32" => "INTEGER".to_string(),
"i64" => "BIGINT".to_string(),
"f32" => "REAL".to_string(),
"f64" => "DOUBLE PRECISION".to_string(),
"bool" => "BOOLEAN".to_string(),
"String" => "TEXT".to_string(),
"Uuid" => "UUID".to_string(),
"Decimal" => "NUMERIC".to_string(),
"Option" => {
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
if let Some(syn::GenericArgument::Type(inner_type)) =
args.args.first()
{
return rust_type_to_sql_type(inner_type);
}
}
"TEXT".to_string()
}
_ => "TEXT".to_string(), }
} else {
"TEXT".to_string()
}
}
_ => "TEXT".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_composite_type_ddl_trait_exists() {
fn assert_ddl_trait<T: CompositeTypeDdl>() {}
}
}