aboutsummaryrefslogtreecommitdiffstats
path: root/src/deserialize.rs
blob: 0998cbdeade0e6b012ba2f7efc97e6a46b9b67d8 (plain) (blame)
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
// SPDX-License-Identifier: AGPL-3.0-only
use std::collections::HashMap;
use std::sync::Arc;

use serde::{Deserialize, Deserializer, de};
use serde_json::Value;

fn str_num<'de, D, T>(d: D) -> Result<T, D::Error> where D: Deserializer<'de>, T: From<u64> {
  struct Visitor;
  impl<'v> de::Visitor<'v> for Visitor {
    type Value = u64;
    fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
      write!(f, "a value which can be interpreted as a uint")
    }
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
      where E: de::Error
    {
      v.parse().map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a uint"))
    }
    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
      where E: de::Error
    {
      Ok(v)
    }
  }
  d.deserialize_any(Visitor).map(Into::into)
}
fn str_bool<'de, D, T>(d: D) -> Result<T, D::Error> where D: Deserializer<'de>, T: From<bool>{
  struct Visitor;
  impl<'v> de::Visitor<'v> for Visitor {
    type Value = bool;
    fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
      write!(f, "a value which can be interpreted as a uint")
    }
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
      where E: de::Error
    {
      match v.to_ascii_lowercase().as_str() {
        "true" | "1" => Ok(true),
        "false" | "0" => Ok(false),
        _ => Err(de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a bool"))
      }
    }
    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
      where E: de::Error
    {
      Ok(v)
    }
  }
  d.deserialize_any(Visitor).map(Into::into)
}


#[derive(Deserialize, Debug)]
pub struct TimeStamp {
  #[serde(alias = "unixtime")]
  #[serde(alias = "uts")]
  #[serde(deserialize_with = "str_num")]
  pub unix_timestamp: u64,
  #[serde(rename = "#text")]
  pub text: Value
}

#[derive(Deserialize, Debug)]
pub struct Artist {
  #[serde(alias = "#text")]
  pub name: Arc<str>,

  #[serde(default)]
  pub url: Option<Arc<str>>
}

#[derive(Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum ImageSize {
  Small,
  Medium,
  Large,
  ExtraLarge
}

#[derive(Deserialize, Debug)]
pub struct Image {
  pub size: ImageSize,
  #[serde(alias = "#text")]
  pub url: Arc<str>,
}

#[derive(Deserialize, Debug)]
pub struct Album {
  pub mbid: Option<Arc<str>>,
  #[serde(alias = "#text")]
  pub title: Arc<str>,
  #[serde(default)]
  #[serde(rename = "image")]
  pub images: Vec<Image>,

}


#[derive(Deserialize, Debug)]
pub struct Track {
  pub artist: Artist,
  #[serde(alias = "image")]
  #[serde(default)]
  pub images: Vec<Image>,
  pub mbid: Option<Arc<str>>,
  pub album: Option<Album>,
  pub name: Arc<str>,
  pub url: Arc<str>,
  #[serde(deserialize_with = "str_num")]
  pub duration: u64,
  #[serde(deserialize_with = "str_num")]
  pub listeners: u64,
  #[serde(deserialize_with = "str_num")]
  pub playcount: u64,

  #[serde(default)]
  #[serde(rename = "userloved")]
  #[serde(deserialize_with = "str_bool")]
  pub loved: Option<bool>,
  #[serde(deserialize_with = "str_num")]
  pub userplaycount: u64,
}

#[derive(Deserialize, Debug)]
pub struct GetTrackInfo {
  pub track: Track
}

#[derive(Default, Deserialize, Debug)]
pub struct TrackAttr {
  #[serde(default)]
  #[serde(deserialize_with = "str_bool")]
  pub nowplaying: bool,
  #[serde(flatten)]
  pub rest: HashMap<Arc<str>, Value>,
}
#[derive(Deserialize, Debug)]
pub struct ArtistStub {
  #[serde(alias = "#text")]
  pub name: Arc<str>
}
#[derive(Deserialize, Debug)]
pub struct TrackStub {
  pub name: Arc<str>,
  pub artist: ArtistStub,
  #[serde(default)]
  pub date: Option<TimeStamp>,
  #[serde(rename = "@attr")]
  #[serde(default)]
  pub attr: TrackAttr,
}
#[derive(Deserialize, Debug)]
pub struct RecentTracks {
  pub track: Vec<TrackStub>
}
#[derive(Deserialize, Debug)]
pub struct GetRecentTracks {
  pub recenttracks: RecentTracks
}

#[derive(Deserialize, Debug)]
pub struct User {
  pub name: Arc<str>,
  pub realname: Arc<str>,
  #[serde(deserialize_with = "str_num")]
  pub playcount: u64,
  #[serde(deserialize_with = "str_num")]
  pub artist_count: u64,
  #[serde(deserialize_with = "str_num")]
  pub playlists: u64,
  #[serde(deserialize_with = "str_num")]
  pub track_count: u64,
  #[serde(deserialize_with = "str_num")]
  pub album_count: u64,

  #[serde(rename = "image")]
  pub images: Vec<Image>,

  pub registered: TimeStamp,
  pub url: Arc<str>
}

#[derive(Deserialize, Debug)]
pub struct GetUserInfo {
  pub user: User
}