macros.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #[macro_export]
  2. macro_rules! impl_from_and_to_type_option {
  3. ($target: ident, $field_type:expr) => {
  4. impl_from_field_type_option!($target);
  5. impl_to_field_type_option!($target, $field_type);
  6. };
  7. }
  8. #[macro_export]
  9. macro_rules! impl_from_field_type_option {
  10. ($target: ident) => {
  11. impl std::convert::From<&FieldMeta> for $target {
  12. fn from(field_meta: &FieldMeta) -> $target {
  13. match serde_json::from_str(&field_meta.type_options) {
  14. Ok(obj) => obj,
  15. Err(err) => {
  16. tracing::error!("{} convert from any data failed, {:?}", stringify!($target), err);
  17. $target::default()
  18. }
  19. }
  20. }
  21. }
  22. };
  23. }
  24. #[macro_export]
  25. macro_rules! impl_to_field_type_option {
  26. ($target: ident, $field_type:expr) => {
  27. impl $target {
  28. pub fn field_type(&self) -> FieldType {
  29. $field_type
  30. }
  31. }
  32. impl std::convert::From<$target> for String {
  33. fn from(field_description: $target) -> Self {
  34. match serde_json::to_string(&field_description) {
  35. Ok(s) => s,
  36. Err(e) => {
  37. tracing::error!("Field type data convert to AnyData fail, error: {:?}", e);
  38. serde_json::to_string(&$target::default()).unwrap()
  39. }
  40. }
  41. }
  42. }
  43. };
  44. }