Skip to main content

spoolframe_protocol/
artwork.rs

1//! Artwork over a resource stream ([`crate::streams::bi::RESOURCE`]).
2//!
3//! The client opens a bi stream, writes the kind byte and one
4//! [`crate::resource::ResourceRequest`] containing [`ArtworkRequest`].
5//! The server answers with [`crate::resource::ResourceResponse`] and exactly `length` raw
6//! bytes of the image. Each image is its own stream, so a slow one never
7//! holds up RPCs or media.
8
9use serde::{Deserialize, Serialize};
10use spoolframe_types::ArtworkId;
11
12use crate::messages::{Error, SessionId};
13
14/// The widths the server serves. A fixed set, not a free number: a free
15/// width would let one client fill the cache with every width there is.
16/// Clients pick the smallest that covers what they draw.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
18#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
19#[serde(rename_all = "snake_case")]
20pub enum ArtworkSize {
21    /// 160 px wide.
22    Thumb,
23    /// 320 px wide.
24    Small,
25    /// 640 px wide.
26    Medium,
27    /// 1280 px wide.
28    Large,
29    /// As the catalog has it.
30    Original,
31    /// A size this build does not know.
32    #[serde(other)]
33    Unknown,
34}
35
36impl ArtworkSize {
37    /// The width to scale down to; `None` for the original.
38    pub fn width(self) -> Option<u32> {
39        match self {
40            ArtworkSize::Thumb => Some(160),
41            ArtworkSize::Small => Some(320),
42            ArtworkSize::Medium => Some(640),
43            ArtworkSize::Large => Some(1280),
44            ArtworkSize::Original | ArtworkSize::Unknown => None,
45        }
46    }
47
48    pub fn as_str(self) -> &'static str {
49        match self {
50            ArtworkSize::Thumb => "thumb",
51            ArtworkSize::Small => "small",
52            ArtworkSize::Medium => "medium",
53            ArtworkSize::Large => "large",
54            ArtworkSize::Original => "original",
55            ArtworkSize::Unknown => "unknown",
56        }
57    }
58}
59
60/// A face found in a backdrop (experimental `smart-crop`). Each edge is a
61/// fraction of the image's width or height, from its left or top, so it
62/// holds at every [`ArtworkSize`].
63#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
64#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
65pub struct ArtworkFace {
66    pub left: f32,
67    pub right: f32,
68    /// A box runs from about the brow to the chin: hair is above it.
69    pub top: f32,
70    pub bottom: f32,
71    /// The eyes' height: the mean of the two.
72    pub eyes: f32,
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
76pub struct ArtworkRequest {
77    pub session: SessionId,
78    pub id: ArtworkId,
79    pub size: ArtworkSize,
80}
81
82/// What follows on the stream.
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub struct ArtworkReply {
85    /// e.g. `image/jpeg`.
86    pub content_type: String,
87    /// How many bytes of image follow this frame.
88    pub length: u64,
89    /// Stable for this `(id, size)`: artwork ids change when the image
90    /// does, so a client may keep its own copy under this key.
91    pub etag: String,
92    /// Experimental (`smart-crop`): the faces in a backdrop.
93    #[serde(default, skip_serializing_if = "Option::is_none")]
94    pub faces: Option<Vec<ArtworkFace>>,
95}
96
97#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
98#[serde(rename_all = "snake_case")]
99pub enum ArtworkResponse {
100    Ok(ArtworkReply),
101    Err(Error),
102}