serde_encom/value/
ser.rs

1use super::Map;
2use super::{to_value, Value};
3use crate::error::{Error, ErrorCode, Result};
4use alloc::borrow::ToOwned;
5use alloc::string::{String, ToString};
6use alloc::vec::Vec;
7use core::fmt::Display;
8use core::result;
9use serde::ser::{Impossible, Serialize};
10
11impl Serialize for Value {
12    #[inline]
13    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
14    where
15        S: ::serde::Serializer,
16    {
17        match self {
18            Value::Null => serializer.serialize_unit(),
19            Value::Bool(b) => serializer.serialize_bool(*b),
20            Value::Number(n) => n.serialize(serializer),
21            Value::String(s) => serializer.serialize_str(s),
22            Value::Bytes(b) => serializer.serialize_bytes(b),
23            Value::Array(v) => v.serialize(serializer),
24            #[cfg(any(feature = "std", feature = "alloc"))]
25            Value::Object(m) => {
26                use serde::ser::SerializeMap;
27                let mut map = serializer.serialize_map(Some(m.len()))?;
28                for (k, v) in m {
29                    map.serialize_entry(k, v)?;
30                }
31                map.end()
32            }
33        }
34    }
35}
36
37/// Serializer whose output is a `Value`.
38///
39/// This is the serializer that backs [`serde_encom::to_value`][crate::to_value].
40/// Unlike the main serde_encom serializer which goes from some serializable
41/// value of type `T` to EnCom text, this one goes from `T` to
42/// `serde_encom::Value`.
43///
44/// The `to_value` function is implementable as:
45///
46/// ```
47/// use serde::Serialize;
48/// use serde_encom::{Error, Value};
49///
50/// pub fn to_value<T>(input: T) -> Result<Value, Error>
51/// where
52///     T: Serialize,
53/// {
54///     input.serialize(serde_encom::Serializer)
55/// }
56/// ```
57pub struct Serializer;
58
59impl serde::Serializer for Serializer {
60    type Ok = Value;
61    type Error = Error;
62
63    type SerializeSeq = SerializeVec;
64    type SerializeTuple = SerializeVec;
65    type SerializeTupleStruct = SerializeVec;
66    type SerializeTupleVariant = SerializeTupleVariant;
67    type SerializeMap = SerializeMap;
68    type SerializeStruct = SerializeMap;
69    type SerializeStructVariant = SerializeStructVariant;
70
71    #[inline]
72    fn serialize_bool(self, value: bool) -> Result<Value> {
73        Ok(Value::Bool(value))
74    }
75
76    #[inline]
77    fn serialize_i8(self, value: i8) -> Result<Value> {
78        self.serialize_i64(value as i64)
79    }
80
81    #[inline]
82    fn serialize_i16(self, value: i16) -> Result<Value> {
83        self.serialize_i64(value as i64)
84    }
85
86    #[inline]
87    fn serialize_i32(self, value: i32) -> Result<Value> {
88        self.serialize_i64(value as i64)
89    }
90
91    #[inline]
92    fn serialize_i64(self, value: i64) -> Result<Value> {
93        Ok(Value::Number(value.into()))
94    }
95
96    #[cfg(feature = "arbitrary_precision")]
97    fn serialize_i128(self, value: i128) -> Result<Value> {
98        Ok(Value::Number(value.into()))
99    }
100
101    #[inline]
102    fn serialize_u8(self, value: u8) -> Result<Value> {
103        self.serialize_u64(value as u64)
104    }
105
106    #[inline]
107    fn serialize_u16(self, value: u16) -> Result<Value> {
108        self.serialize_u64(value as u64)
109    }
110
111    #[inline]
112    fn serialize_u32(self, value: u32) -> Result<Value> {
113        self.serialize_u64(value as u64)
114    }
115
116    #[inline]
117    fn serialize_u64(self, value: u64) -> Result<Value> {
118        Ok(Value::Number(value.into()))
119    }
120
121    fn serialize_u128(self, value: u128) -> Result<Value> {
122        #[cfg(feature = "arbitrary_precision")]
123        {
124            Ok(Value::Number(value.into()))
125        }
126
127        #[cfg(not(feature = "arbitrary_precision"))]
128        {
129            if let Ok(value) = u64::try_from(value) {
130                Ok(Value::Number(value.into()))
131            } else {
132                Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0))
133            }
134        }
135    }
136
137    #[inline]
138    fn serialize_f32(self, float: f32) -> Result<Value> {
139        Ok(Value::from(float))
140    }
141
142    #[inline]
143    fn serialize_f64(self, float: f64) -> Result<Value> {
144        Ok(Value::from(float))
145    }
146
147    #[inline]
148    fn serialize_char(self, value: char) -> Result<Value> {
149        let mut s = String::new();
150        s.push(value);
151        Ok(Value::String(s))
152    }
153
154    #[inline]
155    fn serialize_str(self, value: &str) -> Result<Value> {
156        Ok(Value::String(value.to_owned()))
157    }
158
159    #[inline]
160    fn serialize_bytes(self, value: &[u8]) -> Result<Value> {
161        let vec = value.iter().map(|&b| Value::Number(b.into())).collect();
162        Ok(Value::Array(vec))
163    }
164
165    #[inline]
166    fn serialize_unit(self) -> Result<Value> {
167        Ok(Value::Null)
168    }
169
170    #[inline]
171    fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
172        self.serialize_unit()
173    }
174
175    #[inline]
176    fn serialize_unit_variant(
177        self,
178        _name: &'static str,
179        _variant_index: u32,
180        variant: &'static str,
181    ) -> Result<Value> {
182        self.serialize_str(variant)
183    }
184
185    #[inline]
186    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value>
187    where
188        T: ?Sized + Serialize,
189    {
190        value.serialize(self)
191    }
192
193    fn serialize_newtype_variant<T>(
194        self,
195        _name: &'static str,
196        _variant_index: u32,
197        variant: &'static str,
198        value: &T,
199    ) -> Result<Value>
200    where
201        T: ?Sized + Serialize,
202    {
203        let mut values = Map::new();
204        values.insert(String::from(variant), to_value(value)?);
205        Ok(Value::Object(values))
206    }
207
208    #[inline]
209    fn serialize_none(self) -> Result<Value> {
210        self.serialize_unit()
211    }
212
213    #[inline]
214    fn serialize_some<T>(self, value: &T) -> Result<Value>
215    where
216        T: ?Sized + Serialize,
217    {
218        value.serialize(self)
219    }
220
221    #[inline]
222    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
223        Ok(SerializeVec {
224            vec: Vec::with_capacity(len.unwrap_or(0)),
225        })
226    }
227
228    #[inline]
229    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
230        self.serialize_seq(Some(len))
231    }
232
233    #[inline]
234    fn serialize_tuple_struct(
235        self,
236        _name: &'static str,
237        len: usize,
238    ) -> Result<Self::SerializeTupleStruct> {
239        self.serialize_seq(Some(len))
240    }
241
242    #[inline]
243    fn serialize_tuple_variant(
244        self,
245        _name: &'static str,
246        _variant_index: u32,
247        variant: &'static str,
248        len: usize,
249    ) -> Result<Self::SerializeTupleVariant> {
250        Ok(SerializeTupleVariant {
251            name: String::from(variant),
252            vec: Vec::with_capacity(len),
253        })
254    }
255
256    #[inline]
257    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
258        Ok(SerializeMap::Map {
259            map: Map::new(),
260            next_key: None,
261        })
262    }
263
264    #[inline]
265    fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
266        match name {
267            #[cfg(feature = "arbitrary_precision")]
268            crate::number::TOKEN => Ok(SerializeMap::Number { out_value: None }),
269            #[cfg(feature = "raw_value")]
270            crate::raw::TOKEN => Ok(SerializeMap::RawValue { out_value: None }),
271            _ => self.serialize_map(Some(len)),
272        }
273    }
274
275    #[inline]
276    fn serialize_struct_variant(
277        self,
278        _name: &'static str,
279        _variant_index: u32,
280        variant: &'static str,
281        _len: usize,
282    ) -> Result<Self::SerializeStructVariant> {
283        Ok(SerializeStructVariant {
284            name: String::from(variant),
285            map: Map::new(),
286        })
287    }
288
289    #[inline]
290    fn collect_str<T>(self, value: &T) -> Result<Value>
291    where
292        T: ?Sized + Display,
293    {
294        Ok(Value::String(value.to_string()))
295    }
296}
297
298pub struct SerializeVec {
299    vec: Vec<Value>,
300}
301
302pub struct SerializeTupleVariant {
303    name: String,
304    vec: Vec<Value>,
305}
306
307pub enum SerializeMap {
308    Map {
309        map: Map<String, Value>,
310        next_key: Option<String>,
311    },
312    #[cfg(feature = "arbitrary_precision")]
313    Number { out_value: Option<Value> },
314    #[cfg(feature = "raw_value")]
315    RawValue { out_value: Option<Value> },
316}
317
318pub struct SerializeStructVariant {
319    name: String,
320    map: Map<String, Value>,
321}
322
323impl serde::ser::SerializeSeq for SerializeVec {
324    type Ok = Value;
325    type Error = Error;
326
327    #[inline]
328    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
329    where
330        T: ?Sized + Serialize,
331    {
332        self.vec.push(to_value(value)?);
333        Ok(())
334    }
335
336    #[inline]
337    fn end(self) -> Result<Value> {
338        Ok(Value::Array(self.vec))
339    }
340}
341
342impl serde::ser::SerializeTuple for SerializeVec {
343    type Ok = Value;
344    type Error = Error;
345
346    #[inline]
347    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
348    where
349        T: ?Sized + Serialize,
350    {
351        serde::ser::SerializeSeq::serialize_element(self, value)
352    }
353
354    #[inline]
355    fn end(self) -> Result<Value> {
356        serde::ser::SerializeSeq::end(self)
357    }
358}
359
360impl serde::ser::SerializeTupleStruct for SerializeVec {
361    type Ok = Value;
362    type Error = Error;
363
364    #[inline]
365    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
366    where
367        T: ?Sized + Serialize,
368    {
369        serde::ser::SerializeSeq::serialize_element(self, value)
370    }
371
372    #[inline]
373    fn end(self) -> Result<Value> {
374        serde::ser::SerializeSeq::end(self)
375    }
376}
377
378impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
379    type Ok = Value;
380    type Error = Error;
381
382    #[inline]
383    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
384    where
385        T: ?Sized + Serialize,
386    {
387        self.vec.push(to_value(value)?);
388        Ok(())
389    }
390
391    fn end(self) -> Result<Value> {
392        let mut object = Map::new();
393
394        object.insert(self.name, Value::Array(self.vec));
395
396        Ok(Value::Object(object))
397    }
398}
399
400impl serde::ser::SerializeMap for SerializeMap {
401    type Ok = Value;
402    type Error = Error;
403
404    #[inline]
405    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
406    where
407        T: ?Sized + Serialize,
408    {
409        match self {
410            SerializeMap::Map { next_key, .. } => {
411                *next_key = Some(key.serialize(MapKeySerializer)?);
412                Ok(())
413            }
414            #[cfg(feature = "arbitrary_precision")]
415            SerializeMap::Number { .. } => unreachable!(),
416            #[cfg(feature = "raw_value")]
417            SerializeMap::RawValue { .. } => unreachable!(),
418        }
419    }
420
421    #[inline]
422    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
423    where
424        T: ?Sized + Serialize,
425    {
426        match self {
427            SerializeMap::Map { map, next_key } => {
428                let key = next_key.take();
429                // Panic because this indicates a bug in the program rather than an
430                // expected failure.
431                let key = key.expect("serialize_value called before serialize_key");
432                map.insert(key, to_value(value)?);
433                Ok(())
434            }
435            #[cfg(feature = "arbitrary_precision")]
436            SerializeMap::Number { .. } => unreachable!(),
437            #[cfg(feature = "raw_value")]
438            SerializeMap::RawValue { .. } => unreachable!(),
439        }
440    }
441
442    #[inline]
443    fn end(self) -> Result<Value> {
444        match self {
445            SerializeMap::Map { map, .. } => Ok(Value::Object(map)),
446            #[cfg(feature = "arbitrary_precision")]
447            SerializeMap::Number { .. } => unreachable!(),
448            #[cfg(feature = "raw_value")]
449            SerializeMap::RawValue { .. } => unreachable!(),
450        }
451    }
452}
453
454struct MapKeySerializer;
455
456fn key_must_be_a_string() -> Error {
457    Error::syntax(ErrorCode::KeyMustBeAString, 0, 0)
458}
459
460fn float_key_must_be_finite() -> Error {
461    Error::syntax(ErrorCode::FloatKeyMustBeFinite, 0, 0)
462}
463
464impl serde::Serializer for MapKeySerializer {
465    type Ok = String;
466    type Error = Error;
467
468    type SerializeSeq = Impossible<String, Error>;
469    type SerializeTuple = Impossible<String, Error>;
470    type SerializeTupleStruct = Impossible<String, Error>;
471    type SerializeTupleVariant = Impossible<String, Error>;
472    type SerializeMap = Impossible<String, Error>;
473    type SerializeStruct = Impossible<String, Error>;
474    type SerializeStructVariant = Impossible<String, Error>;
475
476    #[inline]
477    fn serialize_unit_variant(
478        self,
479        _name: &'static str,
480        _variant_index: u32,
481        variant: &'static str,
482    ) -> Result<String> {
483        Ok(variant.to_owned())
484    }
485
486    #[inline]
487    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
488    where
489        T: ?Sized + Serialize,
490    {
491        value.serialize(self)
492    }
493
494    fn serialize_bool(self, value: bool) -> Result<String> {
495        Ok(value.to_string())
496    }
497
498    #[inline]
499    fn serialize_i8(self, value: i8) -> Result<String> {
500        Ok(value.to_string())
501    }
502
503    #[inline]
504    fn serialize_i16(self, value: i16) -> Result<String> {
505        Ok(value.to_string())
506    }
507
508    #[inline]
509    fn serialize_i32(self, value: i32) -> Result<String> {
510        Ok(value.to_string())
511    }
512
513    #[inline]
514    fn serialize_i64(self, value: i64) -> Result<String> {
515        Ok(value.to_string())
516    }
517
518    #[inline]
519    fn serialize_u8(self, value: u8) -> Result<String> {
520        Ok(value.to_string())
521    }
522
523    #[inline]
524    fn serialize_u16(self, value: u16) -> Result<String> {
525        Ok(value.to_string())
526    }
527
528    #[inline]
529    fn serialize_u32(self, value: u32) -> Result<String> {
530        Ok(value.to_string())
531    }
532
533    #[inline]
534    fn serialize_u64(self, value: u64) -> Result<String> {
535        Ok(value.to_string())
536    }
537
538    fn serialize_f32(self, value: f32) -> Result<String> {
539        if value.is_finite() {
540            Ok(ryu::Buffer::new().format_finite(value).to_owned())
541        } else {
542            Err(float_key_must_be_finite())
543        }
544    }
545
546    fn serialize_f64(self, value: f64) -> Result<String> {
547        if value.is_finite() {
548            Ok(ryu::Buffer::new().format_finite(value).to_owned())
549        } else {
550            Err(float_key_must_be_finite())
551        }
552    }
553
554    #[inline]
555    fn serialize_char(self, value: char) -> Result<String> {
556        Ok({
557            let mut s = String::new();
558            s.push(value);
559            s
560        })
561    }
562
563    #[inline]
564    fn serialize_str(self, value: &str) -> Result<String> {
565        Ok(value.to_owned())
566    }
567
568    fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
569        Err(key_must_be_a_string())
570    }
571
572    fn serialize_unit(self) -> Result<String> {
573        Err(key_must_be_a_string())
574    }
575
576    fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
577        Err(key_must_be_a_string())
578    }
579
580    fn serialize_newtype_variant<T>(
581        self,
582        _name: &'static str,
583        _variant_index: u32,
584        _variant: &'static str,
585        _value: &T,
586    ) -> Result<String>
587    where
588        T: ?Sized + Serialize,
589    {
590        Err(key_must_be_a_string())
591    }
592
593    fn serialize_none(self) -> Result<String> {
594        Err(key_must_be_a_string())
595    }
596
597    fn serialize_some<T>(self, _value: &T) -> Result<String>
598    where
599        T: ?Sized + Serialize,
600    {
601        Err(key_must_be_a_string())
602    }
603
604    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
605        Err(key_must_be_a_string())
606    }
607
608    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
609        Err(key_must_be_a_string())
610    }
611
612    fn serialize_tuple_struct(
613        self,
614        _name: &'static str,
615        _len: usize,
616    ) -> Result<Self::SerializeTupleStruct> {
617        Err(key_must_be_a_string())
618    }
619
620    fn serialize_tuple_variant(
621        self,
622        _name: &'static str,
623        _variant_index: u32,
624        _variant: &'static str,
625        _len: usize,
626    ) -> Result<Self::SerializeTupleVariant> {
627        Err(key_must_be_a_string())
628    }
629
630    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
631        Err(key_must_be_a_string())
632    }
633
634    fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
635        Err(key_must_be_a_string())
636    }
637
638    fn serialize_struct_variant(
639        self,
640        _name: &'static str,
641        _variant_index: u32,
642        _variant: &'static str,
643        _len: usize,
644    ) -> Result<Self::SerializeStructVariant> {
645        Err(key_must_be_a_string())
646    }
647
648    #[inline]
649    fn collect_str<T>(self, value: &T) -> Result<String>
650    where
651        T: ?Sized + Display,
652    {
653        Ok(value.to_string())
654    }
655}
656
657impl serde::ser::SerializeStruct for SerializeMap {
658    type Ok = Value;
659    type Error = Error;
660
661    #[inline]
662    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
663    where
664        T: ?Sized + Serialize,
665    {
666        match self {
667            SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value),
668            #[cfg(feature = "arbitrary_precision")]
669            SerializeMap::Number { out_value } => {
670                if key == crate::number::TOKEN {
671                    *out_value = Some(value.serialize(NumberValueEmitter)?);
672                    Ok(())
673                } else {
674                    Err(invalid_number())
675                }
676            }
677            #[cfg(feature = "raw_value")]
678            SerializeMap::RawValue { out_value } => {
679                if key == crate::raw::TOKEN {
680                    *out_value = Some(value.serialize(RawValueEmitter)?);
681                    Ok(())
682                } else {
683                    Err(invalid_raw_value())
684                }
685            }
686        }
687    }
688
689    #[inline]
690    fn end(self) -> Result<Value> {
691        match self {
692            SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self),
693            #[cfg(feature = "arbitrary_precision")]
694            SerializeMap::Number { out_value, .. } => {
695                Ok(out_value.expect("number value was not emitted"))
696            }
697            #[cfg(feature = "raw_value")]
698            SerializeMap::RawValue { out_value, .. } => {
699                Ok(out_value.expect("raw value was not emitted"))
700            }
701        }
702    }
703}
704
705impl serde::ser::SerializeStructVariant for SerializeStructVariant {
706    type Ok = Value;
707    type Error = Error;
708
709    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
710    where
711        T: ?Sized + Serialize,
712    {
713        self.map.insert(String::from(key), to_value(value)?);
714        Ok(())
715    }
716
717    fn end(self) -> Result<Value> {
718        let mut object = Map::new();
719
720        object.insert(self.name, Value::Object(self.map));
721
722        Ok(Value::Object(object))
723    }
724}
725
726#[cfg(feature = "arbitrary_precision")]
727struct NumberValueEmitter;
728
729#[cfg(feature = "arbitrary_precision")]
730fn invalid_number() -> Error {
731    Error::syntax(ErrorCode::InvalidNumber, 0, 0)
732}
733
734#[cfg(feature = "arbitrary_precision")]
735impl serde::ser::Serializer for NumberValueEmitter {
736    type Ok = Value;
737    type Error = Error;
738
739    type SerializeSeq = Impossible<Value, Error>;
740    type SerializeTuple = Impossible<Value, Error>;
741    type SerializeTupleStruct = Impossible<Value, Error>;
742    type SerializeTupleVariant = Impossible<Value, Error>;
743    type SerializeMap = Impossible<Value, Error>;
744    type SerializeStruct = Impossible<Value, Error>;
745    type SerializeStructVariant = Impossible<Value, Error>;
746
747    fn serialize_bool(self, _v: bool) -> Result<Value> {
748        Err(invalid_number())
749    }
750
751    fn serialize_i8(self, _v: i8) -> Result<Value> {
752        Err(invalid_number())
753    }
754
755    fn serialize_i16(self, _v: i16) -> Result<Value> {
756        Err(invalid_number())
757    }
758
759    fn serialize_i32(self, _v: i32) -> Result<Value> {
760        Err(invalid_number())
761    }
762
763    fn serialize_i64(self, _v: i64) -> Result<Value> {
764        Err(invalid_number())
765    }
766
767    fn serialize_u8(self, _v: u8) -> Result<Value> {
768        Err(invalid_number())
769    }
770
771    fn serialize_u16(self, _v: u16) -> Result<Value> {
772        Err(invalid_number())
773    }
774
775    fn serialize_u32(self, _v: u32) -> Result<Value> {
776        Err(invalid_number())
777    }
778
779    fn serialize_u64(self, _v: u64) -> Result<Value> {
780        Err(invalid_number())
781    }
782
783    fn serialize_f32(self, _v: f32) -> Result<Value> {
784        Err(invalid_number())
785    }
786
787    fn serialize_f64(self, _v: f64) -> Result<Value> {
788        Err(invalid_number())
789    }
790
791    fn serialize_char(self, _v: char) -> Result<Value> {
792        Err(invalid_number())
793    }
794
795    fn serialize_str(self, value: &str) -> Result<Value> {
796        let n = tri!(value.to_owned().parse());
797        Ok(Value::Number(n))
798    }
799
800    fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
801        Err(invalid_number())
802    }
803
804    fn serialize_none(self) -> Result<Value> {
805        Err(invalid_number())
806    }
807
808    fn serialize_some<T>(self, _value: &T) -> Result<Value>
809    where
810        T: ?Sized + Serialize,
811    {
812        Err(invalid_number())
813    }
814
815    fn serialize_unit(self) -> Result<Value> {
816        Err(invalid_number())
817    }
818
819    fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
820        Err(invalid_number())
821    }
822
823    fn serialize_unit_variant(
824        self,
825        _name: &'static str,
826        _variant_index: u32,
827        _variant: &'static str,
828    ) -> Result<Value> {
829        Err(invalid_number())
830    }
831
832    fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
833    where
834        T: ?Sized + Serialize,
835    {
836        Err(invalid_number())
837    }
838
839    fn serialize_newtype_variant<T>(
840        self,
841        _name: &'static str,
842        _variant_index: u32,
843        _variant: &'static str,
844        _value: &T,
845    ) -> Result<Value>
846    where
847        T: ?Sized + Serialize,
848    {
849        Err(invalid_number())
850    }
851
852    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
853        Err(invalid_number())
854    }
855
856    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
857        Err(invalid_number())
858    }
859
860    fn serialize_tuple_struct(
861        self,
862        _name: &'static str,
863        _len: usize,
864    ) -> Result<Self::SerializeTupleStruct> {
865        Err(invalid_number())
866    }
867
868    fn serialize_tuple_variant(
869        self,
870        _name: &'static str,
871        _variant_index: u32,
872        _variant: &'static str,
873        _len: usize,
874    ) -> Result<Self::SerializeTupleVariant> {
875        Err(invalid_number())
876    }
877
878    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
879        Err(invalid_number())
880    }
881
882    fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
883        Err(invalid_number())
884    }
885
886    fn serialize_struct_variant(
887        self,
888        _name: &'static str,
889        _variant_index: u32,
890        _variant: &'static str,
891        _len: usize,
892    ) -> Result<Self::SerializeStructVariant> {
893        Err(invalid_number())
894    }
895}
896
897#[cfg(feature = "raw_value")]
898struct RawValueEmitter;
899
900#[cfg(feature = "raw_value")]
901fn invalid_raw_value() -> Error {
902    Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
903}
904
905#[cfg(feature = "raw_value")]
906impl serde::ser::Serializer for RawValueEmitter {
907    type Ok = Value;
908    type Error = Error;
909
910    type SerializeSeq = Impossible<Value, Error>;
911    type SerializeTuple = Impossible<Value, Error>;
912    type SerializeTupleStruct = Impossible<Value, Error>;
913    type SerializeTupleVariant = Impossible<Value, Error>;
914    type SerializeMap = Impossible<Value, Error>;
915    type SerializeStruct = Impossible<Value, Error>;
916    type SerializeStructVariant = Impossible<Value, Error>;
917
918    fn serialize_bool(self, _v: bool) -> Result<Value> {
919        Err(invalid_raw_value())
920    }
921
922    fn serialize_i8(self, _v: i8) -> Result<Value> {
923        Err(invalid_raw_value())
924    }
925
926    fn serialize_i16(self, _v: i16) -> Result<Value> {
927        Err(invalid_raw_value())
928    }
929
930    fn serialize_i32(self, _v: i32) -> Result<Value> {
931        Err(invalid_raw_value())
932    }
933
934    fn serialize_i64(self, _v: i64) -> Result<Value> {
935        Err(invalid_raw_value())
936    }
937
938    fn serialize_u8(self, _v: u8) -> Result<Value> {
939        Err(invalid_raw_value())
940    }
941
942    fn serialize_u16(self, _v: u16) -> Result<Value> {
943        Err(invalid_raw_value())
944    }
945
946    fn serialize_u32(self, _v: u32) -> Result<Value> {
947        Err(invalid_raw_value())
948    }
949
950    fn serialize_u64(self, _v: u64) -> Result<Value> {
951        Err(invalid_raw_value())
952    }
953
954    fn serialize_f32(self, _v: f32) -> Result<Value> {
955        Err(invalid_raw_value())
956    }
957
958    fn serialize_f64(self, _v: f64) -> Result<Value> {
959        Err(invalid_raw_value())
960    }
961
962    fn serialize_char(self, _v: char) -> Result<Value> {
963        Err(invalid_raw_value())
964    }
965
966    fn serialize_str(self, value: &str) -> Result<Value> {
967        crate::from_str(value)
968    }
969
970    fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
971        Err(invalid_raw_value())
972    }
973
974    fn serialize_none(self) -> Result<Value> {
975        Err(invalid_raw_value())
976    }
977
978    fn serialize_some<T>(self, _value: &T) -> Result<Value>
979    where
980        T: ?Sized + Serialize,
981    {
982        Err(invalid_raw_value())
983    }
984
985    fn serialize_unit(self) -> Result<Value> {
986        Err(invalid_raw_value())
987    }
988
989    fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
990        Err(invalid_raw_value())
991    }
992
993    fn serialize_unit_variant(
994        self,
995        _name: &'static str,
996        _variant_index: u32,
997        _variant: &'static str,
998    ) -> Result<Value> {
999        Err(invalid_raw_value())
1000    }
1001
1002    fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
1003    where
1004        T: ?Sized + Serialize,
1005    {
1006        Err(invalid_raw_value())
1007    }
1008
1009    fn serialize_newtype_variant<T>(
1010        self,
1011        _name: &'static str,
1012        _variant_index: u32,
1013        _variant: &'static str,
1014        _value: &T,
1015    ) -> Result<Value>
1016    where
1017        T: ?Sized + Serialize,
1018    {
1019        Err(invalid_raw_value())
1020    }
1021
1022    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
1023        Err(invalid_raw_value())
1024    }
1025
1026    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
1027        Err(invalid_raw_value())
1028    }
1029
1030    fn serialize_tuple_struct(
1031        self,
1032        _name: &'static str,
1033        _len: usize,
1034    ) -> Result<Self::SerializeTupleStruct> {
1035        Err(invalid_raw_value())
1036    }
1037
1038    fn serialize_tuple_variant(
1039        self,
1040        _name: &'static str,
1041        _variant_index: u32,
1042        _variant: &'static str,
1043        _len: usize,
1044    ) -> Result<Self::SerializeTupleVariant> {
1045        Err(invalid_raw_value())
1046    }
1047
1048    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
1049        Err(invalid_raw_value())
1050    }
1051
1052    fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
1053        Err(invalid_raw_value())
1054    }
1055
1056    fn serialize_struct_variant(
1057        self,
1058        _name: &'static str,
1059        _variant_index: u32,
1060        _variant: &'static str,
1061        _len: usize,
1062    ) -> Result<Self::SerializeStructVariant> {
1063        Err(invalid_raw_value())
1064    }
1065
1066    fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
1067    where
1068        T: ?Sized + Display,
1069    {
1070        self.serialize_str(&value.to_string())
1071    }
1072}