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
use serde::de::{Deserialize, Deserializer};
use std::result;

use query::Query;
use {Client, Result};

#[derive(Debug)]
pub struct Podcast {
    id: usize,
    url: String,
    title: String,
    description: String,
    cover_art: String,
    image_url: String,
    status: String,
    episodes: Vec<Episode>,
    error: Option<String>,
}

#[derive(Debug)]
pub struct Episode {
    id: usize,
    parent: usize,
    is_dir: bool,
    title: String,
    album: String,
    artist: String,
    year: usize,
    cover_art: String,
    size: usize,
    content_type: String,
    suffix: String,
    duration: usize,
    bitrate: usize,
    is_video: bool,
    created: String,
    artist_id: String,
    media_type: String,
    stream_id: String,
    channel_id: String,
    description: String,
    status: String,
    publish_date: String,
}

impl Podcast {
    /// Fetches the details of a single podcast and its episodes.
    pub fn get<U>(client: &Client, id: U) -> Result<Podcast>
    where
        U: Into<Option<usize>>,
    {
        let channel = client.get("getPodcasts", Query::with("id", id.into()))?;
        Ok(get_list_as!(channel, Podcast).remove(0))
    }
    /// Returns a list of all podcasts the server subscribes to and,
    /// optionally, their episodes.
    pub fn list<B, U>(client: &Client, include_episodes: B) -> Result<Vec<Podcast>>
    where
        B: Into<Option<bool>>,
        U: Into<Option<usize>>,
    {
        let channel = client.get(
            "getPodcasts",
            Query::with("includeEpisodes", include_episodes.into()),
        )?;
        Ok(get_list_as!(channel, Podcast))
    }
}

impl Episode {
    /// Returns a list of the newest episodes of podcasts the server subscribes
    /// to. Optionally takes a number of episodes to maximally return.
    pub fn newest<U>(client: &Client, count: U) -> Result<Vec<Episode>>
    where
        U: Into<Option<usize>>,
    {
        let episode = client.get("getNewestPodcasts", Query::with("count", count.into()))?;
        Ok(get_list_as!(episode, Episode))
    }
}

impl<'de> Deserialize<'de> for Podcast {
    fn deserialize<D>(de: D) -> result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct _Podcast {
            id: String,
            url: String,
            title: String,
            description: String,
            cover_art: String,
            image_url: String,
            status: String,
            #[serde(default)]
            episode: Vec<Episode>,
            #[serde(default)]
            error_message: String,
        }

        let raw = _Podcast::deserialize(de)?;

        Ok(Podcast {
            id: raw.id.parse().unwrap(),
            url: raw.url,
            title: raw.title,
            description: raw.description,
            cover_art: raw.cover_art,
            image_url: raw.image_url,
            status: raw.status,
            episodes: raw.episode,
            error: if raw.error_message.is_empty() {
                None
            } else {
                Some(raw.error_message)
            },
        })
    }
}

impl<'de> Deserialize<'de> for Episode {
    fn deserialize<D>(de: D) -> result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct _Episode {
            id: String,
            parent: String,
            is_dir: bool,
            title: String,
            album: String,
            artist: String,
            year: usize,
            cover_art: String,
            size: usize,
            content_type: String,
            suffix: String,
            duration: usize,
            bit_rate: usize,
            is_video: bool,
            created: String,
            artist_id: String,
            #[serde(rename = "type")]
            _type: String,
            stream_id: String,
            channel_id: String,
            description: String,
            status: String,
            publish_date: String,
        }

        let raw = _Episode::deserialize(de)?;

        Ok(Episode {
            id: raw.id.parse().unwrap(),
            parent: raw.parent.parse().unwrap(),
            is_dir: raw.is_dir,
            title: raw.title,
            album: raw.album,
            artist: raw.artist,
            year: raw.year,
            cover_art: raw.cover_art,
            size: raw.size,
            content_type: raw.content_type,
            suffix: raw.suffix,
            duration: raw.duration,
            bitrate: raw.bit_rate,
            is_video: raw.is_video,
            created: raw.created,
            artist_id: raw.artist_id,
            media_type: raw._type,
            stream_id: raw.stream_id,
            channel_id: raw.channel_id,
            description: raw.description,
            status: raw.status,
            publish_date: raw.publish_date,
        })
    }
}