use super::fields;
use crate::database::introspection::Schema;
use crate::generators::rust::casing;
use crate::generators::rust::model::Model;
use std::fmt::Write as _;
const DERIVES: &str =
"#[derive(Debug, Clone, Serialize, Deserialize, FromRow, ToSchema, TS)]\n#[ts(export)]";
pub(super) fn submodules(model: &Model) -> String {
let mut file = if model.view {
"mod read;\n".to_string()
} else {
"mod create;\nmod delete;\nmod read;\nmod update;\n".to_string()
};
if model.actix {
file.push_str("pub mod routes;\n");
}
file.push('\n');
file
}
pub(super) fn enum_imports(model: &Model, schema: &Schema) -> String {
let names: Vec<String> = model
.enums(schema)
.iter()
.map(|item| casing::to_pascal(&item.name))
.collect();
match names.len() {
0 => String::new(),
1 => format!("use super::enums::{};\n", names[0]),
_ => format!("use super::enums::{{{}}};\n", names.join(", ")),
}
}
pub(super) fn declaration(model: &Model) -> String {
format!(
"{DERIVES}\npub struct {} {{\n{}}}\n",
model.name,
fields::render(model)
)
}
pub(super) fn all_options(model: &Model) -> String {
let mut fields = String::new();
for field in &model.fields {
let _ = writeln!(
fields,
"\tpub {}: Option<{}>,",
field.ident,
field.key_type()
);
}
format!(
"/// Optional equality filters for [`{name}::all_options`].\n\
#[derive(Debug, Clone, Default)]\n\
pub struct {options} {{\n{fields}}}\n",
name = model.name,
options = model.all_options_name(),
)
}