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
use serde::de::Deserialize;
use serde::ser::{self, Serialize};

use crate::UncasedStr;

impl<'a, 'de: 'a> Deserialize<'de> for &'a UncasedStr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de>
    {
        Ok(<&str>::deserialize(deserializer)?.into())
    }
}

impl<'a> Serialize for &'a UncasedStr {
    fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "with-serde-alloc")]
mod uncased_alloc {
    use super::*;
    use crate::Uncased;

    impl<'a, 'de> Deserialize<'de> for Uncased<'a> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where D: serde::Deserializer<'de>
        {
            Ok(alloc::string::String::deserialize(deserializer)?.into())
        }
    }

    impl<'a> Serialize for Uncased<'a> {
        fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(self.as_str())
        }
    }
}