cargo fmt

This commit is contained in:
Chris Tsang 2021-05-17 19:51:26 +08:00
parent 106bb20cb5
commit 62b3c37e20
10 changed files with 64 additions and 84 deletions

View File

@ -1,4 +1,4 @@
use sea_orm::{ColumnTrait, Database, EntityTrait, QueryErr, QueryHelper, FromQueryResult};
use sea_orm::{ColumnTrait, Database, EntityTrait, FromQueryResult, QueryErr, QueryHelper};
mod example_cake;
mod example_fruit;

View File

@ -1,24 +1,24 @@
use heck::SnakeCase;
use proc_macro2::{Ident, TokenStream};
use syn::{Data, DataEnum, Fields, Variant};
use quote::{quote, quote_spanned};
use syn::{Data, DataEnum, Fields, Variant};
pub fn expend_derive_column(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let variants = match data {
syn::Data::Enum(DataEnum { variants, .. }) => variants,
_ => return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveColumn on enums");
}),
_ => {
return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveColumn on enums");
})
}
};
let variant: Vec<TokenStream> = variants
.iter()
.map(|Variant { ident, fields, .. }| {
match fields {
Fields::Named(_) => quote! { #ident{..} },
Fields::Unnamed(_) => quote! { #ident(..) },
Fields::Unit => quote! { #ident },
}
.map(|Variant { ident, fields, .. }| match fields {
Fields::Named(_) => quote! { #ident{..} },
Fields::Unnamed(_) => quote! { #ident(..) },
Fields::Unit => quote! { #ident },
})
.collect();
@ -45,4 +45,4 @@ pub fn expend_derive_column(ident: Ident, data: Data) -> syn::Result<TokenStream
}
}
))
}
}

View File

@ -1,7 +1,7 @@
use heck::SnakeCase;
use proc_macro2::{Ident, TokenStream};
use syn::{Attribute, Meta};
use quote::quote;
use syn::{Attribute, Meta};
fn get_entity_attr(attrs: &[Attribute]) -> Option<syn::Lit> {
for attr in attrs {
@ -40,4 +40,4 @@ pub fn expend_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<
}
}
))
}
}

View File

@ -1,25 +1,23 @@
use proc_macro2::{Ident, TokenStream};
use syn::{Data, DataStruct, Field, Fields};
use quote::{format_ident, quote, quote_spanned};
use syn::{Data, DataStruct, Field, Fields};
pub fn expend_derive_from_query_result(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let fields = match data {
Data::Struct(DataStruct {
fields: Fields::Named(named),
..
}) => {
named.named
},
_ => return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveModel on structs");
}),
}) => named.named,
_ => {
return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveModel on structs");
})
}
};
let field: Vec<Ident> = fields
.into_iter()
.map(|Field { ident, .. }| {
format_ident!("{}", ident.unwrap().to_string())
})
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string()))
.collect();
let name: Vec<TokenStream> = field
@ -39,4 +37,4 @@ pub fn expend_derive_from_query_result(ident: Ident, data: Data) -> syn::Result<
}
}
))
}
}

View File

@ -1,11 +1,11 @@
mod column;
mod entity;
mod from_query_result;
mod primary_key;
mod column;
mod model;
mod primary_key;
pub use column::*;
pub use entity::*;
pub use from_query_result::*;
pub use model::*;
pub use primary_key::*;
pub use column::*;
pub use model::*;

View File

@ -1,52 +1,48 @@
use heck::CamelCase;
use proc_macro2::{Ident, TokenStream};
use syn::{Data, DataStruct, Field, Fields};
use quote::{format_ident, quote, quote_spanned};
use syn::{Data, DataStruct, Field, Fields};
pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let fields = match data {
Data::Struct(DataStruct {
fields: Fields::Named(named),
..
}) => {
named.named
},
_ => return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveModel on structs");
}),
}) => named.named,
_ => {
return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DeriveModel on structs");
})
}
};
let field: Vec<Ident> = fields
.clone()
.into_iter()
.map(|Field { ident, .. }| {
format_ident!("{}", ident.unwrap().to_string())
})
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string()))
.collect();
let name: Vec<Ident> = fields
.into_iter()
.map(|Field { ident, .. }| {
format_ident!("{}", ident.unwrap().to_string().to_camel_case())
})
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case()))
.collect();
Ok(quote!(
impl ModelTrait for #ident {
type Column = Column;
fn get(&self, c: Self::Column) -> Value {
match c {
#(Self::Column::#name => self.#field.clone().into()),*
}
}
fn set(&mut self, c: Self::Column, v: Value) {
match c {
#(Self::Column::#name => self.#field = v.unwrap()),*
}
}
fn from_query_result(row: &QueryResult, pre: &str) -> Result<Self, TypeErr> {
Ok(Self {
#(#field: row.try_get(pre, Self::Column::#name.as_str().into())?),*
@ -54,4 +50,4 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
}
}
))
}
}

