From 66bc9ba510c4defee1364d633dd1726b2255ebd5 Mon Sep 17 00:00:00 2001 From: AndrielFR Date: Sat, 11 Jan 2025 19:42:46 -0300 Subject: [PATCH] feat: import forgotten `Media` --- src/models/media.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/models/media.rs diff --git a/src/models/media.rs b/src/models/media.rs new file mode 100644 index 0000000..cfa2119 --- /dev/null +++ b/src/models/media.rs @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT↴ +// Copyright (c) 2022-2025 Andriel Ferreira ↴ + +//! This module contains the `Media` enum. + +use serde::{Deserialize, Serialize}; + +use super::{Anime, Format, Manga}; + +/// Represents different types of media. +#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)] +pub enum Media { + /// Represents an anime media type. + Anime(Anime), + /// Represents a manga media type. + Manga(Manga), + /// Represents an unknown media type. + /// + /// This variant is used when the media type is unknown or unsupported. + #[default] + Unknown, +} + +impl Media { + /// Returns the id of the media. + pub fn id(&self) -> i64 { + match self { + Media::Anime(anime) => anime.id, + Media::Manga(manga) => manga.id, + Media::Unknown => 0, + } + } + + /// Returns the title of the media. + pub fn title(&self) -> &str { + match self { + Media::Anime(anime) => anime.title.romaji(), + Media::Manga(manga) => manga.title.romaji(), + Media::Unknown => "Unknown", + } + } + + /// Returns the format of the media. + pub fn format(&self) -> Option<&Format> { + match self { + Media::Anime(anime) => Some(&anime.format), + Media::Manga(manga) => Some(&manga.format), + Media::Unknown => None, + } + } +} + +impl From for Media { + fn from(anime: Anime) -> Self { + Media::Anime(anime) + } +} + +impl From for Media { + fn from(manga: Manga) -> Self { + Media::Manga(manga) + } +}