use crate::types::columns::ColumnVal;
use crate::types::sql_operator::SqlOperator;
#[cfg(feature = "try-parse")]
use proc_macro_error2::emit_error;
use proc_macro2::Ident;
use syn::Token;
use syn::parse::{Parse, ParseStream};
#[cfg_attr(feature = "debug", derive(Debug))]
pub(crate) struct Condition {
pub(crate) column_name: Ident,
pub(crate) field_alias: Option<Ident>,
pub(crate) operator: SqlOperator,
pub(crate) column_type: ColumnVal,
pub(crate) optional: bool,
}
impl Condition {
pub(crate) fn rust_name(&self) -> &Ident {
if let Some(alias) = &self.field_alias {
alias
} else {
&self.column_name
}
}
}
impl Parse for Condition {
fn parse(input: ParseStream) -> syn::Result<Self> {
let optional = input.peek(Token![?]);
if optional {
input.parse::<Token![?]>()?;
}
let column_name = input.parse().unwrap_or_else(|err| {
let span = err.span();
#[cfg(not(feature = "try-parse"))]
proc_macro_error2::abort!(
span, "Failed to parse condition name";
note = "The name will also be used for the column name, if you want to change the name in rust use aliasing `<column_name> as <alias>`"
);
#[cfg(feature = "try-parse")]
emit_error!(
span, "Failed to parse condition name";
note = "The name will also be used for the column name, if you want to change the name in rust use aliasing `<column_name> as <alias>`"
);
#[cfg(feature = "try-parse")]
Ident::new("__err__", span)
});
let mut field_alias = None;
let lookahead = input.lookahead1();
if lookahead.peek(Token![as]) {
input.parse::<Token![as]>()?;
field_alias = input.parse().unwrap_or_else(|err| {
let span = err.span();
#[cfg(not(feature = "try-parse"))]
proc_macro_error2::abort!(
span,
"Failed to parse column alias, the alias must be a valid identifier"
);
#[cfg(feature = "try-parse")]
emit_error!(
span,
"Failed to parse column alias, the alias must be a valid identifier"
);
#[cfg(feature = "try-parse")]
None
});
}
let operator = input.parse()?;
let column_type = input.parse()?;
Ok(Self {
column_name,
field_alias,
operator,
column_type,
optional,
})
}
}