rs_plugin_common_interfaces/domain/
external_images.rs1use serde::{Deserialize, Serialize};
2use strum_macros::{Display, EnumString};
3
4use crate::RsRequest;
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)
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#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
31#[serde(rename_all = "camelCase")]
32pub struct ExternalImage {
33 #[serde(rename = "type")]
34 pub kind: Option<ImageType>,
35 pub url: RsRequest,
36 pub aspect_ratio: Option<f64>,
37 pub height: Option<i64>,
38 pub lang: Option<String>,
39 pub vote_average: Option<f64>,
40 pub vote_count: Option<i64>,
41 pub width: Option<i64>,
42}
43
44#[cfg(feature = "rusqlite")]
45pub mod external_images_rusqlite {
46 use rusqlite::{
47 types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef},
48 ToSql,
49 };
50 use std::str::FromStr;
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 impl ToSql for ImageType {
78 fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
79 let l = (&self.clone()).to_string();
80 Ok(ToSqlOutput::from(l))
81 }
82 }
83}