AppFlowy/frontend/rust-lib/flowy-grid/src/macros.rs

48 lines
1.5 KiB
Rust
Raw Normal View History

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) => {
2022-03-15 03:07:18 +00:00
impl std::convert::From<&FieldMeta> for $target {
fn from(field_meta: &FieldMeta) -> $target {
match serde_json::from_str(&field_meta.type_option) {
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
}
}
2022-03-12 14:52:24 +00:00
impl std::convert::From<$target> for String {
2022-03-12 13:06:15 +00:00
fn from(field_description: $target) -> Self {
2022-03-12 14:52:24 +00:00
match serde_json::to_string(&field_description) {
Ok(s) => s,
2022-03-03 14:17:07 +00:00
Err(e) => {
tracing::error!("Field type data convert to AnyData fail, error: {:?}", e);
2022-03-12 14:52:24 +00:00
serde_json::to_string(&$target::default()).unwrap()
2022-03-03 14:17:07 +00:00
}
}
}
}
};
}