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

80 lines
2.4 KiB
Rust
Raw Normal View History

2022-03-25 12:55:56 +00:00
#[macro_export]
macro_rules! impl_into_box_type_option_builder {
($target: ident) => {
impl std::convert::Into<BoxTypeOptionBuilder> for $target {
fn into(self) -> Box<dyn TypeOptionBuilder> {
Box::new(self)
}
}
};
}
macro_rules! impl_from_json_str_and_from_bytes {
($target: ident,$type_option: ident) => {
impl $target {
pub fn from_json_str(s: &str) -> $target {
$target($type_option::from(s))
}
pub fn from_bytes(bytes: Bytes) -> $target {
let type_option = $type_option::try_from(bytes).unwrap_or($type_option::default());
$target(type_option)
}
}
};
}
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 {
2022-03-25 12:55:56 +00:00
$target::from(field_meta.type_option_json.as_str())
}
}
impl std::convert::From<&str> for $target {
fn from(type_option_str: &str) -> $target {
match serde_json::from_str(type_option_str) {
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
}
}
}
}
};
}