From 4f8ad56cc485c80c3f6a690b7f05936097d95894 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 10 Mar 2023 21:49:08 +0800 Subject: [PATCH] Cont. Added support for using sea-orm with #[deny(missing_docs)] (#1531) * Added support for using sea-orm with #[deny(missing_docs)] (#1522) * feat(macros): Added documentation tags for generated entities * chore: Added deny(missing_docs) attribute to basic example * chore: Fix clippy errors * ci: test missing docs of derive macros generated types * Try missing docs (CI should fail) * Revert "Try missing docs (CI should fail)" This reverts commit 83356bfca8939e7807f14bad8bb816fcabc1bf7b. --------- Co-authored-by: Lewin Probst, M.Sc <30552361+emirror-de@users.noreply.github.com> --- examples/basic/src/example_cake.rs | 7 +++++++ examples/basic/src/example_cake_filling.rs | 15 +++++++++++++++ examples/basic/src/example_filling.rs | 12 ++++++++++++ examples/basic/src/example_fruit.rs | 15 +++++++++++++++ examples/basic/src/main.rs | 13 +++++++++---- examples/basic/src/sea_orm_active_enums.rs | 15 +++++++++++++++ sea-orm-macros/src/derives/active_enum.rs | 4 ++++ sea-orm-macros/src/derives/active_model.rs | 16 ++++++++-------- sea-orm-macros/src/derives/entity_model.rs | 9 ++++++++- 9 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 examples/basic/src/sea_orm_active_enums.rs diff --git a/examples/basic/src/example_cake.rs b/examples/basic/src/example_cake.rs index 6d877ac6..538443c9 100644 --- a/examples/basic/src/example_cake.rs +++ b/examples/basic/src/example_cake.rs @@ -1,15 +1,22 @@ +//! The `cake` entity. + use sea_orm::entity::prelude::*; +/// Cake entity #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[sea_orm(table_name = "cake")] pub struct Model { #[sea_orm(primary_key)] + /// id field pub id: i32, + /// name field pub name: String, } +/// Cake relation #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + /// Fruit relation Fruit, } diff --git a/examples/basic/src/example_cake_filling.rs b/examples/basic/src/example_cake_filling.rs index 1ce203f6..741ec120 100644 --- a/examples/basic/src/example_cake_filling.rs +++ b/examples/basic/src/example_cake_filling.rs @@ -1,5 +1,8 @@ +//! The `cake_filling` entity. + use sea_orm::entity::prelude::*; +/// CakeFilling entity #[derive(Copy, Clone, Default, Debug, DeriveEntity)] pub struct Entity; @@ -9,21 +12,30 @@ impl EntityName for Entity { } } +/// CakeFilling model #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] pub struct Model { + /// cake_id field pub cake_id: i32, + /// filling_id field pub filling_id: i32, } +/// CakeFilling column #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { + /// CakeId column CakeId, + /// FillingId column FillingId, } +/// CakeFilling primary key #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { + /// CakeId primary key CakeId, + /// FillingId primary key FillingId, } @@ -35,9 +47,12 @@ impl PrimaryKeyTrait for PrimaryKey { } } +/// CakeFilling relation #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + /// Cake relation Cake, + /// Filling relation Filling, } diff --git a/examples/basic/src/example_filling.rs b/examples/basic/src/example_filling.rs index 363f31db..008d84b4 100644 --- a/examples/basic/src/example_filling.rs +++ b/examples/basic/src/example_filling.rs @@ -1,5 +1,8 @@ +//! The `filling` entity. + use sea_orm::entity::prelude::*; +/// Filling entity #[derive(Copy, Clone, Default, Debug, DeriveEntity)] pub struct Entity; @@ -9,20 +12,28 @@ impl EntityName for Entity { } } +/// Filling model #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] pub struct Model { + /// id field pub id: i32, + /// name field pub name: String, } +/// Filling column #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { + /// Id column Id, + /// Name column Name, } +/// Filling primary key #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { + /// Id primary key Id, } @@ -34,6 +45,7 @@ impl PrimaryKeyTrait for PrimaryKey { } } +/// Filling relation #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation {} diff --git a/examples/basic/src/example_fruit.rs b/examples/basic/src/example_fruit.rs index 0850a298..59a072e9 100644 --- a/examples/basic/src/example_fruit.rs +++ b/examples/basic/src/example_fruit.rs @@ -1,5 +1,8 @@ +//! The `fruit` entity. + use sea_orm::entity::prelude::*; +/// Fruit entity #[derive(Copy, Clone, Default, Debug, DeriveEntity)] pub struct Entity; @@ -9,22 +12,32 @@ impl EntityName for Entity { } } +/// Fruit model #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] pub struct Model { + /// id field pub id: i32, + /// name field pub name: String, + /// cake_id field pub cake_id: Option, } +/// Fruit column #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { + /// Id column Id, + /// Name column Name, + /// CakeId column CakeId, } +/// Fruit primary key #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { + /// Id primary key Id, } @@ -36,8 +49,10 @@ impl PrimaryKeyTrait for PrimaryKey { } } +/// Fruit relation #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { + /// Cake relation Cake, } diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index ca2aee11..58c09db0 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -1,11 +1,16 @@ +//! Basic sea-orm example. + +#![deny(missing_docs)] + use sea_orm::Database; mod entities; -mod example_cake; -mod example_cake_filling; -mod example_filling; -mod example_fruit; +pub mod example_cake; +pub mod example_cake_filling; +pub mod example_filling; +pub mod example_fruit; mod operation; +pub mod sea_orm_active_enums; mod select; use entities::*; diff --git a/examples/basic/src/sea_orm_active_enums.rs b/examples/basic/src/sea_orm_active_enums.rs new file mode 100644 index 00000000..d6629961 --- /dev/null +++ b/examples/basic/src/sea_orm_active_enums.rs @@ -0,0 +1,15 @@ +//! SeaORM's active enums. + +use sea_orm::entity::prelude::*; + +/// Tea active enum +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")] +pub enum Tea { + /// EverydayTea variant + #[sea_orm(string_value = "EverydayTea")] + EverydayTea, + /// BreakfastTea variant + #[sea_orm(string_value = "BreakfastTea")] + BreakfastTea, +} diff --git a/sea-orm-macros/src/derives/active_enum.rs b/sea-orm-macros/src/derives/active_enum.rs index 74e8c4ab..41d1513b 100644 --- a/sea-orm-macros/src/derives/active_enum.rs +++ b/sea-orm-macros/src/derives/active_enum.rs @@ -256,9 +256,11 @@ impl ActiveEnum { .collect(); quote!( + #[doc = " Generated by sea-orm-macros"] #[derive(Debug, Clone, PartialEq, Eq, sea_orm::EnumIter)] pub enum #enum_variant_iden { #( + #[doc = " Generated by sea-orm-macros"] #enum_variants, )* } @@ -276,6 +278,7 @@ impl ActiveEnum { #[automatically_derived] impl #ident { + #[doc = " Generated by sea-orm-macros"] pub fn iden_values() -> Vec { <#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter() .map(|v| sea_orm::sea_query::SeaRc::new(v) as sea_orm::sea_query::DynIden) @@ -297,6 +300,7 @@ impl ActiveEnum { }; quote!( + #[doc = " Generated by sea-orm-macros"] #[derive(Debug, Clone, PartialEq, Eq)] pub struct #enum_name_iden; diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs index 5dbba669..18a7579c 100644 --- a/sea-orm-macros/src/derives/active_model.rs +++ b/sea-orm-macros/src/derives/active_model.rs @@ -38,11 +38,10 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result) -> syn::Result { let fields = all_fields.filter(field_not_ignored); - let field: Vec = fields.clone().into_iter().map(format_field_ident).collect(); + let field: Vec = fields.clone().map(format_field_ident).collect(); let name: Vec = fields .clone() - .into_iter() .map(|field| { let ident = field.ident.as_ref().unwrap().to_string(); let ident = trim_starting_raw_identifier(ident).to_camel_case(); @@ -78,9 +77,14 @@ fn derive_active_model(all_fields: IntoIter) -> syn::Result let ty: Vec = fields.into_iter().map(|Field { ty, .. }| ty).collect(); Ok(quote!( + #[doc = " Generated by sea-orm-macros"] #[derive(Clone, Debug, PartialEq)] pub struct ActiveModel { - #(pub #field: sea_orm::ActiveValue<#ty>),* + + #( + #[doc = " Generated by sea-orm-macros"] + pub #field: sea_orm::ActiveValue<#ty> + ),* } #[automatically_derived] @@ -172,11 +176,7 @@ fn derive_into_model(model_fields: IntoIter) -> syn::Result .into_iter() .map(format_field_ident) .collect(); - let model_field: Vec = model_fields - .clone() - .into_iter() - .map(format_field_ident) - .collect(); + let model_field: Vec = model_fields.clone().map(format_field_ident).collect(); let ignore_attr: Vec = model_fields .map(|field| !field_not_ignored(&field)) diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 2b27121b..be5c4b28 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -43,6 +43,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res .as_ref() .map(|table_name| { quote! { + #[doc = " Generated by sea-orm-macros"] #[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)] pub struct Entity; @@ -72,6 +73,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res if let Some(table_name) = table_name { let table_field_name = Ident::new("Table", Span::call_site()); columns_enum.push(quote! { + #[doc = " Generated by sea-orm-macros"] #[sea_orm(table_name=#table_name)] #[strum(disabled)] #table_field_name @@ -226,8 +228,11 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res let variant_attrs = match &column_name { Some(column_name) => quote! { #[sea_orm(column_name = #column_name)] + #[doc = " Generated by sea-orm-macros"] + }, + None => quote! { + #[doc = " Generated by sea-orm-macros"] }, - None => quote! {}, }; if ignore { @@ -360,6 +365,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res quote! { (#primary_key_types) } }; quote! { + #[doc = " Generated by sea-orm-macros"] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { #primary_keys @@ -377,6 +383,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res }; Ok(quote! { + #[doc = " Generated by sea-orm-macros"] #[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)] pub enum Column { #columns_enum