refactor: Expanded the serde_enum_from macro to further reduce code duplication
This commit is contained in:
@@ -445,6 +445,21 @@ pub fn strip_non_search_characters(input: &str) -> String {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! serde_enum_from {
|
macro_rules! serde_enum_from {
|
||||||
($enum_name:ident { $($variant:ident($ty:ty),)* }) => {
|
($enum_name:ident { $($variant:ident($ty:ty),)* }) => {
|
||||||
|
#[derive(Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, Debug)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
pub enum $enum_name {
|
||||||
|
$(
|
||||||
|
$variant($ty),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<()> for $enum_name {
|
||||||
|
fn from(_: ()) -> Self {
|
||||||
|
$enum_name::Value(serde_json::json!({}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$(
|
$(
|
||||||
impl From<$ty> for $enum_name {
|
impl From<$ty> for $enum_name {
|
||||||
fn from(value: $ty) -> Self {
|
fn from(value: $ty) -> Self {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use chrono::{DateTime, Utc};
|
|||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use derivative::Derivative;
|
use derivative::Derivative;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Number, Value};
|
use serde_json::{Number, Value};
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
|
||||||
use crate::{models::HorizontallyScrollableText, serde_enum_from};
|
use crate::{models::HorizontallyScrollableText, serde_enum_from};
|
||||||
@@ -475,49 +475,12 @@ impl Display for RadarrTaskName {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
|
||||||
pub enum RadarrSerdeable {
|
|
||||||
Value(Value),
|
|
||||||
Tag(Tag),
|
|
||||||
BlocklistResponse(BlocklistResponse),
|
|
||||||
Collections(Vec<Collection>),
|
|
||||||
Credits(Vec<Credit>),
|
|
||||||
DiskSpaces(Vec<DiskSpace>),
|
|
||||||
DownloadsResponse(DownloadsResponse),
|
|
||||||
HostConfig(HostConfig),
|
|
||||||
Indexers(Vec<Indexer>),
|
|
||||||
IndexerSettings(IndexerSettings),
|
|
||||||
LogResponse(LogResponse),
|
|
||||||
Movie(Movie),
|
|
||||||
MovieHistoryItems(Vec<MovieHistoryItem>),
|
|
||||||
Movies(Vec<Movie>),
|
|
||||||
QualityProfiles(Vec<QualityProfile>),
|
|
||||||
QueueEvents(Vec<QueueEvent>),
|
|
||||||
Releases(Vec<RadarrRelease>),
|
|
||||||
RootFolders(Vec<RootFolder>),
|
|
||||||
SecurityConfig(SecurityConfig),
|
|
||||||
SystemStatus(SystemStatus),
|
|
||||||
Tags(Vec<Tag>),
|
|
||||||
Tasks(Vec<RadarrTask>),
|
|
||||||
Updates(Vec<Update>),
|
|
||||||
AddMovieSearchResults(Vec<AddMovieSearchResult>),
|
|
||||||
IndexerTestResults(Vec<IndexerTestResult>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RadarrSerdeable> for Serdeable {
|
impl From<RadarrSerdeable> for Serdeable {
|
||||||
fn from(value: RadarrSerdeable) -> Serdeable {
|
fn from(value: RadarrSerdeable) -> Serdeable {
|
||||||
Serdeable::Radarr(value)
|
Serdeable::Radarr(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<()> for RadarrSerdeable {
|
|
||||||
fn from(_: ()) -> Self {
|
|
||||||
RadarrSerdeable::Value(json!({}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serde_enum_from!(
|
serde_enum_from!(
|
||||||
RadarrSerdeable {
|
RadarrSerdeable {
|
||||||
Value(Value),
|
Value(Value),
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use chrono::{DateTime, Utc};
|
|||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use derivative::Derivative;
|
use derivative::Derivative;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Number, Value};
|
use serde_json::{Number, Value};
|
||||||
use strum::EnumIter;
|
use strum::EnumIter;
|
||||||
|
|
||||||
use crate::serde_enum_from;
|
use crate::serde_enum_from;
|
||||||
@@ -681,52 +681,12 @@ impl Display for SonarrTaskName {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
|
||||||
pub enum SonarrSerdeable {
|
|
||||||
AddSeriesSearchResults(Vec<AddSeriesSearchResult>),
|
|
||||||
BlocklistResponse(BlocklistResponse),
|
|
||||||
DownloadsResponse(DownloadsResponse),
|
|
||||||
DiskSpaces(Vec<DiskSpace>),
|
|
||||||
Episode(Episode),
|
|
||||||
Episodes(Vec<Episode>),
|
|
||||||
EpisodeFiles(Vec<EpisodeFile>),
|
|
||||||
HostConfig(HostConfig),
|
|
||||||
IndexerSettings(IndexerSettings),
|
|
||||||
Indexers(Vec<Indexer>),
|
|
||||||
IndexerTestResults(Vec<IndexerTestResult>),
|
|
||||||
LanguageProfiles(Vec<Language>),
|
|
||||||
LogResponse(LogResponse),
|
|
||||||
QualityProfiles(Vec<QualityProfile>),
|
|
||||||
QueueEvents(Vec<QueueEvent>),
|
|
||||||
Releases(Vec<SonarrRelease>),
|
|
||||||
RootFolders(Vec<RootFolder>),
|
|
||||||
SecurityConfig(SecurityConfig),
|
|
||||||
SeriesVec(Vec<Series>),
|
|
||||||
Series(Series),
|
|
||||||
SonarrHistoryItems(Vec<SonarrHistoryItem>),
|
|
||||||
SonarrHistoryWrapper(SonarrHistoryWrapper),
|
|
||||||
SystemStatus(SystemStatus),
|
|
||||||
Tag(Tag),
|
|
||||||
Tags(Vec<Tag>),
|
|
||||||
Tasks(Vec<SonarrTask>),
|
|
||||||
Updates(Vec<Update>),
|
|
||||||
Value(Value),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SonarrSerdeable> for Serdeable {
|
impl From<SonarrSerdeable> for Serdeable {
|
||||||
fn from(value: SonarrSerdeable) -> Serdeable {
|
fn from(value: SonarrSerdeable) -> Serdeable {
|
||||||
Serdeable::Sonarr(value)
|
Serdeable::Sonarr(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<()> for SonarrSerdeable {
|
|
||||||
fn from(_: ()) -> Self {
|
|
||||||
SonarrSerdeable::Value(json!({}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serde_enum_from!(
|
serde_enum_from!(
|
||||||
SonarrSerdeable {
|
SonarrSerdeable {
|
||||||
AddSeriesSearchResults(Vec<AddSeriesSearchResult>),
|
AddSeriesSearchResults(Vec<AddSeriesSearchResult>),
|
||||||
|
|||||||
Reference in New Issue
Block a user