Make eligible enums convertable to static strs

This commit is contained in:
SleeplessOne1917 2024-07-22 13:04:23 -04:00
parent 572a42d880
commit 87a2af4b5d
14 changed files with 414 additions and 402 deletions

762
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -149,8 +149,7 @@ regex = "1.10.4"
once_cell = "1.19.0" once_cell = "1.19.0"
diesel-derive-newtype = "2.1.2" diesel-derive-newtype = "2.1.2"
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] } diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
strum = "0.26.2" strum = { version = "0.26.3", features = ["derive"] }
strum_macros = "0.26.4"
itertools = "0.13.0" itertools = "0.13.0"
futures = "0.3.30" futures = "0.3.30"
http = "0.2.12" http = "0.2.12"

View file

@ -31,7 +31,7 @@ serde = { workspace = true }
actix-web = { workspace = true } actix-web = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
strum_macros = { workspace = true } strum = { workspace = true }
url = { workspace = true } url = { workspace = true }
http = { workspace = true } http = { workspace = true }
futures = { workspace = true } futures = { workspace = true }

View file

@ -13,10 +13,10 @@ use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{source::community::Community, traits::Crud}; use lemmy_db_schema::{source::community::Community, traits::Crud};
use lemmy_utils::{error::LemmyResult, LemmyErrorType}; use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::Display; use strum::{Display, IntoStaticStr};
use url::Url; use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize, Display)] #[derive(Clone, Debug, Display, Deserialize, Serialize, IntoStaticStr)]
pub enum LockType { pub enum LockType {
Lock, Lock,
} }

View file

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::Display; use strum::{Display, IntoStaticStr};
pub mod block; pub mod block;
pub mod community; pub mod community;
@ -8,7 +8,7 @@ pub mod deletion;
pub mod following; pub mod following;
pub mod voting; pub mod voting;
#[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Debug, Display, IntoStaticStr, Deserialize, Serialize, PartialEq, Eq)]
pub enum CreateOrUpdateType { pub enum CreateOrUpdateType {
Create, Create,
Update, Update,

View file

@ -8,7 +8,7 @@ use activitypub_federation::{config::Data, fetch::object_id::ObjectId};
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult}; use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::Display; use strum::{Display, IntoStaticStr};
use url::Url; use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@ -22,7 +22,7 @@ pub struct Vote {
pub(crate) audience: Option<ObjectId<ApubCommunity>>, pub(crate) audience: Option<ObjectId<ApubCommunity>>,
} }
#[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Debug, Display, IntoStaticStr, Deserialize, Serialize, PartialEq, Eq)]
pub enum VoteType { pub enum VoteType {
Like, Like,
Dislike, Dislike,

View file

@ -46,7 +46,6 @@ serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }
url = { workspace = true } url = { workspace = true }
strum = { workspace = true } strum = { workspace = true }
strum_macros = { workspace = true }
serde_json = { workspace = true, optional = true } serde_json = { workspace = true, optional = true }
activitypub_federation = { workspace = true, optional = true } activitypub_federation = { workspace = true, optional = true }
lemmy_utils = { workspace = true, optional = true } lemmy_utils = { workspace = true, optional = true }

View file

@ -48,12 +48,12 @@ pub mod utils;
mod schema_setup; mod schema_setup;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString}; use strum::{Display, EnumString, IntoStaticStr};
#[cfg(feature = "full")] #[cfg(feature = "full")]
use ts_rs::TS; use ts_rs::TS;
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash, EnumString, IntoStaticStr, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash,
)] )]
#[cfg_attr(feature = "full", derive(DbEnum, TS))] #[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr( #[cfg_attr(
@ -87,7 +87,7 @@ pub enum SortType {
Scaled, Scaled,
} }
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)] #[derive(EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// The comment sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html /// The comment sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
@ -100,7 +100,7 @@ pub enum CommentSortType {
} }
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash, EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash,
)] )]
#[cfg_attr(feature = "full", derive(DbEnum, TS))] #[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr( #[cfg_attr(
@ -123,7 +123,7 @@ pub enum ListingType {
} }
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash, EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash,
)] )]
#[cfg_attr(feature = "full", derive(DbEnum, TS))] #[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr( #[cfg_attr(
@ -144,7 +144,7 @@ pub enum RegistrationMode {
} }
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq, Eq, Hash, EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq, Eq, Hash,
)] )]
#[cfg_attr(feature = "full", derive(DbEnum, TS))] #[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr( #[cfg_attr(
@ -164,7 +164,7 @@ pub enum PostListingMode {
SmallCard, SmallCard,
} }
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)] #[derive(EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// The type of content returned from a search. /// The type of content returned from a search.
@ -177,7 +177,7 @@ pub enum SearchType {
Url, Url,
} }
#[derive(EnumString, Display, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash)] #[derive(EnumString, Display, IntoStaticStr, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// A type / status for a community subscribe. /// A type / status for a community subscribe.
@ -187,7 +187,7 @@ pub enum SubscribedType {
Pending, Pending,
} }
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)] #[derive(EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// A list of possible types for the various modlog actions. /// A list of possible types for the various modlog actions.
@ -211,7 +211,7 @@ pub enum ModlogActionType {
} }
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash, EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash,
)] )]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -225,7 +225,7 @@ pub enum PostFeatureType {
} }
#[derive( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash, EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash,
)] )]
#[cfg_attr(feature = "full", derive(DbEnum, TS))] #[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr( #[cfg_attr(

