use super::{Codec, UseStorageOptions}; use std::str::FromStr; #[derive(Clone, Default, PartialEq)] pub struct StringCodec(); impl Codec for StringCodec { type Error = T::Err; fn encode(&self, val: &T) -> Result { Ok(val.to_string()) } fn decode(&self, str: String) -> Result { T::from_str(&str) } } impl UseStorageOptions { pub fn string_codec() -> Self { Self::new(StringCodec()) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_string_codec() { let s = String::from("party time 🎉"); let codec = StringCodec(); assert_eq!(codec.encode(&s), Ok(s.clone())); assert_eq!(codec.decode(s.clone()), Ok(s)); } }