rs_plugin_common_interfaces/domain/
external_images.rs

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