spoolframe-types 0.17.0-beta.1

Spoolframe's client-facing types: the media model and its identifiers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Identifiers. On the wire each one is a namespaced string, for example
//! `plex:movie:12345`.
//!
//! Clients treat them as opaque: they compare them and send them back, and
//! never take them apart. The namespace is part of the ID so that `native:`
//! IDs (and, with federation, IDs from another server) can be added without
//! a format change. Only the server reads the parts, through the accessors.
//!
//! A paired server's item is in [`Namespace::Federated`]: its key
//! is the origin's server id, then the id the origin knows it by without
//! the kind, which the federated id carries already
//! (`fed:movie:mzxw6ytbojtg633c:plex:12`). The origin is the server id,
//! not its endpoint key, so a rotated key keeps every id.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Why a string is not a valid identifier.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum IdError {
    #[error("{0:?} is not a known ID namespace")]
    UnknownNamespace(String),
    #[error("{0:?} is not a known media kind")]
    UnknownKind(String),
    #[error("{0:?} has no key")]
    MissingKey(String),
    /// A federated id's origin is empty or has a `:` in it.
    #[error("{0:?} is not a server id to name an origin by")]
    InvalidOrigin(String),
}

/// Which catalog an ID belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Namespace {
    /// An item in the Plex library; the key is its Plex `ratingKey`.
    Plex,
    /// Something the server made itself rather than a catalog: a sheet of
    /// frame previews, an [`ArtworkId`] like `preview:<set>/<n>`.
    Preview,
    /// An image the server keeps or draws itself: a playlist's
    /// uploaded cover (`image:playlist/<hash>`), its mosaic, a built-in
    /// playlist's tile. Only ever an [`ArtworkId`].
    Image,
    /// A paired server's, as this server caches it: the key is the
    /// origin's server id and the id the origin knows it by
    /// (`fed:movie:<origin>:plex:12`, `fed:<origin>:plex:2`).
    Federated,
}

impl Namespace {
    fn as_str(self) -> &'static str {
        match self {
            Namespace::Plex => "plex",
            Namespace::Preview => "preview",
            Namespace::Image => "image",
            Namespace::Federated => "fed",
        }
    }
}

/// `origin` checked as a federated id's: not empty, and no `:` in it.
fn origin(origin: &str) -> Result<&str, IdError> {
    if origin.is_empty() || origin.contains(':') {
        return Err(IdError::InvalidOrigin(origin.to_owned()));
    }
    Ok(origin)
}

/// A federated key's origin and the rest, if it is one.
fn split_origin(key: &str) -> Option<(&str, &str)> {
    key.split_once(':')
        .filter(|(origin, rest)| !origin.is_empty() && !rest.is_empty())
}

impl FromStr for Namespace {
    type Err = IdError;

    fn from_str(s: &str) -> Result<Self, IdError> {
        match s {
            "plex" => Ok(Namespace::Plex),
            "preview" => Ok(Namespace::Preview),
            "image" => Ok(Namespace::Image),
            "fed" => Ok(Namespace::Federated),
            other => Err(IdError::UnknownNamespace(other.to_owned())),
        }
    }
}

/// What kind of item a [`MediaId`] names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MediaKind {
    Movie,
    Show,
    Season,
    Episode,
    Artist,
    Album,
    Track,
}

impl MediaKind {
    /// Its name in an id (`movie` in `plex:movie:12`).
    pub fn as_str(self) -> &'static str {
        match self {
            MediaKind::Movie => "movie",
            MediaKind::Show => "show",
            MediaKind::Season => "season",
            MediaKind::Episode => "episode",
            MediaKind::Artist => "artist",
            MediaKind::Album => "album",
            MediaKind::Track => "track",
        }
    }

    /// Whether an item of this kind plays: a movie, an episode or a
    /// track, never a show, a season, an artist or an album.
    pub fn plays(self) -> bool {
        matches!(
            self,
            MediaKind::Movie | MediaKind::Episode | MediaKind::Track
        )
    }
}

impl FromStr for MediaKind {
    type Err = IdError;

    fn from_str(s: &str) -> Result<Self, IdError> {
        Ok(match s {
            "movie" => MediaKind::Movie,
            "show" => MediaKind::Show,
            "season" => MediaKind::Season,
            "episode" => MediaKind::Episode,
            "artist" => MediaKind::Artist,
            "album" => MediaKind::Album,
            "track" => MediaKind::Track,
            other => return Err(IdError::UnknownKind(other.to_owned())),
        })
    }
}

