Skip to main content

spoolframe_types/
search.rs

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