Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/codegen/src/column_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<rustorm_dao::ColumnName> {
vec![
#(#from_fields)*
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/dao_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)*
Expand Down
5 changes: 3 additions & 2 deletions crates/codegen/src/table_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
32 changes: 30 additions & 2 deletions crates/dao/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) }
}
Expand Down Expand Up @@ -232,7 +241,6 @@ macro_rules! impl_tryfrom_numeric {
}
}
}

}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -280,7 +289,6 @@ impl<'a> TryFrom<&'a Value> for String {
}
}

impl_tryfrom!(bool, "bool", Bool);
impl_tryfrom!(Vec<u8>, "Vec<u8>", Blob);
impl_tryfrom!(char, "char", Char);
impl_tryfrom!(Uuid, "Uuid", Uuid);
Expand All @@ -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<Self, Self::Error> {
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;

Expand Down