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
pub(super) mod opt;
pub(super) mod vec;

use crate::err::Error;
use crate::sql::value::serde::ser;
use serde::ser::Impossible;
use serde::Serialize;

pub(super) struct Serializer;

impl ser::Serializer for Serializer {
	type Ok = String;
	type Error = Error;

	type SerializeSeq = Impossible<String, Error>;
	type SerializeTuple = Impossible<String, Error>;
	type SerializeTupleStruct = Impossible<String, Error>;
	type SerializeTupleVariant = Impossible<String, Error>;
	type SerializeMap = Impossible<String, Error>;
	type SerializeStruct = Impossible<String, Error>;
	type SerializeStructVariant = Impossible<String, Error>;

	const EXPECTED: &'static str = "a string";

	fn serialize_str(self, value: &str) -> Result<Self::Ok, Error> {
		Ok(value.to_owned())
	}

	#[inline]
	fn serialize_unit_variant(
		self,
		_name: &'static str,
		_variant_index: u32,
		variant: &'static str,
	) -> Result<Self::Ok, Error> {
		self.serialize_str(variant)
	}

	#[inline]
	fn serialize_newtype_struct<T>(
		self,
		_name: &'static str,
		value: &T,
	) -> Result<Self::Ok, Self::Error>
	where
		T: ?Sized + Serialize,
	{
		value.serialize(self.wrap())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use ser::Serializer as _;
	use serde::Serialize;
	use std::borrow::Cow;

	#[test]
	fn string() {
		let duration = "foo".to_owned();
		let serialized = duration.serialize(Serializer.wrap()).unwrap();
		assert_eq!(duration, serialized);
	}

	#[test]
	fn str() {
		let duration = "bar";
		let serialized = duration.serialize(Serializer.wrap()).unwrap();
		assert_eq!(duration, serialized);
	}

	#[test]
	fn cow() {
		let duration = Cow::from("bar");
		let serialized = duration.serialize(Serializer.wrap()).unwrap();
		assert_eq!(duration, serialized);
	}
}