From 230c71e8b5b58ea9fc854fe5509395a16dd55c64 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 29 Nov 2024 17:43:49 +0000 Subject: [PATCH] feat: add helper macros --- src/lib.rs | 2 ++ src/macros/mod.rs | 4 +++ src/macros/newtype.rs | 56 +++++++++++++++++++++++++++++++++++++++++ src/macros/print.rs | 14 +++++++++++ src/macros/to_string.rs | 14 +++++++++++ 5 files changed, 90 insertions(+) create mode 100644 src/macros/mod.rs create mode 100644 src/macros/newtype.rs create mode 100644 src/macros/print.rs create mode 100644 src/macros/to_string.rs diff --git a/src/lib.rs b/src/lib.rs index 4b861bf..6755e19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ use std::path::PathBuf; use clap::Parser; use kxio::{fs::FileSystem, net::Net}; +mod macros; + #[derive(Parser, Debug)] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] struct Commands { diff --git a/src/macros/mod.rs b/src/macros/mod.rs new file mode 100644 index 0000000..bdb5d51 --- /dev/null +++ b/src/macros/mod.rs @@ -0,0 +1,4 @@ +// +mod newtype; +mod print; +mod to_string; diff --git a/src/macros/newtype.rs b/src/macros/newtype.rs new file mode 100644 index 0000000..e5c2f99 --- /dev/null +++ b/src/macros/newtype.rs @@ -0,0 +1,56 @@ +// +#[macro_export] +macro_rules! newtype { + ($name:ident, $docs:literal) => { + #[doc = $docs] + #[derive( + Clone, + Copy, + Default, + Debug, + derive_more::Display, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, + derive_more::Constructor, + serde::Serialize, + serde::Deserialize, + )] + pub struct $name; + }; + ($name:ident, $type:ty $(, $derive:ty)*, $docs:literal) => { + #[doc = $docs] + #[derive( + Clone, + Debug, + derive_more::From, + PartialEq, + Eq, + derive_more::AsRef, + derive_more::Deref, + serde::Serialize, + serde::Deserialize, + $($derive),* + )] + pub struct $name($type); + impl $name { + pub fn new(value: impl Into<$type>) -> Self { + Self(value.into()) + } + #[allow(clippy::missing_const_for_fn)] + #[must_use] + pub fn peel(self) -> $type { + self.0 + } + } + impl From<$name> for $type { + fn from(value: $name) -> $type { + value.peel() + } + } + }; +} diff --git a/src/macros/print.rs b/src/macros/print.rs new file mode 100644 index 0000000..1a1ea95 --- /dev/null +++ b/src/macros/print.rs @@ -0,0 +1,14 @@ +// +#[macro_export] +macro_rules! p { + ($($arg:tt)*) => {{ + println!($($arg)*); + }}; +} + +#[macro_export] +macro_rules! e { + ($($arg:tt)*) => {{ + eprintln!($($arg)*); + }}; +} diff --git a/src/macros/to_string.rs b/src/macros/to_string.rs new file mode 100644 index 0000000..aca5f5c --- /dev/null +++ b/src/macros/to_string.rs @@ -0,0 +1,14 @@ +// +#[macro_export] +macro_rules! s { + ($value:expr) => { + $value.to_string() + }; +} + +#[macro_export] +macro_rules! f { + ($($arg:tt)*) => { + format!($($arg)*) + }; +}