rust_anilist/models/
cover.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR>
3
4//! This module contains the `Cover` struct.
5
6use serde::{Deserialize, Serialize};
7
8use crate::models::Color;
9
10/// Represents the cover images of various sizes and the color of the cover.
11///
12/// The `Cover` struct contains URLs for the cover images in different sizes
13/// (extra large, large, and medium) and an optional color.
14///
15/// # Fields
16///
17/// * `extra_large` - The URL of the cover image in extra large size.
18/// * `large` - The URL of the cover image in large size.
19/// * `medium` - The URL of the cover image in medium size.
20/// * `color` - The color of the cover image.
21#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
22#[serde(rename_all(deserialize = "camelCase"))]
23pub struct Cover {
24    /// The URL of the cover image in extra large size.
25    pub extra_large: Option<String>,
26    /// The URL of the cover image in large size.
27    pub large: Option<String>,
28    /// The URL of the cover image in medium size.
29    pub medium: Option<String>,
30    /// The color of the cover image.
31    pub color: Option<Color>,
32}
33
34impl Cover {
35    /// Returns the URL of the largest version of the cover image.
36    pub fn largest(&self) -> Option<&str> {
37        if let Some(extra_large) = self.extra_large.as_deref() {
38            Some(extra_large)
39        } else if let Some(large) = self.large.as_deref() {
40            Some(large)
41        } else if let Some(medium) = self.medium.as_deref() {
42            Some(medium)
43        } else {
44            None
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_largest_with_extra_large() {
55        let cover = Cover {
56            extra_large: Some("https://example.com/extra_large.jpg".to_string()),
57            large: Some("https://example.com/large.jpg".to_string()),
58            medium: Some("https://example.com/medium.jpg".to_string()),
59            color: None,
60        };
61
62        assert_eq!(cover.largest(), Some("https://example.com/extra_large.jpg"));
63    }
64
65    #[test]
66    fn test_largest_with_large() {
67        let cover = Cover {
68            extra_large: None,
69            large: Some("https://example.com/large.jpg".to_string()),
70            medium: Some("https://example.com/medium.jpg".to_string()),
71            color: None,
72        };
73
74        assert_eq!(cover.largest(), Some("https://example.com/large.jpg"));
75    }
76
77    #[test]
78    fn test_largest_with_medium() {
79        let cover = Cover {
80            extra_large: None,
81            large: None,
82            medium: Some("https://example.com/medium.jpg".to_string()),
83            color: None,
84        };
85
86        assert_eq!(cover.largest(), Some("https://example.com/medium.jpg"));
87    }
88
89    #[test]
90    fn test_largest_with_none() {
91        let cover = Cover {
92            extra_large: None,
93            large: None,
94            medium: None,
95            color: None,
96        };
97
98        assert_eq!(cover.largest(), None);
99    }
100}