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