extern crate typed_codec; use quickcheck_macros::quickcheck; use typed_codec::*; struct Base64Codec(T); impl Encode for Base64Codec where T: AsRef<[u8]>, { type Target = T; type Output = String; fn encode(value: Self::Target) -> Self::Output { base64::encode(value) } } impl Decode for Base64Codec where T: AsRef<[u8]>, { type Target = T; type Output = Result>; fn decode(value: T) -> Result> { let bytes = base64::decode(value)?; String::from_utf8(bytes).map_err(Into::into) } } #[test] fn encode() { let actual = "foobarbaz12345".encode::>(); let expected = "Zm9vYmFyYmF6MTIzNDU=".to_owned(); assert_eq!(actual, expected); } #[test] fn decode() { let actual = "Zm9vYmFyYmF6MTIzNDU=".decode::>().unwrap(); let expected = "foobarbaz12345".to_owned(); assert_eq!(actual, expected); } #[quickcheck] fn equivalent_when_encode_and_then_decode(random_value: String) { let actual = random_value .encode::>() .decode::>() .unwrap(); assert_eq!(actual, random_value); }