/// A key must be non-empty; it may contain `:` (only the leading parts are
/// split off).
fn key(whole: &str, key: Option<&str>) -> Result<String, IdError> {
    match key {
        Some(k) if !k.is_empty() => Ok(k.to_owned()),
        _ => Err(IdError::MissingKey(whole.to_owned())),
    }
}

/// A movie, show, season, episode, artist, album or track:
/// `<namespace>:<kind>:<key>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct MediaId {
    namespace: Namespace,
    kind: MediaKind,
    key: String,
}

impl MediaId {
    /// Build an ID. The server does this; clients only receive them.
    pub fn new(
        namespace: Namespace,
        kind: MediaKind,
        key: impl Into<String>,
    ) -> Result<Self, IdError> {
        let key = key.into();
        if key.is_empty() {
            return Err(IdError::MissingKey(format!(
                "{}:{}:",
                namespace.as_str(),
                kind.as_str()
            )));
        }
        Ok(Self {
            namespace,
            kind,
            key,
        })
    }

    /// For the server: which catalog resolves this ID.
    pub fn namespace(&self) -> Namespace {
        self.namespace
    }

    /// For the server: what kind of item this is.
    pub fn kind(&self) -> MediaKind {
        self.kind
    }

    /// For the server: the catalog's own key for the item.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// `remote`, an item of the server whose server id is `origin`, as
    /// this server names it: `fed:<kind>:<origin>:<namespace>:<key>`.
    pub fn federated(origin_id: &str, remote: &MediaId) -> Result<Self, IdError> {
        let key = format!(
            "{}:{}:{}",
            origin(origin_id)?,
            remote.namespace.as_str(),
            remote.key
        );
        Self::new(Namespace::Federated, remote.kind, key)
    }

    /// For a federated id: the origin's server id, and the id the origin
    /// knows the item by. `None` for any other id, or one that does not
    /// read as one.
    pub fn remote(&self) -> Option<(&str, MediaId)> {
        if self.namespace != Namespace::Federated {
            return None;
        }
        let (origin, rest) = split_origin(&self.key)?;
        let (namespace, key) = rest.split_once(':')?;
        Some((
            origin,
            Self::new(namespace.parse().ok()?, self.kind, key).ok()?,
        ))
    }

    /// The server id of the paired server a federated id's item is on;
    /// `None` for this server's own, as [`MediaId::remote`] reads it.
    pub fn origin(&self) -> Option<&str> {
        self.remote().map(|(origin, _)| origin)
    }
}

impl fmt::Display for MediaId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}:{}:{}",
            self.namespace.as_str(),
            self.kind.as_str(),
            self.key
        )
    }
}

impl FromStr for MediaId {
    type Err = IdError;

    fn from_str(s: &str) -> Result<Self, IdError> {
        let mut parts = s.splitn(3, ':');
        let namespace = parts.next().unwrap_or_default().parse()?;
        let kind = parts.next().unwrap_or_default().parse()?;
        let key = key(s, parts.next())?;
        Ok(Self {
            namespace,
            kind,
            key,
        })
    }
}

