diff --git a/crates/codegen/src/column_derive.rs b/crates/codegen/src/column_derive.rs index 347736b..7794339 100644 --- a/crates/codegen/src/column_derive.rs +++ b/crates/codegen/src/column_derive.rs @@ -3,6 +3,7 @@ use syn; pub fn impl_to_column_names(ast: &syn::MacroInput) -> quote::Tokens { let name = &ast.ident; + let generics = &ast.generics; let fields: Vec<(&syn::Ident, &syn::Ty)> = match ast.body { syn::Body::Struct(ref data) => match *data { syn::VariantData::Struct(ref fields) => fields @@ -31,8 +32,7 @@ pub fn impl_to_column_names(ast: &syn::MacroInput) -> quote::Tokens { .collect(); quote! { - impl rustorm_dao::ToColumnNames for #name { - + impl #generics rustorm_dao::ToColumnNames for #name #generics { fn to_column_names() -> Vec { vec![ #(#from_fields)* diff --git a/crates/codegen/src/dao_derive.rs b/crates/codegen/src/dao_derive.rs index 2d21b82..fa76a06 100644 --- a/crates/codegen/src/dao_derive.rs +++ b/crates/codegen/src/dao_derive.rs @@ -39,6 +39,7 @@ pub fn impl_from_dao(ast: &syn::MacroInput) -> quote::Tokens { pub fn impl_to_dao(ast: &syn::MacroInput) -> quote::Tokens { let name = &ast.ident; + let generics = &ast.generics; let fields: Vec<(&syn::Ident, &syn::Ty)> = match ast.body { syn::Body::Struct(ref data) => match *data { syn::VariantData::Struct(ref fields) => fields @@ -61,8 +62,7 @@ pub fn impl_to_dao(ast: &syn::MacroInput) -> quote::Tokens { .collect(); quote! { - impl rustorm_dao::ToDao for #name { - + impl #generics rustorm_dao::ToDao for #name #generics { fn to_dao(&self) -> rustorm_dao::Dao { let mut dao = rustorm_dao::Dao::new(); #(#from_fields)* diff --git a/crates/codegen/src/table_derive.rs b/crates/codegen/src/table_derive.rs index f912438..17115c2 100644 --- a/crates/codegen/src/table_derive.rs +++ b/crates/codegen/src/table_derive.rs @@ -3,9 +3,10 @@ use syn; pub fn impl_to_table_name(ast: &syn::MacroInput) -> quote::Tokens { let name = &ast.ident; - quote! { - impl rustorm_dao::ToTableName for #name { + let generics = &ast.generics; + quote! { + impl #generics rustorm_dao::ToTableName for #name #generics { fn to_table_name() -> rustorm_dao::TableName { rustorm_dao::TableName{ name: stringify!(#name).to_lowercase().into(), diff --git a/crates/dao/src/value.rs b/crates/dao/src/value.rs index 3850a8b..83d4a4f 100644 --- a/crates/dao/src/value.rs +++ b/crates/dao/src/value.rs @@ -120,6 +120,11 @@ macro_rules! impl_from { fn from(f: &'a $ty) -> Self { Value::$variant(f.to_owned()) } } + /// For dobule borrowed types + impl<'a> From<&&'a $ty> for Value { + fn from(f: &&'a $ty) -> Self { Value::$variant(f.to_owned().to_owned()) } + } + /// for borrowed option types impl<'a> From<&'a Option<$ty>> for Value { fn from(f: &'a Option<$ty>) -> Self { @@ -178,6 +183,10 @@ impl<'a> From<&'a str> for Value { fn from(f: &'a str) -> Value { Value::Text(f.to_string()) } } +impl<'a> From<&&'a str> for Value { + fn from(f: &&'a str) -> Value { Value::Text(f.to_string()) } +} + impl ToValue for &str { fn to_value(&self) -> Value { Value::Text(self.to_string()) } } @@ -232,7 +241,6 @@ macro_rules! impl_tryfrom_numeric { } } } - } } @@ -252,6 +260,7 @@ macro_rules! impl_tryfrom_option { }; } + /// Char can be casted into String /// and they havea separate implementation for extracting data impl<'a> TryFrom<&'a Value> for String { @@ -280,7 +289,6 @@ impl<'a> TryFrom<&'a Value> for String { } } -impl_tryfrom!(bool, "bool", Bool); impl_tryfrom!(Vec, "Vec", Blob); impl_tryfrom!(char, "char", Char); impl_tryfrom!(Uuid, "Uuid", Uuid); @@ -292,6 +300,26 @@ impl_tryfrom_numeric!(i64, to_i64, "i64", Tinyint, Smallint, Int, Bigint); impl_tryfrom_numeric!(f32, to_f32, "f32", Float); impl_tryfrom_numeric!(f64, to_f64, "f64", Float, Double); +impl<'a> TryFrom<&'a Value> for bool { + type Error = ConvertError; + + fn try_from(value: &'a Value) -> Result { + match *value { + Value::Bool(v) => Ok(v), + Value::Tinyint(v) => Ok(v == 1), + Value::Smallint(v) => Ok(v == 1), + Value::Int(v) => Ok(v == 1), + Value::Bigint(v) => Ok(v == 1), + _ => { + Err(ConvertError::NotSupported( + format!("{:?}", value), + "bool".to_string(), + )) + } + } + } +} + impl<'a> TryFrom<&'a Value> for NaiveDateTime { type Error = ConvertError;