Skip to content

Commit

Permalink
Remove butane_type for FieldType derived enums (#99)
Browse files Browse the repository at this point in the history
Deriving `FieldType` with JSON already added a custom type mapping, but a parallel was missing when deriving `FieldType` for an `enum`.
This change adds the latter to bring consistency and avoid the `butane_type` macro for enums.
  • Loading branch information
jayvdb authored May 29, 2023
1 parent 35ef9d0 commit 2dc6ddd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 1 addition & 2 deletions butane/tests/custom_enum_derived.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Tests deriving FieldType for an enum
use butane::db::Connection;
use butane::prelude::*;
use butane::{butane_type, model, query};
use butane::{model, query};
use butane::{FieldType, FromSql, ObjectState, SqlVal, ToSql};

use butane_test_helper::*;

#[butane_type(Text)]
#[derive(PartialEq, Eq, Debug, Clone, FieldType)]
enum Whatsit {
Foo,
Expand Down
15 changes: 11 additions & 4 deletions butane_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

extern crate proc_macro;

use butane_core::{codegen, make_compile_error, migrations};
use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier};
use butane_core::{codegen, make_compile_error,SqlType, migrations};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::TokenTree;
Expand Down Expand Up @@ -189,6 +190,15 @@ fn derive_field_type_for_enum(ident: &Ident, data_enum: syn::DataEnum) -> TokenS
return derive_field_type_with_json(ident);
}

let mut migrations = migrations_for_dir();

codegen::add_custom_type(
&mut migrations,
ident.to_string(),
DeferredSqlType::KnownId(TypeIdentifier::Ty(SqlType::Text)),
)
.unwrap();

let match_arms_to_string: Vec<TokenStream2> = data_enum
.variants
.iter()
Expand Down Expand Up @@ -254,9 +264,6 @@ fn derive_field_type_for_enum(ident: &Ident, data_enum: syn::DataEnum) -> TokenS

#[cfg(feature = "json")]
fn derive_field_type_with_json(struct_name: &Ident) -> TokenStream {
use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier};
use butane_core::SqlType;

let mut migrations = migrations_for_dir();

codegen::add_custom_type(
Expand Down

0 comments on commit 2dc6ddd

Please sign in to comment.