rs_plugin_common_interfaces/domain/
image.rs1use std::fmt;
2use std::num::ParseIntError;
3use std::str::FromStr;
4
5use serde::{Deserialize, Serialize};
6use strum_macros::EnumIter;
7
8use crate::domain::external_images::ImageType;
9
10#[derive(Debug, Serialize, Deserialize, Clone, EnumIter, PartialEq)]
12#[serde(rename_all = "camelCase")]
13pub enum ImageSize {
14 Thumb,
15 Small,
16 Large,
17 Custom(u32),
18}
19
20impl fmt::Display for ImageSize {
21 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 match self {
23 ImageSize::Thumb => write!(f, "thumb"),
24 ImageSize::Small => write!(f, "small"),
25 ImageSize::Large => write!(f, "large"),
26 ImageSize::Custom(width) => write!(f, "{}", width),
27 }
28 }
29}
30
31impl FromStr for ImageSize {
32 type Err = ();
33
34 fn from_str(input: &str) -> std::result::Result<ImageSize, ()> {
35 let int_size: core::result::Result<u32, ParseIntError> = input.parse();
36 match int_size {
37 Ok(size) => Ok(ImageSize::Custom(size)),
38 Err(_) => match input {
39 "thumb" => Ok(ImageSize::Thumb),
40 "small" => Ok(ImageSize::Small),
41 "large" => Ok(ImageSize::Small),
43 _ => Err(()),
44 },
45 }
46 }
47}
48
49impl ImageSize {
50 pub fn to_size(&self) -> u32 {
52 match self {
53 ImageSize::Thumb => 258,
54 ImageSize::Small => 512,
55 ImageSize::Large => 1024,
56 ImageSize::Custom(width) => *width,
57 }
58 }
59
60 pub fn to_filename_element(&self) -> String {
61 format!(".{}", self)
62 }
63
64 pub fn optional_to_filename_element(optinal: &Option<Self>) -> String {
65 match optinal {
66 Some(kind) => kind.to_filename_element(),
67 None => "".to_string(),
68 }
69 }
70}
71
72#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
76pub struct ImageRequestOptions {
77 pub size: Option<ImageSize>,
78 #[serde(rename = "type")]
79 pub kind: Option<ImageType>,
80 #[serde(default)]
81 pub defaulting: bool,
82}