use syn::Attribute;
#[derive(Default)]
pub struct TableAttr {
pub table_name: Option<String>,
pub schema: Option<String>,
}
#[derive(Default, Clone)]
pub struct ColumnAttr {
pub column_name: Option<String>,
}
#[derive(Default, Clone)]
pub struct PkAttr {
pub is_identity: bool,
}
#[derive(Default, Clone)]
pub struct IgnoreAttr {
pub ignore: bool,
}
#[derive(Default, Clone)]
pub struct ComputedAttr {
pub is_computed: bool,
}
#[derive(Default, Clone)]
pub struct ServerDefaultAttr {
pub is_server_default: bool,
}
impl TableAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let mut result = TableAttr::default();
for attr in attrs {
if !attr.path().is_ident("sql_table") {
continue;
}
let _ = attr.parse_args_with(|input: syn::parse::ParseStream| {
if input.peek(syn::LitStr) {
let s: syn::LitStr = input.parse()?;
result.table_name = Some(s.value());
let _ = input.parse::<syn::Token![,]>();
}
while !input.is_empty() {
let ident: syn::Ident = input.parse()?;
let _: syn::Token![=] = input.parse()?;
let s: syn::LitStr = input.parse()?;
if ident == "schema" {
result.schema = Some(s.value());
}
let _ = input.parse::<syn::Token![,]>();
}
Ok(())
});
}
result
}
}
impl ColumnAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let mut result = ColumnAttr::default();
for attr in attrs {
if !attr.path().is_ident("sql_column") {
continue;
}
if let Ok(args) = attr.parse_args_with(
syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated,
) {
for arg in &args {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}) = arg
{
result.column_name = Some(s.value());
break;
}
}
}
}
result
}
}
impl PkAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let mut result = PkAttr::default();
for attr in attrs {
if !attr.path().is_ident("sql_primary_key") {
continue;
}
let _ = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("identity") {
result.is_identity = true;
}
Ok(())
});
}
result
}
}
impl IgnoreAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let ignore = attrs.iter().any(|a| a.path().is_ident("sql_ignore"));
Self { ignore }
}
}
impl ComputedAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let is_computed = attrs.iter().any(|a| a.path().is_ident("sql_computed"));
Self { is_computed }
}
}
impl ServerDefaultAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Self {
let is_server_default = attrs.iter().any(|a| a.path().is_ident("sql_default"));
Self { is_server_default }
}
}