mod common; #[test] fn list_of_bytestrings() { #[derive(udigest::Digestable)] struct Post { title: String, content: String, #[udigest(as = Vec)] images: Vec>, } impl Post { fn digest_expected(&self) -> impl udigest::Digestable + '_ { udigest::inline_struct!({ title: &self.title, content: &self.content, images: self.images .clone() .into_iter() .map(udigest::Bytes) .collect::>(), }) } } let post = Post { title: "My first post!".into(), content: "This is the first post I've ever written!".into(), images: vec![b"some image".to_vec()], }; let expected = common::encode_to_vec(&post.digest_expected()); let actual = common::encode_to_vec(&post); assert_eq!(hex::encode(expected), hex::encode(actual)); } #[test] fn hash_map() { #[derive(udigest::Digestable)] struct Attributes( #[udigest(as = std::collections::BTreeMap<_, udigest::Bytes>)] std::collections::HashMap>, ); #[derive(udigest::Digestable)] struct EncodingExpected(std::collections::BTreeMap>>); impl Attributes { fn digest_expected(&self) -> impl udigest::Digestable + '_ { let encoding = self .0 .iter() .map(|(k, v)| (k.clone(), udigest::Bytes(v.clone()))) .collect::>(); EncodingExpected(encoding) } } let attrs = Attributes(FromIterator::from_iter([ ("some_attr".to_string(), b"value1".to_vec()), ("attr".to_string(), b"value2".to_vec()), ("some_other_attr".to_string(), b"value3".to_vec()), ])); let expected = common::encode_to_vec(&attrs.digest_expected()); let actual = common::encode_to_vec(&attrs); assert_eq!(hex::encode(expected), hex::encode(actual)); } #[test] fn option() { #[derive(udigest::Digestable)] struct Person { nickname: String, #[udigest(as = Option)] avatar: Option>, } impl Person { fn digest_expected(&self) -> impl udigest::Digestable + '_ { udigest::inline_struct!({ nickname: &self.nickname, avatar: self.avatar.as_ref().map(udigest::Bytes) }) } } let person = Person { nickname: "c00l_name".to_string(), avatar: Some(b"image".to_vec()), }; let expected = common::encode_to_vec(&person.digest_expected()); let actual = common::encode_to_vec(&person); assert_eq!(hex::encode(expected), hex::encode(actual)); } #[test] fn array() { #[derive(udigest::Digestable)] struct Foo { #[udigest(as = [udigest::Bytes; 5])] bar: [Vec; 5], } impl Foo { fn digest_expected(&self) -> impl udigest::Digestable + '_ { udigest::inline_struct!({ bar: self.bar.each_ref().map(udigest::Bytes), }) } } let foo = Foo { bar: [ b"abcd".to_vec(), b"aaaa".to_vec(), b"..-..---.-..-".to_vec(), b"bbbbbb".to_vec(), b"finally".to_vec(), ], }; let expected = common::encode_to_vec(&foo.digest_expected()); let actual = common::encode_to_vec(&foo); assert_eq!(hex::encode(expected), hex::encode(actual)); }