1use crate::ser::part::{PartSerializer, Sink};
2use crate::ser::Error;
3use form_urlencoded::Serializer as UrlEncodedSerializer;
4use form_urlencoded::Target as UrlEncodedTarget;
5use serde::ser::Serialize;
6use std::str;
7
8pub struct ValueSink<'input, 'key, 'target, Target>
9where
10 Target: UrlEncodedTarget,
11{
12 urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
13 key: &'key str,
14}
15
16impl<'input, 'key, 'target, Target> ValueSink<'input, 'key, 'target, Target>
17where
18 Target: 'target + UrlEncodedTarget,
19{
20 pub fn new(
21 urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
22 key: &'key str,
23 ) -> Self {
24 ValueSink { urlencoder, key }
25 }
26}
27
28impl<'input, 'key, 'target, Target> Sink
29 for ValueSink<'input, 'key, 'target, Target>
30where
31 Target: 'target + UrlEncodedTarget,
32{
33 type Ok = ();
34
35 fn serialize_str(self, value: &str) -> Result<(), Error> {
36 self.urlencoder.append_pair(self.key, value);
37 Ok(())
38 }
39
40 fn serialize_static_str(self, value: &'static str) -> Result<(), Error> {
41 self.serialize_str(value)
42 }
43
44 fn serialize_string(self, value: String) -> Result<(), Error> {
45 self.serialize_str(&value)
46 }
47
48 fn serialize_none(self) -> Result<Self::Ok, Error> {
49 Ok(())
50 }
51
52 fn serialize_some<T: ?Sized + Serialize>(
53 self,
54 value: &T,
55 ) -> Result<Self::Ok, Error> {
56 value.serialize(PartSerializer::new(self))
57 }
58
59 fn unsupported(self) -> Error {
60 Error::Custom("unsupported value".into())
61 }
62}
63