2022-03-03 14:17:07 +00:00
|
|
|
#[macro_export]
|
2022-03-12 13:06:15 +00:00
|
|
|
macro_rules! impl_from_and_to_type_option {
|
2022-03-03 14:17:07 +00:00
|
|
|
($target: ident, $field_type:expr) => {
|
2022-03-12 13:06:15 +00:00
|
|
|
impl_from_field_type_option!($target);
|
|
|
|
impl_to_field_type_option!($target, $field_type);
|
2022-03-03 14:17:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2022-03-12 13:06:15 +00:00
|
|
|
macro_rules! impl_from_field_type_option {
|
2022-03-03 14:17:07 +00:00
|
|
|
($target: ident) => {
|
|
|
|
impl std::convert::From<&Field> for $target {
|
|
|
|
fn from(field: &Field) -> $target {
|
2022-03-12 13:06:15 +00:00
|
|
|
match $target::try_from(Bytes::from(field.type_options.value.clone())) {
|
2022-03-03 14:17:07 +00:00
|
|
|
Ok(obj) => obj,
|
|
|
|
Err(err) => {
|
|
|
|
tracing::error!("{} convert from any data failed, {:?}", stringify!($target), err);
|
|
|
|
$target::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2022-03-12 13:06:15 +00:00
|
|
|
macro_rules! impl_to_field_type_option {
|
2022-03-03 14:17:07 +00:00
|
|
|
($target: ident, $field_type:expr) => {
|
|
|
|
impl $target {
|
2022-03-12 13:06:15 +00:00
|
|
|
pub fn field_type(&self) -> FieldType {
|
2022-03-03 14:17:07 +00:00
|
|
|
$field_type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<$target> for AnyData {
|
2022-03-12 13:06:15 +00:00
|
|
|
fn from(field_description: $target) -> Self {
|
|
|
|
let field_type = field_description.field_type();
|
|
|
|
match field_description.try_into() {
|
2022-03-03 14:17:07 +00:00
|
|
|
Ok(bytes) => {
|
|
|
|
let bytes: Bytes = bytes;
|
2022-03-12 13:06:15 +00:00
|
|
|
AnyData::from_bytes(field_type, bytes)
|
2022-03-03 14:17:07 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Field type data convert to AnyData fail, error: {:?}", e);
|
|
|
|
// it's impossible to fail when unwrapping the default field type data
|
|
|
|
let default_bytes: Bytes = $target::default().try_into().unwrap();
|
2022-03-12 13:06:15 +00:00
|
|
|
AnyData::from_bytes(field_type, default_bytes)
|
2022-03-03 14:17:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|