Skip to main content

spoolframe_types/
search.rs

1//! What a search covers and how its results are grouped (P3-09), and the
2//! people it finds.
3
4use serde::{Deserialize, Serialize};
5
6use crate::id::PersonId;
7
8/// What a search covers: everything, or one kind of thing. Music is one
9/// scope, with artists, albums and tracks as groups inside it.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum SearchScope {
13    All,
14    Movies,
15    Shows,
16    Episodes,
17    Music,
18}
19
20impl SearchScope {
21    /// The groups a search in this scope returns, in the order they are
22    /// shown: episodes last.
23    pub fn kinds(self) -> &'static [SearchKind] {
24        use SearchKind::*;
25        match self {
26            SearchScope::All => &[Shows, Movies, People, Artists, Albums, Tracks, Episodes],
27            SearchScope::Movies => &[Movies],
28            SearchScope::Shows => &[Shows],
29            SearchScope::Episodes => &[Episodes],
30            SearchScope::Music => &[Artists, Albums, Tracks],
31        }
32    }
33}
34
35/// One group of search results. People, artists, albums and tracks have no
36/// scope of their own; a search for one kind ("See all") names it.
37///
38/// Ordered as the groups are shown.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
40#[serde(rename_all = "snake_case")]
41pub enum SearchKind {
42    Shows,
43    Movies,
44    People,
45    Artists,
46    Albums,
47    Tracks,
48    Episodes,
49}
50
51/// What a person did in the items they are found for.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
53#[serde(rename_all = "snake_case")]
54pub enum PersonRole {
55    Actor,
56    Director,
57}
58
59/// An actor or director a search found. Their items are listed by `id`.
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61pub struct Person {
62    pub id: PersonId,
63    pub name: String,
64    pub role: PersonRole,
65    /// How many items in the library they are in, as the catalog counts
66    /// them; listing them may find a few more or fewer.
67    pub count: Option<u32>,
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::id::Namespace;
74
75    #[test]
76    fn scopes_and_kinds_are_snake_case_on_the_wire() {
77        assert_eq!(
78            serde_json::to_string(&SearchScope::Episodes).unwrap(),
79            r#""episodes""#
80        );
81        assert_eq!(
82            serde_json::to_string(&SearchKind::People).unwrap(),
83            r#""people""#
84        );
85    }
86
87    #[test]
88    fn music_holds_artists_albums_and_tracks() {
89        assert_eq!(
90            SearchScope::Music.kinds(),
91            [SearchKind::Artists, SearchKind::Albums, SearchKind::Tracks]
92        );
93    }
94
95    #[test]
96    fn everything_ends_with_episodes() {
97        assert_eq!(SearchScope::All.kinds().last(), Some(&SearchKind::Episodes));
98    }
99
100    #[test]
101    fn a_person_round_trips() {
102        let person = Person {
103            id: PersonId::new(Namespace::Plex, "actor-134469").unwrap(),
104            name: "Keanu Reeves".into(),
105            role: PersonRole::Actor,
106            count: Some(9),
107        };
108        let json = serde_json::to_value(&person).unwrap();
109        assert_eq!(json["id"], "plex:actor-134469");
110        assert_eq!(json["role"], "actor");
111        assert_eq!(serde_json::from_value::<Person>(json).unwrap(), person);
112    }
113}