use serde::{Serialize, Serializer, Deserialize, Deserializer};
use crate::SeString;

impl Serialize for SeString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        self.encode().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for SeString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        let bytes = Vec::<u8>::deserialize(deserializer)?;
        SeString::parse(&bytes).map_err(|e| serde::de::Error::custom(format!("invalid sestring: {:?}", e)))
    }
}
