use interfacer_http::{ define_from_content, define_to_content, mime::{self, Mime}, ContentInto, FromContent, FromContentError, ToContent, ToContentError, }; use std::str::FromStr; use std::string::ToString; define_from_content!(FromContentString); define_to_content!(StringToContent); impl FromContentString for T where T::Err: std::string::ToString, { fn _from_content(data: Vec, content_type: &Mime) -> Result { String::from_utf8_lossy(&data) .parse() .map_err(|err: ::Err| { (data, content_type.clone(), err.to_string()).into() }) } } impl StringToContent for T { fn _to_content(&self, _content_type: &Mime) -> Result, ToContentError> { Ok(self.to_string().into_bytes()) } } #[derive(Debug, Eq, PartialEq, FromContent, ToContent)] struct I32(i32); impl FromStr for I32 { type Err = ::Err; fn from_str(s: &str) -> Result { Ok(Self(s.parse()?)) } } impl ToString for I32 { fn to_string(&self) -> String { self.0.to_string() } } #[test] fn test_to_content() { let data = I32(1).to_content(&mime::TEXT_PLAIN).unwrap(); assert_eq!("1", String::from_utf8_lossy(&data).as_ref()); } #[test] fn test_from_content() { assert_eq!( I32(1), "1".to_owned() .into_bytes() .content_into(&mime::TEXT_PLAIN) .unwrap() ); }