/// Defines an ID made of a namespace and a key: `<namespace>:<key>`.
macro_rules! namespaced_id {
    ($(#[$doc:meta])* $name:ident) => {
        $(#[$doc])*
        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
        #[serde(try_from = "String", into = "String")]
        pub struct $name {
            namespace: Namespace,
            key: String,
        }

        impl $name {
            /// Build an ID. The server does this; clients only receive them.
            pub fn new(namespace: Namespace, key: impl Into<String>) -> Result<Self, IdError> {
                let key = key.into();
                if key.is_empty() {
                    return Err(IdError::MissingKey(format!("{}:", namespace.as_str())));
                }
                Ok(Self { namespace, key })
            }

            /// For the server: which catalog resolves this ID.
            pub fn namespace(&self) -> Namespace {
                self.namespace
            }

            /// For the server: the catalog's own key.
            pub fn key(&self) -> &str {
                &self.key
            }

            /// `remote`, of the server whose server id is `origin`, as
            /// this server names it: `fed:<origin>:<remote>`.
            pub fn federated(origin_id: &str, remote: &$name) -> Result<Self, IdError> {
                Self::new(
                    Namespace::Federated,
                    format!("{}:{remote}", origin(origin_id)?),
                )
            }

            /// For a federated id: the origin's server id, and the id the
            /// origin knows it by. `None` for any other id, or one that
            /// does not read as one.
            pub fn remote(&self) -> Option<(&str, $name)> {
                if self.namespace != Namespace::Federated {
                    return None;
                }
                let (origin, rest) = split_origin(&self.key)?;
                Some((origin, rest.parse().ok()?))
            }

            /// The server id of the paired server a federated id is
            /// on; `None` for this server's own, as `remote` reads it.
            pub fn origin(&self) -> Option<&str> {
                self.remote().map(|(origin, _)| origin)
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{}:{}", self.namespace.as_str(), self.key)
            }
        }

        impl FromStr for $name {
            type Err = IdError;

            fn from_str(s: &str) -> Result<Self, IdError> {
                let mut parts = s.splitn(2, ':');
                let namespace = parts.next().unwrap_or_default().parse()?;
                let key = key(s, parts.next())?;
                Ok(Self { namespace, key })
            }
        }

        impl TryFrom<String> for $name {
            type Error = IdError;

            fn try_from(s: String) -> Result<Self, IdError> {
                s.parse()
            }
        }

        impl From<$name> for String {
            fn from(id: $name) -> String {
                id.to_string()
            }
        }
    };
}

namespaced_id!(
    /// A library (a Plex library section today): `<namespace>:<key>`.
    LibraryId
);

namespaced_id!(
    /// A piece of artwork, fetched through the client protocol, never from
    /// Plex directly: `<namespace>:<key>`.
    ArtworkId
);

namespaced_id!(
    /// A person a search found, whose items can be listed:
    /// `<namespace>:<key>`.
    PersonId
);

impl TryFrom<String> for MediaId {
    type Error = IdError;

    fn try_from(s: String) -> Result<Self, IdError> {
        s.parse()
    }
}

impl From<MediaId> for String {
    fn from(id: MediaId) -> String {
        id.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_paired_servers_library_names_its_origin() {
        let own: LibraryId = "plex:1".parse().unwrap();
        let fed = LibraryId::federated("sa", &own).unwrap();
        assert_eq!(fed.to_string(), "fed:sa:plex:1");
        assert_eq!(fed.origin(), Some("sa"));
        assert_eq!(own.origin(), None);
    }

    #[test]
    fn a_paired_servers_item_names_its_origin_and_kind() {
        let own: MediaId = "plex:episode:7".parse().unwrap();
        let fed = MediaId::federated("sa", &own).unwrap();
        assert_eq!((fed.origin(), fed.kind()), (Some("sa"), MediaKind::Episode));
        assert_eq!(own.origin(), None);
    }

    #[test]
    fn a_media_id_prints_as_namespace_kind_key() {
        let id = MediaId::new(Namespace::Plex, MediaKind::Movie, "12345").unwrap();
        assert_eq!(id.to_string(), "plex:movie:12345");
    }

    #[test]
    fn a_media_id_survives_a_round_trip_through_its_string() {
        let id: MediaId = "plex:episode:98321".parse().unwrap();
        assert_eq!(id.to_string().parse::<MediaId>().unwrap(), id);
    }

    #[test]
    fn a_media_id_on_the_wire_is_a_plain_string() {
        let id = MediaId::new(Namespace::Plex, MediaKind::Track, "7").unwrap();
        assert_eq!(serde_json::to_string(&id).unwrap(), r#""plex:track:7""#);
    }

    #[test]
    fn a_key_may_contain_colons() {
        let id: MediaId = "plex:movie:a:b".parse().unwrap();
        assert_eq!(id.key(), "a:b");
    }

    #[test]
    fn an_unknown_namespace_is_rejected() {
        assert_eq!(
            "nope:movie:1".parse::<MediaId>(),
            Err(IdError::UnknownNamespace("nope".into()))
        );
    }

    #[test]
    fn an_unknown_kind_is_rejected() {
        assert_eq!(
            "plex:photo:1".parse::<MediaId>(),
            Err(IdError::UnknownKind("photo".into()))
        );
    }

    #[test]
    fn an_empty_key_is_rejected() {
        assert_eq!(
            "plex:movie:".parse::<MediaId>(),
            Err(IdError::MissingKey("plex:movie:".into()))
        );
        assert!(MediaId::new(Namespace::Plex, MediaKind::Movie, "").is_err());
    }

    #[test]
    fn a_missing_key_is_rejected() {
        assert!("plex:movie".parse::<MediaId>().is_err());
    }

    #[test]
    fn deserializing_a_bad_media_id_fails() {
        assert!(serde_json::from_str::<MediaId>(r#""nope:movie:1""#).is_err());
    }

    #[test]
    fn a_library_id_round_trips_through_its_string() {
        let id = LibraryId::new(Namespace::Plex, "3").unwrap();
        assert_eq!(id.to_string(), "plex:3");
        assert_eq!("plex:3".parse::<LibraryId>().unwrap(), id);
    }

    #[test]
    fn an_artwork_id_keeps_a_path_like_key() {
        let id: ArtworkId = "plex:/library/metadata/123/thumb/1699".parse().unwrap();
        assert_eq!(id.key(), "/library/metadata/123/thumb/1699");
    }

    #[test]
    fn a_preview_sheet_s_id_is_in_a_namespace_of_its_own() {
        let id: ArtworkId = "preview:0f3a/2".parse().unwrap();
        assert_eq!(id.namespace(), Namespace::Preview);
        assert_eq!(id.key(), "0f3a/2");
        assert_eq!(id.to_string(), "preview:0f3a/2");
    }

    #[test]
    fn a_stored_image_s_id_is_in_a_namespace_of_its_own() {
        let id: ArtworkId = "image:playlist/0f3a".parse().unwrap();
        assert_eq!(id.namespace(), Namespace::Image);
        assert_eq!(id.key(), "playlist/0f3a");
        assert_eq!(id.to_string(), "image:playlist/0f3a");
    }

    const ORIGIN: &str = "mzxw6ytbojtg633c";

    /// P8-02: a paired server's item names its origin and the origin's
    /// id, reads back with the parser every id reads with, and says what
    /// it is of.
    #[test]
    fn a_federated_id_round_trips_and_names_its_origin() {
        let remote: MediaId = "plex:episode:cabin:12".parse().unwrap();
        let id = MediaId::federated(ORIGIN, &remote).unwrap();
        assert_eq!(id.to_string(), "fed:episode:mzxw6ytbojtg633c:plex:cabin:12");
        assert_eq!(id.namespace(), Namespace::Federated);
        assert_eq!(id.kind(), MediaKind::Episode);
        let read: MediaId = id.to_string().parse().unwrap();
        assert_eq!(read, id);
        assert_eq!(read.remote(), Some((ORIGIN, remote)));
        assert_eq!(
            serde_json::from_str::<MediaId>(&serde_json::to_string(&id).unwrap()).unwrap(),
            id
        );

        let library = LibraryId::federated(ORIGIN, &"plex:2".parse().unwrap()).unwrap();
        assert_eq!(library.to_string(), "fed:mzxw6ytbojtg633c:plex:2");
        assert_eq!(
            library.to_string().parse::<LibraryId>().unwrap().remote(),
            Some((ORIGIN, "plex:2".parse().unwrap()))
        );
        let art: ArtworkId = "plex:/library/metadata/1/thumb/2".parse().unwrap();
        let ours = ArtworkId::federated(ORIGIN, &art).unwrap();
        assert_eq!(
            ours.to_string(),
            "fed:mzxw6ytbojtg633c:plex:/library/metadata/1/thumb/2"
        );
        assert_eq!(ours.remote(), Some((ORIGIN, art)));
    }

    /// The namespaces there were read and print as they did, and are not
    /// federated.
    #[test]
    fn the_namespaces_there_were_are_unchanged() {
        for text in [
            "plex:movie:12345",
            "plex:episode:cabin:12",
            "plex:movie:a:b",
        ] {
            let id: MediaId = text.parse().unwrap();
            assert_eq!(id.to_string(), text);
            assert_eq!(id.remote(), None);
        }
        for text in ["plex:3", "preview:0f3a/2", "image:playlist/0f3a"] {
            let id: ArtworkId = text.parse().unwrap();
            assert_eq!(id.to_string(), text);
            assert_eq!(id.remote(), None);
        }
    }

    #[test]
    fn a_federated_id_needs_an_origin_and_an_id_the_origin_reads() {
        let remote: MediaId = "plex:movie:1".parse().unwrap();
        assert_eq!(
            MediaId::federated("", &remote),
            Err(IdError::InvalidOrigin(String::new()))
        );
        assert!(MediaId::federated("a:b", &remote).is_err());
        // Read, but not as anything an origin has.
        for text in [
            "fed:movie:mzxw",
            "fed:movie:mzxw:nope:1",
            "fed:movie::plex:1",
        ] {
            let id: MediaId = text.parse().unwrap();
            assert_eq!(id.remote(), None, "{text}");
        }
    }

    #[test]
    fn an_artwork_id_without_a_key_is_rejected() {
        assert!("plex:".parse::<ArtworkId>().is_err());
        assert!("plex".parse::<ArtworkId>().is_err());
    }
}