From 2e71e403789217afb05d40a4b7284865113a5f50 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 16 Jun 2024 08:00:00 +0100 Subject: [PATCH] refactor: add newtype macro --- crates/config/src/lib.rs | 1 + crates/config/src/newtype.rs | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 crates/config/src/newtype.rs diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index bdbcba5..89776dc 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -8,6 +8,7 @@ mod forge_name; mod forge_type; pub mod git_dir; mod host_name; +mod newtype; mod registered_webhook; mod repo_alias; mod repo_branches; diff --git a/crates/config/src/newtype.rs b/crates/config/src/newtype.rs new file mode 100644 index 0000000..df2ef6c --- /dev/null +++ b/crates/config/src/newtype.rs @@ -0,0 +1,57 @@ +// +#[macro_export] +macro_rules! newtype { + ($name:ident is a $type:ty, without Display) => { + #[derive( + Clone, + Default, + Debug, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, + derive_more::Deref, + serde::Deserialize, + serde::Serialize, + )] + pub struct $name($type); + impl $name { + pub fn new(value: impl Into<$type>) -> Self { + Self(value.into()) + } + pub fn unwrap(self) -> $type { + self.0 + } + } + }; + ($name:ident is a $type:ty) => { + #[derive( + Clone, + Default, + Debug, + derive_more::Display, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, + derive_more::Deref, + serde::Deserialize, + serde::Serialize, + )] + pub struct $name($type); + impl $name { + pub fn new(value: impl Into<$type>) -> Self { + Self(value.into()) + } + pub fn unwrap(self) -> $type { + self.0 + } + } + }; +}