rust_anilist/models/
media.rs

1// SPDX-License-Identifier: MIT↴
2// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR>↴
3
4//! This module contains the `Media` enum.
5
6use serde::{Deserialize, Serialize};
7
8use super::{Anime, Format, Manga};
9
10/// Represents different types of media.
11#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
12pub enum Media {
13    /// Represents an anime media type.
14    Anime(Anime),
15    /// Represents a manga media type.
16    Manga(Manga),
17    /// Represents an unknown media type.
18    ///
19    /// This variant is used when the media type is unknown or unsupported.
20    #[default]
21    Unknown,
22}
23
24impl Media {
25    /// Returns the id of the media.
26    pub fn id(&self) -> i64 {
27        match self {
28            Media::Anime(anime) => anime.id,
29            Media::Manga(manga) => manga.id,
30            Media::Unknown => 0,
31        }
32    }
33
34    /// Returns the title of the media.
35    pub fn title(&self) -> &str {
36        match self {
37            Media::Anime(anime) => anime.title.romaji(),
38            Media::Manga(manga) => manga.title.romaji(),
39            Media::Unknown => "Unknown",
40        }
41    }
42
43    /// Returns the format of the media.
44    pub fn format(&self) -> Option<&Format> {
45        match self {
46            Media::Anime(anime) => Some(&anime.format),
47            Media::Manga(manga) => Some(&manga.format),
48            Media::Unknown => None,
49        }
50    }
51}
52
53impl From<Anime> for Media {
54    fn from(anime: Anime) -> Self {
55        Media::Anime(anime)
56    }
57}
58
59impl From<Manga> for Media {
60    fn from(manga: Manga) -> Self {
61        Media::Manga(manga)
62    }
63}