Derive macros traits with crate name

This commit is contained in:
Billy Chan 2021-05-18 18:43:14 +08:00
parent 90388a3523
commit 8d92e09858
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
9 changed files with 19 additions and 14 deletions

View File

@ -3,4 +3,5 @@ cp ../../src/tests_cfg/fruit.rs src/example_fruit.rs
cp ../../src/tests_cfg/filling.rs src/example_filling.rs
cp ../../src/tests_cfg/cake_filling.rs src/example_cake_filling.rs
sed -i 's/^use crate::/use sea_orm::/g' src/*.rs
sed -i 's/^use crate::/use sea_orm::/g' src/*.rs
sed -i '/^use crate as sea_orm;/d' src/*.rs

View File

@ -31,13 +31,13 @@ pub fn expend_derive_column(ident: Ident, data: Data) -> syn::Result<TokenStream
.collect();
Ok(quote!(
impl Iden for #ident {
impl sea_orm::Iden for #ident {
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
impl IdenStatic for #ident {
impl sea_orm::IdenStatic for #ident {
fn as_str(&self) -> &str {
match self {
#(Self::#variant => #name),*

View File

@ -26,15 +26,15 @@ pub fn expend_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<
};
Ok(quote!(
impl EntityName for #ident {}
impl sea_orm::EntityName for #ident {}
impl IdenStatic for #ident {
impl sea_orm::IdenStatic for #ident {
fn as_str(&self) -> &str {
#entity_name
}
}
impl Iden for #ident {
impl sea_orm::Iden for #ident {
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}

View File

@ -28,22 +28,22 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
.collect();
Ok(quote!(
impl ModelTrait for #ident {
impl sea_orm::ModelTrait for #ident {
type Column = Column;
fn get(&self, c: Self::Column) -> Value {
fn get(&self, c: Self::Column) -> sea_orm::Value {
match c {
#(Self::Column::#name => self.#field.clone().into()),*
}
}
fn set(&mut self, c: Self::Column, v: Value) {
fn set(&mut self, c: Self::Column, v: sea_orm::Value) {
match c {
#(Self::Column::#name => self.#field = v.unwrap()),*
}
}
fn from_query_result(row: &QueryResult, pre: &str) -> Result<Self, TypeErr> {
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::TypeErr> {
Ok(Self {
#(#field: row.try_get(pre, Self::Column::#name.as_str().into())?),*
})

View File

@ -31,13 +31,13 @@ pub fn expend_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
.collect();
Ok(quote!(
impl Iden for #ident {
impl sea_orm::Iden for #ident {
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
impl IdenStatic for #ident {
impl sea_orm::IdenStatic for #ident {
fn as_str(&self) -> &str {
match self {
#(Self::#variant => #name),*
@ -45,9 +45,9 @@ pub fn expend_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
}
}
impl PrimaryKeyTrait for #ident {}
impl sea_orm::PrimaryKeyTrait for #ident {}
impl PrimaryKeyOfModel<Model> for #ident {
impl sea_orm::PrimaryKeyOfModel<Model> for #ident {
fn into_column(self) -> <Model as ModelTrait>::Column {
match self {
#(Self::#variant => Column::#variant),*

View File

@ -1,4 +1,5 @@
use crate::entity::prelude::*;
use crate as sea_orm;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake"]

View File

@ -1,4 +1,5 @@
use crate::entity::prelude::*;
use crate as sea_orm;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake_filling"]

View File

@ -1,4 +1,5 @@
use crate::entity::prelude::*;
use crate as sea_orm;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "filling"]

View File

@ -1,4 +1,5 @@
use crate::entity::prelude::*;
use crate as sea_orm;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "fruit"]