swc_common/
serializer.rs

1#![allow(deprecated)]
2
3use serde::Deserialize;
4
5#[derive(Deserialize)]
6#[deprecated = "Not used by swc, and this will be removed with next breaking change"]
7pub struct Node<T> {
8    #[serde(default, rename = "type")]
9    pub ty: String,
10    #[serde(flatten)]
11    pub node: T,
12}
13
14#[derive(Deserialize)]
15pub struct Type {
16    #[serde(rename = "type")]
17    pub ty: String,
18}
19
20#[cfg(feature = "encoding-impl")]
21pub struct WithChar<T>(pub T);
22
23#[cfg(feature = "encoding-impl")]
24impl cbor4ii::core::enc::Encode for WithChar<&char> {
25    fn encode<W: cbor4ii::core::enc::Write>(
26        &self,
27        writer: &mut W,
28    ) -> Result<(), cbor4ii::core::enc::Error<W::Error>> {
29        u32::from(*self.0).encode(writer)
30    }
31}
32
33#[cfg(feature = "encoding-impl")]
34impl<'de> cbor4ii::core::dec::Decode<'de> for WithChar<char> {
35    fn decode<R: cbor4ii::core::dec::Read<'de>>(
36        reader: &mut R,
37    ) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {
38        let n = u32::decode(reader)?;
39        let value = char::from_u32(n).ok_or_else(|| cbor4ii::core::dec::Error::Mismatch {
40            name: &"WithChar",
41            found: 0,
42        })?;
43        Ok(WithChar(value))
44    }
45}
46
47#[cfg(feature = "encoding-impl")]
48pub struct ArrayOption<T>(pub T);
49
50#[cfg(feature = "encoding-impl")]
51impl<T: cbor4ii::core::enc::Encode> cbor4ii::core::enc::Encode for ArrayOption<&'_ Vec<Option<T>>> {
52    fn encode<W: cbor4ii::core::enc::Write>(
53        &self,
54        writer: &mut W,
55    ) -> Result<(), cbor4ii::core::enc::Error<W::Error>> {
56        <cbor4ii::core::types::Array<()>>::bounded(self.0.len(), writer)?;
57        for item in self.0.iter() {
58            cbor4ii::core::types::Maybe(item).encode(writer)?;
59        }
60        Ok(())
61    }
62}
63
64#[cfg(feature = "encoding-impl")]
65impl<'de, T: cbor4ii::core::dec::Decode<'de>> cbor4ii::core::dec::Decode<'de>
66    for ArrayOption<Vec<Option<T>>>
67{
68    fn decode<R: cbor4ii::core::dec::Read<'de>>(
69        reader: &mut R,
70    ) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {
71        let len = <cbor4ii::core::types::Array<()>>::len(reader)?;
72        let Some(len) = len else {
73            return Err(cbor4ii::core::error::DecodeError::RequireLength {
74                name: &"array-option",
75                found: len
76                    .map(cbor4ii::core::error::Len::new)
77                    .unwrap_or(cbor4ii::core::error::Len::Indefinite),
78            });
79        };
80        let mut q = Vec::with_capacity(std::cmp::min(len, 128));
81        for _ in 0..len {
82            q.push(<cbor4ii::core::types::Maybe<Option<T>>>::decode(reader)?.0);
83        }
84        Ok(ArrayOption(q))
85    }
86}