Skip to main content

amql_selector/
types.rs

1//! Domain newtypes used by the selector crate.
2
3use serde::{Deserialize, Serialize};
4use std::borrow::Borrow;
5use std::fmt;
6use std::ops::Deref;
7
8macro_rules! define_newtype_string {
9    ($name:ident, $doc:expr) => {
10        #[doc = $doc]
11        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12        #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
13        #[cfg_attr(feature = "ts", derive(ts_rs::TS))]
14        #[cfg_attr(feature = "flow", derive(flowjs_rs::Flow))]
15        #[cfg_attr(feature = "ts", ts(export))]
16        #[cfg_attr(feature = "flow", flow(export))]
17        #[serde(transparent)]
18        pub struct $name(String);
19
20        impl Deref for $name {
21            type Target = str;
22            #[inline]
23            fn deref(&self) -> &str {
24                &self.0
25            }
26        }
27
28        impl AsRef<str> for $name {
29            #[inline]
30            fn as_ref(&self) -> &str {
31                &self.0
32            }
33        }
34
35        impl Borrow<str> for $name {
36            #[inline]
37            fn borrow(&self) -> &str {
38                &self.0
39            }
40        }
41
42        impl fmt::Display for $name {
43            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44                f.write_str(&self.0)
45            }
46        }
47
48        impl From<String> for $name {
49            #[inline]
50            fn from(s: String) -> Self {
51                Self(s)
52            }
53        }
54
55        impl From<&str> for $name {
56            #[inline]
57            fn from(s: &str) -> Self {
58                Self(s.to_owned())
59            }
60        }
61
62        impl PartialEq<str> for $name {
63            #[inline]
64            fn eq(&self, other: &str) -> bool {
65                self.0 == other
66            }
67        }
68
69        impl PartialEq<&str> for $name {
70            #[inline]
71            fn eq(&self, other: &&str) -> bool {
72                self.0 == *other
73            }
74        }
75
76        impl PartialEq<String> for $name {
77            #[inline]
78            fn eq(&self, other: &String) -> bool {
79                self.0 == *other
80            }
81        }
82    };
83}
84
85define_newtype_string!(TagName, "Annotation or code element tag name.");
86define_newtype_string!(AttrName, "Attribute key in metadata maps.");