View File

@ -1,24 +1,24 @@
use heck::SnakeCase;
use proc_macro2::{Ident, TokenStream};
use syn::{Data, DataEnum, Fields, Variant};
use quote::{quote, quote_spanned};
use syn::{Data, DataEnum, Fields, Variant};
pub fn expend_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let variants = match data {
syn::Data::Enum(DataEnum { variants, .. }) => variants,
_ => return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DerivePrimaryKey on enums");
}),
_ => {
return Ok(quote_spanned! {
ident.span() => compile_error!("you can only derive DerivePrimaryKey on enums");
})
}
};
let variant: Vec<TokenStream> = variants
.iter()
.map(|Variant { ident, fields, .. }| {
match fields {
Fields::Named(_) => quote! { #ident{..} },
Fields::Unnamed(_) => quote! { #ident(..) },
Fields::Unit => quote! { #ident },
}
.map(|Variant { ident, fields, .. }| match fields {
Fields::Named(_) => quote! { #ident{..} },
Fields::Unnamed(_) => quote! { #ident(..) },
Fields::Unit => quote! { #ident },
})
.collect();
@ -55,4 +55,4 @@ pub fn expend_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
}
}
))
}
}

View File

@ -1,15 +1,13 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};
use syn::{parse_macro_input, DeriveInput};
mod derives;
#[proc_macro_derive(DeriveEntity, attributes(entity))]
pub fn derive_entity(input: TokenStream) -> TokenStream {
let DeriveInput {
ident, attrs, ..
} = parse_macro_input!(input);
let DeriveInput { ident, attrs, .. } = parse_macro_input!(input);
match derives::expend_derive_entity(ident, attrs) {
Ok(ts) => ts.into(),
@ -19,9 +17,7 @@ pub fn derive_entity(input: TokenStream) -> TokenStream {
#[proc_macro_derive(DerivePrimaryKey)]
pub fn derive_primary_key(input: TokenStream) -> TokenStream {
let DeriveInput {
ident, data, ..
} = parse_macro_input!(input);
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
match derives::expend_derive_primary_key(ident, data) {
Ok(ts) => ts.into(),
@ -31,9 +27,7 @@ pub fn derive_primary_key(input: TokenStream) -> TokenStream {
#[proc_macro_derive(DeriveColumn)]
pub fn derive_column(input: TokenStream) -> TokenStream {
let DeriveInput {
ident, data, ..
} = parse_macro_input!(input);
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
match derives::expend_derive_column(ident, data) {
Ok(ts) => ts.into(),
@ -43,9 +37,7 @@ pub fn derive_column(input: TokenStream) -> TokenStream {
#[proc_macro_derive(DeriveModel)]
pub fn derive_model(input: TokenStream) -> TokenStream {
let DeriveInput {
ident, data, ..
} = parse_macro_input!(input);
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
match derives::expend_derive_model(ident, data) {
Ok(ts) => ts.into(),
@ -55,12 +47,10 @@ pub fn derive_model(input: TokenStream) -> TokenStream {
#[proc_macro_derive(FromQueryResult)]
pub fn derive_from_query_result(input: TokenStream) -> TokenStream {
let DeriveInput {
ident, data, ..
} = parse_macro_input!(input);
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
match derives::expend_derive_from_query_result(ident, data) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error().into(),
}
}
}

View File

@ -1,5 +1,5 @@
pub use crate::{
ColumnTrait, ColumnType, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related, RelationDef, RelationTrait, Select,
TypeErr, Value, DeriveEntity, DerivePrimaryKey, DeriveColumn, DeriveModel
ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName,
EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait,
QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value,
};

View File

@ -12,13 +12,9 @@ pub use driver::*;
pub use entity::*;
pub use query::*;
pub use sea_orm_macros::{
DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult,
};
pub use sea_query;
pub use sea_query::Iden;
pub use strum::EnumIter;
pub use sea_orm_macros::{
DeriveEntity,
DerivePrimaryKey,
DeriveColumn,
DeriveModel,
FromQueryResult,
};