rs-plugin-common-interfaces 0.47.0

Common description for plugin creation
Documentation
use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;

use crate::domain::external_images::ImageType;

/// The size of an image the server gives: the width in pixels of a resized copy.
#[derive(Debug, Serialize, Deserialize, Clone, EnumIter, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ImageSize {
    Thumb,
    Small,
    Large,
    Custom(u32),
}

impl fmt::Display for ImageSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ImageSize::Thumb => write!(f, "thumb"),
            ImageSize::Small => write!(f, "small"),
            ImageSize::Large => write!(f, "large"),
            ImageSize::Custom(width) => write!(f, "{}", width),
        }
    }
}

impl FromStr for ImageSize {
    type Err = ();

    fn from_str(input: &str) -> std::result::Result<ImageSize, ()> {
        let int_size: core::result::Result<u32, ParseIntError> = input.parse();
        match int_size {
            Ok(size) => Ok(ImageSize::Custom(size)),
            Err(_) => match input {
                "thumb" => Ok(ImageSize::Thumb),
                "small" => Ok(ImageSize::Small),
                // As the server always read it.
                "large" => Ok(ImageSize::Small),
                _ => Err(()),
            },
        }
    }
}

impl ImageSize {
    /// The width in pixels.
    pub fn to_size(&self) -> u32 {
        match self {
            ImageSize::Thumb => 258,
            ImageSize::Small => 512,
            ImageSize::Large => 1024,
            ImageSize::Custom(width) => *width,
        }
    }

    pub fn to_filename_element(&self) -> String {
        format!(".{}", self)
    }

    pub fn optional_to_filename_element(optinal: &Option<Self>) -> String {
        match optinal {
            Some(kind) => kind.to_filename_element(),
            None => "".to_string(),
        }
    }
}

/// Query of an image request (`GET .../image`): the poster of a movie, the portrait
/// of a person, the still of an episode... With `defaulting`, the server gives another
/// image of the item when it lacks this one.
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
pub struct ImageRequestOptions {
    pub size: Option<ImageSize>,
    #[serde(rename = "type")]
    pub kind: Option<ImageType>,
    #[serde(default)]
    pub defaulting: bool,
}