rust_jsr_registry/
private_tools.rs

1#[macro_export]
2macro_rules! priv_impl_default {
3    ($name:ident) => {
4        impl std::default::Default for $name {
5            fn default() -> Self {
6                return $name::new();
7            }
8        }
9    };
10}
11#[macro_export]
12macro_rules! priv_impl_getinfo {
13    ($name:ident) => {
14        impl crate::info::GetInfo for $name {
15            fn get_info(&self) -> crate::info::Info {
16                return crate::info::Info {
17                    scope: (*self.scope).to_string(),
18                    name: (*self.name).to_string(),
19                };
20            }
21        }
22    };
23}
24#[macro_export]
25macro_rules! priv_set_info {
26    () => {
27        /// Set package name
28        pub fn set_name(mut self, value: impl Into<String>) -> Self {
29            self.name = value.into();
30            self
31        }
32        /// Set package scope
33        pub fn set_scope(mut self, value: impl Into<String>) -> Self {
34            self.scope = value.into();
35            self
36        }
37    };
38}
39#[macro_export]
40macro_rules! priv_from_info {
41    // Version with no arguments
42    () => {
43        crate::priv_set_info!();
44        /// Set `scope` and `name` from struct that extends [crate::info::GetInfo] trait
45        pub fn from_info<T: AsRef<U>, U: crate::info::GetInfo>(info: T) -> Self
46        {
47            let res = info.as_ref().get_info();
48            Self {
49                scope: res.scope,
50                name: res.name,
51            }
52        }
53    };
54
55    // Version with arguments
56    ($( $name:ident: $value:expr ),*) => {
57        crate::priv_set_info!();
58        /// Set `scope` and `name` from struct that extends [crate::info::GetInfo] trait
59        pub fn from_info<T: AsRef<U>, U: crate::info::GetInfo>(info: T) -> Self
60        {
61            let res = info.as_ref().get_info();
62            Self {
63                $( $name: $value ),*,
64                scope: res.scope,
65                name: res.name
66            }
67        }
68    };
69}
70#[macro_export]
71macro_rules! priv_default_urls {
72    ( $( $name:ident = $url:literal ),* ) => {
73        $(
74            pub static $name: Lazy<Url> = Lazy::new(|| {
75                return Url::parse($url).expect(concat!("Failed to parse default url (", stringify!($name), ") for rust-jsr-registry"))
76            });
77        )*
78    };
79}
80#[macro_export]
81macro_rules! priv_as_ref {
82    ($name:ident) => {
83        impl AsRef<$name> for $name {
84            fn as_ref(&self) -> &$name {
85                return self;
86            }
87        }
88    };
89}
90#[macro_export]
91macro_rules! priv_enum_derived {
92    ($name:ident, $( $field_name:ident ),*) => {
93        #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
94        #[serde(rename_all = "lowercase")]
95        pub enum $name {
96            $( $field_name ),*
97        }
98    };
99}