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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*!
# CDTOC: Serde
*/

use crate::{
	Duration,
	Toc,
	Track,
	TrackPosition,
};
#[cfg(feature = "accuraterip")] use crate::AccurateRip;
#[cfg(feature = "cddb")] use crate::Cddb;
#[cfg(feature = "sha1")] use crate::ShaB64;
use serde::{
	de,
	Deserialize,
	ser::{
		self,
		SerializeStruct,
	},
	Serialize,
};
use std::fmt;



macro_rules! deserialize_str_with {
	($ty:ty, $fn:ident) => (
		#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
		impl<'de> Deserialize<'de> for $ty {
			fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
			where D: de::Deserializer<'de> {
				struct Visitor;

				impl<'de> de::Visitor<'de> for Visitor {
					type Value = $ty;

					fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
						f.write_str("string")
					}

					fn visit_str<S>(self, src: &str) -> Result<$ty, S>
					where S: de::Error {
						<$ty>::$fn(src).map_err(de::Error::custom)
					}

					fn visit_bytes<S>(self, src: &[u8]) -> Result<$ty, S>
					where S: de::Error {
						std::str::from_utf8(src)
							.map_err(de::Error::custom)
							.and_then(|s| <$ty>::$fn(s).map_err(de::Error::custom))
					}
				}

				deserializer.deserialize_str(Visitor)
			}
		}
	);
}

macro_rules! serialize_with {
	($ty:ty, $fn:ident) => (
		#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
		impl Serialize for $ty {
			fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
			where S: ser::Serializer { self.$fn().serialize(serializer) }
		}
	);
}



#[cfg(feature = "accuraterip")] deserialize_str_with!(AccurateRip, decode);
#[cfg(feature = "accuraterip")] serialize_with!(AccurateRip, pretty_print);

#[cfg(feature = "cddb")] deserialize_str_with!(Cddb, decode);
#[cfg(feature = "cddb")] serialize_with!(Cddb, to_string);

#[cfg(feature = "sha1")] deserialize_str_with!(ShaB64, decode);
#[cfg(feature = "sha1")] serialize_with!(ShaB64, pretty_print);

deserialize_str_with!(Toc, from_cdtoc);
serialize_with!(Toc, to_string);

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for Duration {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where D: de::Deserializer<'de> {
		u64::deserialize(deserializer).map(Self::from)
	}
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for Duration {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where S: ser::Serializer { self.0.serialize(serializer) }
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for Track {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where D: de::Deserializer<'de> {
		const FIELDS: &[&str] = &["num", "pos", "from", "to"];
		struct TrackVisitor;

		impl<'de> de::Visitor<'de> for TrackVisitor {
			type Value = Track;

			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
				formatter.write_str("struct Track")
			}

			fn visit_seq<V>(self, mut seq: V) -> Result<Track, V::Error>
            where V: de::SeqAccess<'de> {
				let num = seq.next_element()?
					.ok_or_else(|| de::Error::invalid_length(0, &self))?;
				let pos = seq.next_element()?
					.ok_or_else(|| de::Error::invalid_length(1, &self))?;
				let from = seq.next_element()?
					.ok_or_else(|| de::Error::invalid_length(2, &self))?;
				let to = seq.next_element()?
					.ok_or_else(|| de::Error::invalid_length(3, &self))?;
				Ok(Track { num, pos, from, to })
            }

			fn visit_map<V>(self, mut map: V) -> Result<Track, V::Error>
			where V: de::MapAccess<'de> {
				let mut num = None;
				let mut pos = None;
				let mut from = None;
				let mut to = None;

				macro_rules! set {
					($var:ident, $name:literal) => (
						if $var.is_none() { $var.replace(map.next_value()?); }
						else { return Err(de::Error::duplicate_field($name)); }
					);
				}

				while let Some(key) = map.next_key()? {
					match key {
						"num" => set!(num, "num"),
						"pos" => set!(pos, "pos"),
						"from" => set!(from, "from"),
						"to" => set!(to, "to"),
						_ => return Err(de::Error::unknown_field(key, FIELDS)),
					}
				}

				let num = num.ok_or_else(|| de::Error::missing_field("num"))?;
				let pos = pos.ok_or_else(|| de::Error::missing_field("pos"))?;
				let from = from.ok_or_else(|| de::Error::missing_field("from"))?;
				let to = to.ok_or_else(|| de::Error::missing_field("to"))?;

				Ok(Track { num, pos, from, to })
			}
		}

		deserializer.deserialize_struct("Track", FIELDS, TrackVisitor)
	}
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for Track {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where S: ser::Serializer {
		let mut state = serializer.serialize_struct("Track", 4)?;

		state.serialize_field("num", &self.num)?;
		state.serialize_field("pos", &self.pos)?;
		state.serialize_field("from", &self.from)?;
		state.serialize_field("to", &self.to)?;

		state.end()
	}
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for TrackPosition {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where D: de::Deserializer<'de> {
		struct Visitor;

		impl<'de> de::Visitor<'de> for Visitor {
			type Value = TrackPosition;

			fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
				f.write_str("string")
			}

			fn visit_str<S>(self, src: &str) -> Result<TrackPosition, S>
			where S: de::Error {
				Ok(match src {
					"First" => TrackPosition::First,
					"Middle" => TrackPosition::Middle,
					"Last" => TrackPosition::Last,
					"Only" => TrackPosition::Only,
					_ => TrackPosition::Invalid,
				})
			}

			fn visit_bytes<S>(self, src: &[u8]) -> Result<TrackPosition, S>
			where S: de::Error {
				Ok(match src {
					b"First" => TrackPosition::First,
					b"Middle" => TrackPosition::Middle,
					b"Last" => TrackPosition::Last,
					b"Only" => TrackPosition::Only,
					_ => TrackPosition::Invalid,
				})
			}
		}

		deserializer.deserialize_str(Visitor)
	}
}

