notionrs_schema/macro/
impl_from_as_ref.rs

1/// This macro is used to generate a setter method for a struct field.
2///
3/// ```ignore
4/// crate::impl_from_as_ref!(BookmarkBlock, url);
5/// ```
6/// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
7/// ```ignore
8/// impl<T> From<T> for BookmarkBlock
9/// where
10///     T: AsRef<str>,
11/// {
12///     fn from(url: T) -> Self {
13///         Self::default().url(url)
14///     }
15/// }
16/// ```
17#[macro_export]
18macro_rules! impl_from_as_ref {
19    ($struct_name:ident, $field:ident) => {
20        impl<T> From<T> for $struct_name
21        where
22            T: AsRef<str>,
23        {
24            fn from(value: T) -> Self {
25                let mut instance = Self::default();
26                instance.$field = value.as_ref().to_string();
27                instance
28            }
29        }
30    };
31}