Use DeriveIden to replace ToString

This commit is contained in:
Chris Tsang 2021-09-30 22:05:23 +08:00
parent 5728ac4329
commit fcd969e57c
3 changed files with 12 additions and 29 deletions

View File

@ -320,24 +320,14 @@ pub trait TryGetableMany: Sized {
/// # ]]) /// # ]])
/// # .into_connection(); /// # .into_connection();
/// # /// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, EnumIter, TryGetableMany}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, EnumIter, DeriveIden, TryGetableMany};
/// ///
/// #[derive(EnumIter)] /// #[derive(EnumIter, DeriveIden)]
/// enum ResultCol { /// enum ResultCol {
/// Name, /// Name,
/// NumOfCakes, /// NumOfCakes,
/// } /// }
/// ///
/// // this can be derived using derive_more crate
/// impl std::fmt::Display for ResultCol {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// match self {
/// Self::Name => write!(f, "name"),
/// Self::NumOfCakes => write!(f, "num_of_cakes"),
/// }
/// }
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async { /// # let _: Result<(), DbErr> = smol::block_on(async {
/// # /// #
/// let res: Vec<(String, i32)> = /// let res: Vec<(String, i32)> =
@ -371,7 +361,7 @@ pub trait TryGetableMany: Sized {
/// ``` /// ```
fn find_by_statement<C>(stmt: Statement) -> SelectorRaw<SelectGetableValue<Self, C>> fn find_by_statement<C>(stmt: Statement) -> SelectorRaw<SelectGetableValue<Self, C>>
where where
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
SelectorRaw::<SelectGetableValue<Self, C>>::with_columns(stmt) SelectorRaw::<SelectGetableValue<Self, C>>::with_columns(stmt)
} }

View File

@ -34,7 +34,7 @@ pub trait SelectorTrait {
pub struct SelectGetableValue<T, C> pub struct SelectGetableValue<T, C>
where where
T: TryGetableMany, T: TryGetableMany,
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
columns: PhantomData<C>, columns: PhantomData<C>,
model: PhantomData<T>, model: PhantomData<T>,
@ -60,7 +60,7 @@ where
impl<T, C> SelectorTrait for SelectGetableValue<T, C> impl<T, C> SelectorTrait for SelectGetableValue<T, C>
where where
T: TryGetableMany, T: TryGetableMany,
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
type Item = T; type Item = T;
@ -143,22 +143,13 @@ where
/// # ]]) /// # ]])
/// # .into_connection(); /// # .into_connection();
/// # /// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, EnumIter, TryGetableMany}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, EnumIter, DeriveIden, TryGetableMany};
/// ///
/// #[derive(EnumIter)] /// #[derive(EnumIter, DeriveIden)]
/// enum ResultCol { /// enum ResultCol {
/// Name, /// Name,
/// } /// }
/// ///
/// // this can be derived using derive_more crate
/// impl std::fmt::Display for ResultCol {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// match self {
/// Self::Name => write!(f, "name"),
/// }
/// }
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async { /// # let _: Result<(), DbErr> = smol::block_on(async {
/// # /// #
/// let res: Vec<String> = cake::Entity::find() /// let res: Vec<String> = cake::Entity::find()
@ -188,7 +179,7 @@ where
pub fn into_values<T, C>(self) -> Selector<SelectGetableValue<T, C>> pub fn into_values<T, C>(self) -> Selector<SelectGetableValue<T, C>>
where where
T: TryGetableMany, T: TryGetableMany,
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
Selector::<SelectGetableValue<T, C>>::with_columns(self.query) Selector::<SelectGetableValue<T, C>>::with_columns(self.query)
} }
@ -323,7 +314,7 @@ where
pub fn with_columns<T, C>(query: SelectStatement) -> Selector<SelectGetableValue<T, C>> pub fn with_columns<T, C>(query: SelectStatement) -> Selector<SelectGetableValue<T, C>>
where where
T: TryGetableMany, T: TryGetableMany,
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
Selector { Selector {
query, query,
@ -386,7 +377,7 @@ where
pub fn with_columns<T, C>(stmt: Statement) -> SelectorRaw<SelectGetableValue<T, C>> pub fn with_columns<T, C>(stmt: Statement) -> SelectorRaw<SelectGetableValue<T, C>>
where where
T: TryGetableMany, T: TryGetableMany,
C: sea_strum::IntoEnumIterator + ToString, C: sea_strum::IntoEnumIterator + sea_query::Iden,
{ {
SelectorRaw { SelectorRaw {
stmt, stmt,

View File

@ -440,5 +440,7 @@ pub use sea_orm_macros::{
pub use sea_query; pub use sea_query;
pub use sea_query::Iden; pub use sea_query::Iden;
pub use sea_query::Iden as DeriveIden;
pub use sea_strum; pub use sea_strum;
pub use sea_strum::EnumIter; pub use sea_strum::EnumIter;