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#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61pub struct ArtworkRequest {
62    pub session: SessionId,
63    pub id: ArtworkId,
64    pub size: ArtworkSize,
65}
66
67/// What follows on the stream.
68#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
69pub struct ArtworkReply {
70    /// e.g. `image/jpeg`.
71    pub content_type: String,
72    /// How many bytes of image follow this frame.
73    pub length: u64,
74    /// Stable for this `(id, size)`: artwork ids change when the image
75    /// does, so a client may keep its own copy under this key.
76    pub etag: String,
77}
78
79#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
80#[serde(rename_all = "snake_case")]
81pub enum ArtworkResponse {
82    Ok(ArtworkReply),
83    Err(Error),
84}