feat: add helper macros

This commit is contained in:
Paul Campbell 2024-11-29 17:43:49 +00:00
parent c960129be2
commit 230c71e8b5
5 changed files with 90 additions and 0 deletions

View file

@ -4,6 +4,8 @@ use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use kxio::{fs::FileSystem, net::Net}; use kxio::{fs::FileSystem, net::Net};
mod macros;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
struct Commands { struct Commands {

4
src/macros/mod.rs Normal file
View file

@ -0,0 +1,4 @@
//
mod newtype;
mod print;
mod to_string;

56
src/macros/newtype.rs Normal file
View file

@ -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()
}
}
};
}

14
src/macros/print.rs Normal file
View file

@ -0,0 +1,14 @@
//
#[macro_export]
macro_rules! p {
($($arg:tt)*) => {{
println!($($arg)*);
}};
}
#[macro_export]
macro_rules! e {
($($arg:tt)*) => {{
eprintln!($($arg)*);
}};
}

14
src/macros/to_string.rs Normal file
View file

@ -0,0 +1,14 @@
//
#[macro_export]
macro_rules! s {
($value:expr) => {
$value.to_string()
};
}
#[macro_export]
macro_rules! f {
($($arg:tt)*) => {
format!($($arg)*)
};
}