1
1
//! Contains Refinery macros that are used to import and embed migration files.
2
2
#![ recursion_limit = "128" ]
3
3
4
+ #[ cfg( feature = "enums" ) ]
4
5
use heck:: ToUpperCamelCase ;
5
6
use proc_macro:: TokenStream ;
6
7
use proc_macro2:: { Span as Span2 , TokenStream as TokenStream2 } ;
@@ -32,38 +33,53 @@ fn migration_fn_quoted<T: ToTokens>(_migrations: Vec<T>) -> TokenStream2 {
32
33
result
33
34
}
34
35
36
+ #[ cfg( feature = "enums" ) ]
35
37
fn migration_enum_quoted ( migration_names : & [ impl AsRef < str > ] ) -> TokenStream2 {
36
- if cfg ! ( feature = "enums" ) {
37
- let mut variants = Vec :: new ( ) ;
38
- let mut discriminants = Vec :: new ( ) ;
39
-
40
- for m in migration_names {
41
- let m = m. as_ref ( ) ;
42
- let ( _, version, name) = refinery_core:: parse_migration_name ( m)
43
- . unwrap_or_else ( |e| panic ! ( "Couldn't parse migration filename '{}': {:?}" , m, e) ) ;
44
- let variant = Ident :: new ( name. to_upper_camel_case ( ) . as_str ( ) , Span2 :: call_site ( ) ) ;
45
- variants. push ( quote ! { #variant( Migration ) = #version } ) ;
46
- discriminants. push ( quote ! { #version => Self :: #variant( migration) } ) ;
38
+ use refinery_core:: SchemaVersion ;
39
+
40
+ let mut variants = Vec :: new ( ) ;
41
+ let mut discriminants = Vec :: new ( ) ;
42
+
43
+ for m in migration_names {
44
+ let m = m. as_ref ( ) ;
45
+ let ( _, version, name) = refinery_core:: parse_migration_name ( m)
46
+ . unwrap_or_else ( |e| panic ! ( "Couldn't parse migration filename '{}': {:?}" , m, e) ) ;
47
+ let version: SchemaVersion = version;
48
+ let variant = Ident :: new ( name. to_upper_camel_case ( ) . as_str ( ) , Span2 :: call_site ( ) ) ;
49
+ variants. push ( quote ! { #variant( Migration ) = #version } ) ;
50
+ discriminants. push ( quote ! { #version => Self :: #variant( migration) } ) ;
51
+ }
52
+ discriminants. push ( quote ! { v => panic!( "Invalid migration version '{}'" , v) } ) ;
53
+
54
+ #[ cfg( feature = "int8-versions" ) ]
55
+ let embedded = quote ! {
56
+ #[ repr( i64 ) ]
57
+ #[ derive( Debug ) ]
58
+ pub enum EmbeddedMigration {
59
+ #( #variants) , *
47
60
}
48
- discriminants . push ( quote ! { v => panic! ( "Invalid migration version '{}'" , v ) } ) ;
61
+ } ;
49
62
50
- let result = quote ! {
51
- #[ derive( Debug ) ]
52
- pub enum EmbeddedMigration {
53
- #( #variants) , *
54
- }
63
+ #[ cfg( not( feature = "int8-versions" ) ) ]
64
+ let embedded = quote ! {
65
+ #[ repr( i32 ) ]
66
+ #[ derive( Debug ) ]
67
+ pub enum EmbeddedMigration {
68
+ #( #variants) , *
69
+ }
70
+ } ;
71
+
72
+ quote ! {
73
+
74
+ #embedded
55
75
56
- impl From <Migration > for EmbeddedMigration {
57
- fn from( migration: Migration ) -> Self {
58
- match migration. version( ) as SchemaVersion {
59
- #( #discriminants) , *
60
- }
76
+ impl From <Migration > for EmbeddedMigration {
77
+ fn from( migration: Migration ) -> Self {
78
+ match migration. version( ) as SchemaVersion {
79
+ #( #discriminants) , *
61
80
}
62
81
}
63
- } ;
64
- result
65
- } else {
66
- quote ! ( )
82
+ }
67
83
}
68
84
}
69
85
@@ -123,7 +139,11 @@ pub fn embed_migrations(input: TokenStream) -> TokenStream {
123
139
}
124
140
125
141
let fnq = migration_fn_quoted ( _migrations) ;
142
+ #[ cfg( feature = "enums" ) ]
126
143
let enums = migration_enum_quoted ( migration_filenames. as_slice ( ) ) ;
144
+ #[ cfg( not( feature = "enums" ) ) ]
145
+ let enums = quote ! ( ) ;
146
+
127
147
( quote ! {
128
148
pub mod migrations {
129
149
#( #migrations_mods) *
@@ -138,10 +158,33 @@ pub fn embed_migrations(input: TokenStream) -> TokenStream {
138
158
mod tests {
139
159
use super :: { migration_fn_quoted, quote} ;
140
160
161
+ #[ cfg( all( feature = "enums" , feature = "int8-versions" ) ) ]
162
+ #[ test]
163
+ fn test_enum_fn_i8 ( ) {
164
+ let expected = concat ! {
165
+ "# [repr (i64)] " ,
166
+ "# [derive (Debug)] " ,
167
+ "pub enum EmbeddedMigration { " ,
168
+ "Foo (Migration) = 1i64 , " ,
169
+ "BarBaz (Migration) = 3i64 " ,
170
+ "} " ,
171
+ "impl From < Migration > for EmbeddedMigration { " ,
172
+ "fn from (migration : Migration) -> Self { " ,
173
+ "match migration . version () as SchemaVersion { " ,
174
+ "1i64 => Self :: Foo (migration) , " ,
175
+ "3i64 => Self :: BarBaz (migration) , " ,
176
+ "v => panic ! (\" Invalid migration version '{}'\" , v) " ,
177
+ "} } }"
178
+ } ;
179
+ let enums = super :: migration_enum_quoted ( & [ "V1__foo" , "U3__barBAZ" ] ) . to_string ( ) ;
180
+ assert_eq ! ( expected, enums) ;
181
+ }
182
+
183
+ #[ cfg( all( feature = "enums" , not( feature = "int8-versions" ) ) ) ]
141
184
#[ test]
142
- #[ cfg( feature = "enums" ) ]
143
185
fn test_enum_fn ( ) {
144
186
let expected = concat ! {
187
+ "# [repr (i32)] " ,
145
188
"# [derive (Debug)] " ,
146
189
"pub enum EmbeddedMigration { " ,
147
190
"Foo (Migration) = 1i32 , " ,
0 commit comments