serialize_with!(TrackPosition, as_str);



#[cfg(test)]
mod tests {
	use super::*;

	const TOC: &str = "B+96+5DEF+A0F2+F809+1529F+1ACB3+20CBC+24E14+2AF17+2F4EA+35BDD+3B96D";

	/// # Test Serialize->Deserialize Consistency.
	macro_rules! inout {
		($input:ident, $ty:ty, $nice:literal) => (
			let s = serde_json::to_vec(&$input).expect(concat!($nice, " serialize failed."));
			let d = serde_json::from_slice::<$ty>(&s).expect(concat!($nice, " deserialize failed."));
			assert_eq!($input, d, concat!($nice, " JSON serialize/deserialize does not match the original."));
		);
	}

	#[cfg(feature = "accuraterip")]
	#[test]
	fn serde_accuraterip() {
		let accuraterip = Toc::from_cdtoc(TOC).expect("Invalid TOC.").accuraterip_id();
		inout!(accuraterip, AccurateRip, "AccurateRip");
	}

	#[cfg(feature = "cddb")]
	#[test]
	fn serde_cddb() {
		let cddb = Toc::from_cdtoc(TOC).expect("Invalid TOC.").cddb_id();
		inout!(cddb, Cddb, "CDDB");
	}

	#[cfg(feature = "ctdb")]
	#[test]
	fn serde_ctdb() {
		let ctdb = Toc::from_cdtoc(TOC).expect("Invalid TOC.").ctdb_id();
		inout!(ctdb, ShaB64, "ShaB64");
	}

	#[cfg(feature = "musicbrainz")]
	#[test]
	fn serde_musicbrainz() {
		let mb = Toc::from_cdtoc(TOC).expect("Invalid TOC.").musicbrainz_id();
		inout!(mb, ShaB64, "ShaB64");
	}

	#[test]
	fn serde_duration() {
		let duration = Duration::from(123_u32);
		inout!(duration, Duration, "Duration");
	}

	#[test]
	fn serde_toc() {
		let toc = Toc::from_cdtoc(TOC).expect("Invalid TOC.");
		inout!(toc, Toc, "TOC");
	}

	#[test]
	fn serde_tracks() {
		let toc = Toc::from_cdtoc(TOC).expect("Invalid TOC.");
		let tracks: Vec<Track> = toc.audio_tracks().collect();
		inout!(tracks, Vec<Track>, "Track");

		// Make sure HTOA tracks work out okay.
		let toc = Toc::from_cdtoc("15+247E+2BEC+4AF4+7368+9704+B794+E271+110D0+12B7A+145C1+16CAF+195CF+1B40F+1F04A+21380+2362D+2589D+2793D+2A760+2DA32+300E1+32B46")
			.expect("Mummies TOC failed.");
		let htoa = toc.htoa().expect("Mummies HTOA failed.");
		inout!(htoa, Track, "HTOA");
	}
}