View file

@ -33,7 +33,6 @@ serde_with = { workspace = true }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }
chrono.workspace = true chrono.workspace = true
strum = { workspace = true } strum = { workspace = true }
strum_macros = { workspace = true }
[dev-dependencies] [dev-dependencies]
serial_test = { workspace = true } serial_test = { workspace = true }

View file

@ -27,7 +27,7 @@ use lemmy_db_schema::{
SortType, SortType,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString}; use strum::{Display, EnumString, IntoStaticStr};
enum ListMode { enum ListMode {
Admins, Admins,
@ -35,7 +35,7 @@ enum ListMode {
Query(PersonQuery), Query(PersonQuery),
} }
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)] #[derive(EnumString, Display, IntoStaticStr, Debug, Serialize, Deserialize, Clone, Copy)]
/// The person sort types. Converted automatically from `SortType` /// The person sort types. Converted automatically from `SortType`
enum PersonSortType { enum PersonSortType {
New, New,

View file

@ -172,7 +172,7 @@ async fn get_feed_data(
let mut channel = Channel { let mut channel = Channel {
namespaces: RSS_NAMESPACE.clone(), namespaces: RSS_NAMESPACE.clone(),
title: format!("{} - {}", site_view.site.name, listing_type), title: format!("{} - {}", site_view.site.name, Into::<&'static str>::into(listing_type)),
link: context.settings().get_protocol_and_hostname(), link: context.settings().get_protocol_and_hostname(),
items, items,
..Default::default() ..Default::default()

View file

@ -64,7 +64,6 @@ actix-web = { workspace = true, optional = true }
anyhow = { workspace = true, optional = true } anyhow = { workspace = true, optional = true }
reqwest-middleware = { workspace = true, optional = true } reqwest-middleware = { workspace = true, optional = true }
strum = { workspace = true } strum = { workspace = true }
strum_macros = { workspace = true }
futures = { workspace = true, optional = true } futures = { workspace = true, optional = true }
diesel = { workspace = true, features = ["chrono"], optional = true } diesel = { workspace = true, features = ["chrono"], optional = true }
http = { workspace = true, optional = true } http = { workspace = true, optional = true }

View file

@ -1,7 +1,7 @@
use cfg_if::cfg_if; use cfg_if::cfg_if;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use strum_macros::{Display, EnumIter}; use strum::{Display, EnumIter};
#[derive(Display, Debug, Serialize, Deserialize, Clone, PartialEq, Eq, EnumIter, Hash)] #[derive(Display, Debug, Serialize, Deserialize, Clone, PartialEq, Eq, EnumIter, Hash)]
#[cfg_attr(feature = "full", derive(ts_rs::TS))] #[cfg_attr(feature = "full", derive(ts_rs::TS))]

View file

@ -6,7 +6,7 @@ use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr}, net::{IpAddr, Ipv4Addr, Ipv6Addr},
time::Instant, time::Instant,
}; };
use strum_macros::AsRefStr; use strum::{Display, IntoStaticStr};
use tracing::debug; use tracing::debug;
static START_TIME: Lazy<Instant> = Lazy::new(Instant::now); static START_TIME: Lazy<Instant> = Lazy::new(Instant::now);
@ -66,7 +66,7 @@ impl Bucket {
} }
} }
#[derive(Debug, enum_map::Enum, Copy, Clone, AsRefStr)] #[derive(Debug, enum_map::Enum, Copy, Clone, Display, IntoStaticStr)]
pub enum ActionType { pub enum ActionType {
Message, Message,
Register, Register,