use lazy_static::lazy_static;
use regex::Regex;
#[rustfmt::skip]
lazy_static! {
static ref REGEX_LINE: Regex = Regex::new(r"`(\w+)` (\w+(?:\(\d+(?:,\d+)?\))?) (\w+)?(?: COMMENT '([^']+)')?").unwrap();
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Field {
pub name: String,
pub rust_type: String,
pub nullable: bool,
pub db_field_name: String,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Table {
pub name: String,
pub pk_rs_name: String, pub pk_rs_type: String, pub fields: Vec<Field>,
pub pk_db_name: String, }
impl Table {
pub fn get_struct_name(&self) -> String {
snake_to_camel(&self.name.replace("t_", "")) + "Record"
}
}
pub fn to_rust_type(pg_type: &str) -> &str {
match pg_type {
"smallint" => "i16",
"int2" => "i16",
"integer" => "i32",
"int" => "i32",
"int4" => "i32",
"bigint" => "i64",
"int8" => "i64",
pg_type if pg_type.starts_with("numeric") => "bigdecimal::BigDecimal",
pg_type if pg_type.starts_with("decimal") => "bigdecimal::BigDecimal",
"real" => "f32",
"float4" => "f32",
"double precision" => "f64",
"float8" => "f64",
"smallserial" => "i16",
"serial2" => "i16",
"serial" => "i32",
"serial4" => "i32",
"bigserial" => "i64",
"serial8" => "i64",
pg_type if pg_type.starts_with("char") => "String",
pg_type if pg_type.starts_with("varchar") => "String",
"text" => "String",
pg_type if pg_type.starts_with("timestamp") => "chrono::DateTime<chrono::Utc>",
"date" => "chrono::NaiveDate",
pg_type if pg_type.starts_with("time") => "chrono::NaiveTime",
"bool" => "bool",
"boolean" => "bool",
"uuid" => "uuid::Uuid",
_ => "String",
}
}
pub fn snake_to_camel(s: &str) -> String {
let mut result = String::new();
let mut capitalize_next = true;
for c in s.chars() {
if c == '_' {
capitalize_next = true;
} else if capitalize_next {
result.push(c.to_ascii_uppercase());
capitalize_next = false;
} else {
result.push(c);
}
}
result
}