2024-06-24 01:30:47 +00:00
|
|
|
use cuprate_epee_encoding::{epee_object, from_bytes, to_bytes};
|
2024-01-29 22:32:16 +00:00
|
|
|
|
|
|
|
pub struct Optional {
|
|
|
|
val: u8,
|
|
|
|
optional_val: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
epee_object!(
|
|
|
|
Optional,
|
|
|
|
val: u8,
|
2024-01-30 16:09:54 +00:00
|
|
|
optional_val: i32 = -4_i32,
|
2024-01-29 22:32:16 +00:00
|
|
|
);
|
|
|
|
pub struct NotOptional {
|
|
|
|
val: u8,
|
|
|
|
optional_val: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
epee_object!(
|
|
|
|
NotOptional,
|
|
|
|
val: u8,
|
|
|
|
optional_val: i32,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NotPresent {
|
|
|
|
val: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
epee_object!(
|
|
|
|
NotPresent,
|
|
|
|
val: u8,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn epee_default_does_not_encode() {
|
|
|
|
let val = Optional {
|
|
|
|
val: 1,
|
|
|
|
optional_val: -4,
|
|
|
|
};
|
2024-01-30 16:09:54 +00:00
|
|
|
let mut bytes = to_bytes(val).unwrap().freeze();
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
assert!(from_bytes::<NotOptional, _>(&mut bytes.clone()).is_err());
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
let val: Optional = from_bytes(&mut bytes).unwrap();
|
2024-01-29 22:32:16 +00:00
|
|
|
assert_eq!(val.optional_val, -4);
|
|
|
|
assert_eq!(val.val, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn epee_non_default_does_encode() {
|
|
|
|
let val = Optional {
|
|
|
|
val: 8,
|
|
|
|
optional_val: -3,
|
|
|
|
};
|
2024-01-30 16:09:54 +00:00
|
|
|
let mut bytes = to_bytes(val).unwrap().freeze();
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
assert!(from_bytes::<NotOptional, _>(&mut bytes.clone()).is_ok());
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
let val: Optional = from_bytes(&mut bytes).unwrap();
|
2024-01-29 22:32:16 +00:00
|
|
|
assert_eq!(val.optional_val, -3);
|
|
|
|
assert_eq!(val.val, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn epee_value_not_present_with_default() {
|
|
|
|
let val = NotPresent { val: 76 };
|
2024-01-30 16:09:54 +00:00
|
|
|
let mut bytes = to_bytes(val).unwrap().freeze();
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
assert!(from_bytes::<NotOptional, _>(&mut bytes.clone()).is_err());
|
2024-01-29 22:32:16 +00:00
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
let val: Optional = from_bytes(&mut bytes).unwrap();
|
2024-01-29 22:32:16 +00:00
|
|
|
assert_eq!(val.optional_val, -4);
|
|
|
|
assert_eq!(val.val, 76)
|
|
|
|
}
|