Skip to content

Commit

Permalink
Fixed DbEnum macro
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 4, 2023
1 parent a05223a commit 9263bd1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
28 changes: 19 additions & 9 deletions rorm-macro/src/generate/db_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn generate_db_enum(parsed: &ParsedDbEnum) -> TokenStream {
#(stringify!(#variants)),*
];

impl ::rorm::internal::field::FieldType for #ident {
impl ::rorm::fields::traits::FieldType for #ident {
type Kind = ::rorm::internal::field::kind::AsDbType;

type Columns<T> = [T; 1];
Expand Down Expand Up @@ -67,15 +67,25 @@ pub fn generate_db_enum(parsed: &ParsedDbEnum) -> TokenStream {
}
}
}
impl<'a> ::rorm::conditions::IntoSingleValue<'a, ::rorm::internal::hmr::db_type::Choices> for #ident {
type Condition = ::rorm::conditions::Value<'a>;
impl<'rhs> ::rorm::fields::traits::FieldEq<'rhs, #ident> for #ident {
type EqCond<A: ::rorm::FieldAccess> = ::rorm::conditions::Binary<::rorm::conditions::Column<A>, ::rorm::conditions::Value<'rhs>>;
fn field_equals<A: ::rorm::FieldAccess>(access: A, value: #ident) -> Self::EqCond<A> {
let [value] = <#ident as ::rorm::fields::traits::FieldType>::into_values(value);
::rorm::conditions::Binary {
operator: ::rorm::conditions::BinaryOperator::Equals,
fst_arg: ::rorm::conditions::Column(access),
snd_arg: value,
}
}

fn into_condition(self) -> Self::Condition {
::rorm::conditions::Value::Choice(::std::borrow::Cow::Borrowed(match self {
#(
Self::#variants => stringify!(#variants),
)*
}))
type NeCond<A: ::rorm::FieldAccess> = ::rorm::conditions::Binary<::rorm::conditions::Column<A>, ::rorm::conditions::Value<'rhs>>;
fn field_not_equals<A: ::rorm::FieldAccess>(access: A, value: #ident) -> Self::NeCond<A> {
let [value] = <#ident as ::rorm::fields::traits::FieldType>::into_values(value);
::rorm::conditions::Binary {
operator: ::rorm::conditions::BinaryOperator::NotEquals,
fst_arg: ::rorm::conditions::Column(access),
snd_arg: value,
}
}
}
};
Expand Down
33 changes: 24 additions & 9 deletions src/conditions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ impl<'a, A: FieldAccess> Condition<'a> for Column<A> {
/// A binary expression
#[derive(Copy, Clone)]
pub struct Binary<A, B> {
pub(crate) operator: BinaryOperator,
pub(crate) fst_arg: A,
pub(crate) snd_arg: B,
/// SQL operator to use
pub operator: BinaryOperator,

/// The expression's first argument
pub fst_arg: A,

/// The expression's second argument
pub snd_arg: B,
}
/// A binary operator
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -189,10 +194,17 @@ impl<'a, A: Condition<'a>, B: Condition<'a>> Condition<'a> for Binary<A, B> {
/// A ternary expression
#[derive(Copy, Clone)]
pub struct Ternary<A, B, C> {
pub(crate) operator: TernaryOperator,
pub(crate) fst_arg: A,
pub(crate) snd_arg: B,
pub(crate) trd_arg: C,
/// SQL operator to use
pub operator: TernaryOperator,

/// The expression's first argument
pub fst_arg: A,

/// The expression's second argument
pub snd_arg: B,

/// The expression's third argument
pub trd_arg: C,
}
/// A ternary operator
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -224,8 +236,11 @@ impl<'a, A: Condition<'a>, B: Condition<'a>, C: Condition<'a>> Condition<'a> for
/// A unary expression
#[derive(Copy, Clone)]
pub struct Unary<A> {
pub(crate) operator: UnaryOperator,
pub(crate) fst_arg: A,
/// SQL operator to use
pub operator: UnaryOperator,

/// The expression's first argument
pub fst_arg: A,
}
/// A unary operator
#[derive(Copy, Clone)]
Expand Down

0 comments on commit 9263bd1

Please sign in to comment.