use crate::{Decoder, Encoder};
pub struct OptionCodec<C>(C);
impl<T, E> Encoder<Option<T>> for OptionCodec<E>
where
E: Encoder<T, Encoded = String>,
{
type Error = E::Error;
type Encoded = String;
fn encode(val: &Option<T>) -> Result<String, Self::Error> {
match val {
Some(val) => Ok(format!("~<|Some|>~{}", E::encode(val)?)),
None => Ok("~<|None|>~".to_owned()),
}
}
}
impl<T, D> Decoder<Option<T>> for OptionCodec<D>
where
D: Decoder<T, Encoded = str>,
{
type Error = D::Error;
type Encoded = str;
fn decode(str: &Self::Encoded) -> Result<Option<T>, Self::Error> {
str.strip_prefix("~<|Some|>~")
.map(|v| D::decode(v))
.transpose()
}
}