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#[serde(rename_all = "snake_case")]
19pub enum ArtworkSize {
20    /// 160 px wide.
21    Thumb,
22    /// 320 px wide.
23    Small,
24    /// 640 px wide.
25    Medium,
26    /// 1280 px wide.
27    Large,
28    /// As the catalog has it.
29    Original,
30    /// A size this build does not know.
31    #[serde(other)]
32    Unknown,
33}
34
35impl ArtworkSize {
36    /// The width to scale down to; `None` for the original.
37    pub fn width(self) -> Option<u32> {
38        match self {
39            ArtworkSize::Thumb => Some(160),
40            ArtworkSize::Small => Some(320),
41            ArtworkSize::Medium => Some(640),
42            ArtworkSize::Large => Some(1280),
43            ArtworkSize::Original | ArtworkSize::Unknown => None,
44        }
45    }
46
47    pub fn as_str(self) -> &'static str {
48        match self {
49            ArtworkSize::Thumb => "thumb",
50            ArtworkSize::Small => "small",
51            ArtworkSize::Medium => "medium",
52            ArtworkSize::Large => "large",
53            ArtworkSize::Original => "original",
54            ArtworkSize::Unknown => "unknown",
55        }
56    }
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60pub struct ArtworkRequest {
61    pub session: SessionId,
62    pub id: ArtworkId,
63    pub size: ArtworkSize,
64}
65
66/// What follows on the stream.
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct ArtworkReply {
69    /// e.g. `image/jpeg`.
70    pub content_type: String,
71    /// How many bytes of image follow this frame.
72    pub length: u64,
73    /// Stable for this `(id, size)`: artwork ids change when the image
74    /// does, so a client may keep its own copy under this key.
75    pub etag: String,
76}
77
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(rename_all = "snake_case")]
80pub enum ArtworkResponse {
81    Ok(ArtworkReply),
82    Err(Error),
83}