Implement transparent translation for newtypes and enums

This commit is contained in:
2024-11-05 20:42:59 +01:00
parent 9b9f33c4f7
commit b6209baecb
3 changed files with 251 additions and 6 deletions

View File

@@ -403,4 +403,49 @@ mod tests {
])
);
}
#[test]
fn test_transparent_tag() {
#[Tag(translate = transparent)]
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyTag(String);
assert_eq!(
MyTag::into_raw_tag(MyTag("test".to_owned())),
RawTagValue::new("test".to_owned())
);
assert_eq!(
MyTag::from_raw_tag(RawTagValue::new("test".to_owned())).unwrap(),
MyTag("test".to_owned())
);
}
#[test]
fn test_enums() {
#[Tag(translate = transparent)]
#[derive(PartialEq, Debug)]
enum MyCoolioTag {
A,
#[tag(rename = "C")]
B,
}
assert_eq!(
MyCoolioTag::into_raw_tag(MyCoolioTag::A),
RawTagValue::new("A".to_owned())
);
assert_eq!(
MyCoolioTag::from_raw_tag(RawTagValue::new("A".to_owned())).unwrap(),
MyCoolioTag::A
);
assert_eq!(
MyCoolioTag::into_raw_tag(MyCoolioTag::B),
RawTagValue::new("C".to_owned())
);
assert_eq!(
MyCoolioTag::from_raw_tag(RawTagValue::new("C".to_owned())).unwrap(),
MyCoolioTag::B
);
}
}