macro_rules! string_newtype {
(
$(#[$meta:meta])*
$name:ident, $label:expr
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[repr(transparent)]
pub struct $name(String);
impl $name {
pub fn new(value: impl Into<String>) -> $crate::FoundationResult<Self> {
let s = value.into();
if s.is_empty() {
return Err($crate::FoundationError::EmptyIdentifier {
item: $label.to_string(),
});
}
Ok(Self(s))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl schemars::JsonSchema for $name {
fn schema_name() -> std::borrow::Cow<'static, str> {
stringify!($name).into()
}
fn json_schema(
gen: &mut schemars::SchemaGenerator,
) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
};
}
pub(crate) use string_newtype;