lighthouse/tests/ef_tests/src/yaml_decode.rs

59 lines
1.6 KiB
Rust
Raw Normal View History

2019-05-13 12:10:23 +00:00
use super::*;
2019-05-14 00:01:20 +00:00
use types::Fork;
2019-05-13 12:10:23 +00:00
2019-05-15 01:12:49 +00:00
mod utils;
pub use utils::*;
pub trait YamlDecode: Sized {
2019-05-14 00:01:20 +00:00
/// Decode an object from the test specification YAML.
2019-05-15 01:12:49 +00:00
fn yaml_decode(string: &String) -> Result<Self, Error>;
2019-05-13 12:10:23 +00:00
}
2019-05-14 00:01:20 +00:00
/// Basic types can general be decoded with the `parse` fn if they implement `str::FromStr`.
2019-05-13 12:10:23 +00:00
macro_rules! impl_via_parse {
($ty: ty) => {
2019-05-15 01:12:49 +00:00
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
2019-05-13 12:10:23 +00:00
string
.parse::<Self>()
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_parse!(u8);
impl_via_parse!(u16);
impl_via_parse!(u32);
impl_via_parse!(u64);
2019-05-14 05:08:42 +00:00
/// Some `ethereum-types` methods have a `str::FromStr` implementation that expects `0x`-prefixed:
2019-05-14 00:01:20 +00:00
/// hex, so we use `from_dec_str` instead.
2019-05-13 12:10:23 +00:00
macro_rules! impl_via_from_dec_str {
($ty: ty) => {
2019-05-15 01:12:49 +00:00
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
2019-05-13 12:10:23 +00:00
Self::from_dec_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_from_dec_str!(U128);
impl_via_from_dec_str!(U256);
2019-05-14 00:01:20 +00:00
/// Types that already implement `serde::Deserialize` can be decoded using `serde_yaml`.
macro_rules! impl_via_serde_yaml {
($ty: ty) => {
2019-05-15 01:12:49 +00:00
impl YamlDecode for $ty {
fn yaml_decode(string: &String) -> Result<Self, Error> {
2019-05-14 00:01:20 +00:00
serde_yaml::from_str(string)
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_serde_yaml!(Fork);