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
53/// What a person did in the items they are found for.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
55#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
56#[serde(rename_all = "snake_case")]
57pub enum PersonRole {
58    Actor,
59    Director,
60}
61
62/// An actor or director a search found. Their items are listed by `id`.
63#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
64#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
65pub struct Person {
66    pub id: PersonId,
67    pub name: String,
68    pub role: PersonRole,
69    /// How many items in the library they are in, as the catalog counts
70    /// them; listing them may find a few more or fewer.
71    pub count: Option<u32>,
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::id::Namespace;
78
79    #[test]
80    fn scopes_and_kinds_are_snake_case_on_the_wire() {
81        assert_eq!(
82            serde_json::to_string(&SearchScope::Episodes).unwrap(),
83            r#""episodes""#
84        );
85        assert_eq!(
86            serde_json::to_string(&SearchKind::People).unwrap(),
87            r#""people""#
88        );
89    }
90
91    #[test]
92    fn music_holds_artists_albums_and_tracks() {
93        assert_eq!(
94            SearchScope::Music.kinds(),
95            [SearchKind::Artists, SearchKind::Albums, SearchKind::Tracks]
96        );
97    }
98
99    #[test]
100    fn everything_ends_with_episodes() {
101        assert_eq!(SearchScope::All.kinds().last(), Some(&SearchKind::Episodes));
102    }
103
104    #[test]
105    fn a_person_round_trips() {
106        let person = Person {
107            id: PersonId::new(Namespace::Plex, "actor-134469").unwrap(),
108            name: "Keanu Reeves".into(),
109            role: PersonRole::Actor,
110            count: Some(9),
111        };
112        let json = serde_json::to_value(&person).unwrap();
113        assert_eq!(json["id"], "plex:actor-134469");
114        assert_eq!(json["role"], "actor");
115        assert_eq!(serde_json::from_value::<Person>(json).unwrap(), person);
116    }
117}