Skip to main content

rs_plugin_common_interfaces/domain/
external_images.rs

1use serde::{Deserialize, Serialize};
2use strum_macros::{Display, EnumString};
3
4use crate::RsRequest;
5use crate::lookup::RsLookupMatchType;
6
7#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Display, EnumString)]
8#[serde(rename_all = "camelCase")]
9pub enum ImageType {
10    Poster,
11    Background,
12    Still,
13    Card,
14    ClearLogo,
15    ClearArt,
16    Custom(String),
17}
18
19impl ImageType {
20    pub fn to_filename_element(&self) -> String {
21        format!(".{}", self)
22    }
23    pub fn optional_to_filename_element(optinal: &Option<Self>) -> String {
24        match optinal {
25            Some(kind) => kind.to_filename_element(),
26            None => "".to_string(),
27        }
28    }
29}
30
31#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
32#[serde(rename_all = "camelCase")]
33pub struct ExternalImage {
34    #[serde(rename = "type")]
35    pub kind: Option<ImageType>,
36    pub url: RsRequest,
37    pub aspect_ratio: Option<f64>,
38    pub height: Option<i64>,
39    pub lang: Option<String>,
40    pub vote_average: Option<f64>,
41    pub vote_count: Option<i64>,
42    pub width: Option<i64>,
43
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub match_type: Option<RsLookupMatchType>,
46}
47
48#[cfg(feature = "rusqlite")]
49pub mod external_images_rusqlite {
50    use rusqlite::{
51        types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef},
52        ToSql,
53    };
54    use std::str::FromStr;
55
56    use super::{ExternalImage, ImageType};
57
58    impl FromSql for ExternalImage {
59        fn column_result(value: ValueRef) -> FromSqlResult<Self> {
60            String::column_result(value).and_then(|as_string| {
61                let r = serde_json::from_str(&as_string).map_err(|_| FromSqlError::InvalidType);
62                r
63            })
64        }
65    }
66    impl ToSql for ExternalImage {
67        fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
68            let r = serde_json::to_string(self).map_err(|_| FromSqlError::InvalidType)?;
69            Ok(ToSqlOutput::from(r))
70        }
71    }
72
73    impl FromSql for ImageType {
74        fn column_result(value: ValueRef) -> FromSqlResult<Self> {
75            String::column_result(value).and_then(|as_string| {
76                ImageType::from_str(&as_string).map_err(|_| FromSqlError::InvalidType)
77            })
78        }
79    }
80
81    impl ToSql for ImageType {
82        fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
83            let l = (&self.clone()).to_string();
84            Ok(ToSqlOutput::from(l))
85        }
86    }
87}