feat(macros): fail! and s! macros

This commit is contained in:
Lucas Colombo 2024-03-31 22:12:06 -03:00
parent 8aa903815b
commit 06dc51dcfc
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
8 changed files with 202 additions and 2 deletions

12
.github/img/logo-macros.svg vendored Normal file
View File

@ -0,0 +1,12 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 142 125.73438">
<style>
.a { fill: #000000; }
@media (prefers-color-scheme: dark) {
.a { fill: #ffffff; }
}
</style>
<path class="a" d="M4.91962,1.516H0V0H20.42877V61.6416h4.6695v1.51648H.3335V61.6416H4.91962ZM32.68567,58.60962q-5.33725-5.3865-5.33649-15.45264,0-10.06072,5.71161-15.49371,5.71014-5.432,16.051-5.432,10.33767,0,15.42578,5.01056Q69.62248,32.25306,69.624,42.5265,69.624,63.999,48.44489,64,38.02069,64,32.68567,58.60962ZM43.35858,39.83124v6.56811q0,9.18109.54187,11.03144a18.93828,18.93828,0,0,0,1.12549,3.032,3.54886,3.54886,0,0,0,3.58575,2.02167q3.252,0,4.25214-3.79071.751-2.69324.75079-12.12592v-7.1575a57.79357,57.79357,0,0,0-.87579-11.916q-.87543-3.74541-4.04382-3.74713a4.16107,4.16107,0,0,0-2.96,1.05243,6.4872,6.4872,0,0,0-1.62616,3.495A64.699,64.699,0,0,0,43.35858,39.83124ZM137.33051,61.6416V0H116.90173V1.516h4.91962V61.6416h-4.58618v1.51648H142V61.6416Zm-59.36792-3.032Q72.62468,53.2231,72.62518,43.157q0-10.06072,5.7116-15.49371,5.71161-5.432,16.051-5.432,10.33839,0,15.42669,5.01056,5.08347,5.01123,5.08538,15.28473Q114.8999,63.999,93.721,64q-10.423,0-15.75841-5.39038ZM88.6355,39.83124v6.56811q0,9.18109.541,11.03144a18.93282,18.93282,0,0,0,1.12641,3.032,3.54715,3.54715,0,0,0,3.58477,2.02167q3.25276,0,4.25269-3.79071.75165-2.69324.75122-12.12592h.00006v-7.1575a57.81892,57.81892,0,0,0-.87634-11.916q-.87533-3.74541-4.04425-3.74713a4.16011,4.16011,0,0,0-2.95954,1.05243,6.48466,6.48466,0,0,0-1.62567,3.495A64.70833,64.70833,0,0,0,88.6355,39.83124Z" />
<path class="a" d="M0,80.25H142V78H0Z" />
<path d="M12.7251,118.52637V100.98242H16.771v2.584h.23828a6.15028,6.15028,0,0,1,1.17285-2.1084,2.70246,2.70246,0,0,1,2.15918-.88379q2.54957,0,3.12793,2.99219h.20361a6.6499,6.6499,0,0,1,.51026-1.15625,3.874,3.874,0,0,1,.748-.95215,3.3125,3.3125,0,0,1,1.05371-.64551,3.9393,3.9393,0,0,1,1.42823-.23828q3.67162,0,3.67187,5.40625v12.5459h-4.0459V106.49023a2.66121,2.66121,0,0,0-.4248-1.71679,1.3927,1.3927,0,0,0-1.105-.49317,1.56786,1.56786,0,0,0-1.105.42481,1.70345,1.70345,0,0,0-.459,1.30957v12.51172H19.86523V106.49023a2.83881,2.83881,0,0,0-.39111-1.71679,1.29426,1.29426,0,0,0-1.0708-.49317,1.558,1.558,0,0,0-1.63232,1.73438v12.51172Zm34.57763,0a3.78551,3.78551,0,0,1-2.51611-.79883,3.35074,3.35074,0,0,1-1.12207-2.29492h-.16992a4.30974,4.30974,0,0,1-1.88721,2.61816,6.37865,6.37865,0,0,1-3.45068.88379,5.93329,5.93329,0,0,1-4.09717-1.37695,4.896,4.896,0,0,1-1.54687-3.85938,4.54924,4.54924,0,0,1,2.00585-4.08008,10.71218,10.71218,0,0,1,5.88184-1.32617h2.82227v-.88379a3.39428,3.39428,0,0,0-.731-2.36328,3.22194,3.22194,0,0,0-2.46484-.79883,4.84037,4.84037,0,0,0-2.584.61231,5.9886,5.9886,0,0,0-1.70019,1.59765l-2.75391-2.44726a6.59545,6.59545,0,0,1,2.771-2.46582,10.594,10.594,0,0,1,4.84472-.96875,8.66867,8.66867,0,0,1,5.627,1.64941,6.04819,6.04819,0,0,1,2.02294,4.98047v7.68457h1.93848v3.6377Zm-7.24218-2.88965a3.80021,3.80021,0,0,0,2.24414-.64649,2.145,2.145,0,0,0,.918-1.87011V111.0127h-2.686q-3.09448,0-3.09375,2.07324v.68066a1.56922,1.56922,0,0,0,.71387,1.39356A3.37456,3.37456,0,0,0,40.06055,115.63672Zm19.27783,3.29785a10.3923,10.3923,0,0,1-3.72315-.62891,7.49174,7.49174,0,0,1-2.78808-1.81933,8.06457,8.06457,0,0,1-1.751-2.88965,12.37105,12.37105,0,0,1,0-7.68457,8.08154,8.08154,0,0,1,1.751-2.88965,7.50376,7.50376,0,0,1,2.78808-1.81934,10.29054,10.29054,0,0,1,3.689-.6289,8.21776,8.21776,0,0,1,4.64111,1.17285,7.57692,7.57692,0,0,1,2.669,3.11133l-3.876,2.1084a5.15829,5.15829,0,0,0-1.24072-1.7002,3.19857,3.19857,0,0,0-2.19336-.68066,3.4691,3.4691,0,0,0-2.669,1.0039,3.85116,3.85116,0,0,0-.935,2.73633v2.85645a3.85279,3.85279,0,0,0,.935,2.73633,4.22278,4.22278,0,0,0,5.03223.32324,5.68031,5.68031,0,0,0,1.41064-1.76758l3.80811,2.17578a7.65522,7.65522,0,0,1-2.771,3.11133A8.69962,8.69962,0,0,1,59.33838,118.93457Zm8.60205-4.35254h4.0459v-9.65527h-4.0459v-3.94434h9.07764v4.96387h.23828a9.96664,9.96664,0,0,1,.66308-1.85254,6.03177,6.03177,0,0,1,1.08789-1.59863,4.8021,4.8021,0,0,1,1.61524-1.1045,5.65114,5.65114,0,0,1,2.24316-.4082h1.666v4.624H80.79248a3.52833,3.52833,0,0,0-2.82227,1.10058,3.97521,3.97521,0,0,0-.95214,2.69336V114.582h5.78027v3.94434H67.94043Zm25.36377,4.35254a10.15947,10.15947,0,0,1-3.67285-.62891,7.51421,7.51421,0,0,1-2.77051-1.81933,8.05767,8.05767,0,0,1-1.751-2.88965,12.36164,12.36164,0,0,1,0-7.68457,8.07461,8.07461,0,0,1,1.751-2.88965,7.5264,7.5264,0,0,1,2.77051-1.81934,11.03653,11.03653,0,0,1,7.34472,0,7.53439,7.53439,0,0,1,2.77051,1.81934,8.07447,8.07447,0,0,1,1.751,2.88965,12.36164,12.36164,0,0,1,0,7.68457,8.05753,8.05753,0,0,1-1.751,2.88965,7.52218,7.52218,0,0,1-2.77051,1.81933A10.15409,10.15409,0,0,1,93.3042,118.93457Zm0-3.74023a3.39232,3.39232,0,0,0,2.65137-1.05372,4.31,4.31,0,0,0,.95214-2.99218v-2.78809a4.3117,4.3117,0,0,0-.95214-2.99219,3.8638,3.8638,0,0,0-5.30371,0,4.31165,4.31165,0,0,0-.95215,2.99219v2.78809a4.30993,4.30993,0,0,0,.95215,2.99218A3.39163,3.39163,0,0,0,93.3042,115.19434Zm17.47559,3.74023a13.36635,13.36635,0,0,1-5.083-.88379,7.19064,7.19064,0,0,1-3.24707-2.41406l2.82129-2.61817a7.317,7.317,0,0,0,2.39746,1.751,7.48009,7.48009,0,0,0,3.14453.62891,4.98932,4.98932,0,0,0,2.34668-.459,1.50352,1.50352,0,0,0,.84961-1.41113,1.05742,1.05742,0,0,0-.57813-1.03711,5.843,5.843,0,0,0-1.59765-.459l-2.82227-.44141a11.65507,11.65507,0,0,1-2.21-.52734,5.79126,5.79126,0,0,1-1.76758-.96875,4.51746,4.51746,0,0,1-1.19043-1.4961,4.74935,4.74935,0,0,1-.4414-2.14258,5.07766,5.07766,0,0,1,2.10742-4.31738,9.76424,9.76424,0,0,1,5.916-1.56445,12.33252,12.33252,0,0,1,4.40332.69726,6.98242,6.98242,0,0,1,2.90723,1.98926l-2.5166,2.85547a6.399,6.399,0,0,0-2.00586-1.42773,6.87878,6.87878,0,0,0-2.958-.57813q-2.92383,0-2.92383,1.76758a1.08489,1.08489,0,0,0,.57813,1.07129,5.76443,5.76443,0,0,0,1.59765.459l2.78809.44239a11.73064,11.73064,0,0,1,2.21.52636,6.01,6.01,0,0,1,1.78516.96973,4.42269,4.42269,0,0,1,1.207,1.49609,4.75812,4.75812,0,0,1,.4414,2.14161,5.15165,5.15165,0,0,1-2.125,4.35156A9.87225,9.87225,0,0,1,110.77979,118.93457Zm15.16308,2a3.4845,3.4845,0,0,1-2.584-.78223,2.60934,2.60934,0,0,1-.74805-1.87011v-1.08789a2.54235,2.54235,0,0,1,.76465-1.83594,4.60276,4.60276,0,0,1,5.13379,0,2.53943,2.53943,0,0,1,.76562,1.83594v1.08789a2.61344,2.61344,0,0,1-.748,1.87011A3.48761,3.48761,0,0,1,125.94287,120.93457Zm-.81641-8.66992-1.9375-8.56836V96.79492h5.50782v6.90137l-1.93848,8.56836Z" style="fill:#d18d4c" />
</svg>

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -22,6 +22,7 @@ path = "lib/lib.rs"
[features] [features]
cli-stylize = ["dep:bitflags"] cli-stylize = ["dep:bitflags"]
logger = ["dep:log"] logger = ["dep:log"]
macros = []
[dependencies] [dependencies]
bitflags = { version = "2.5.0", optional = true } bitflags = { version = "2.5.0", optional = true }

View File

@ -14,4 +14,5 @@
# Features # Features
- [x] [cli/stylize](lib/cli/stylize) - [x] [cli/stylize](lib/cli/stylize)
- [x] [logging](lib/logger) - [x] [logging](lib/logger)
- [x] [macros](lib/macros)

18
examples/fail_macro.rs Normal file
View File

@ -0,0 +1,18 @@
use {
eyre::{set_hook, DefaultHandler, Result},
lool::f,
};
fn setup_eyre() {
let _ = set_hook(Box::new(DefaultHandler::default_with));
}
fn main() -> Result<()> {
setup_eyre();
if (2 + 2) == 5 {
return f!("Oh no! I'm bad at math!");
}
Ok(())
}

View File

@ -2,3 +2,6 @@ pub mod cli;
#[cfg(feature = "logger")] #[cfg(feature = "logger")]
pub mod logger; pub mod logger;
#[cfg(feature = "macros")]
pub mod macros;

View File

@ -16,7 +16,7 @@
This crate is for internal use. It's only published privately. This crate is for internal use. It's only published privately.
```bash ```bash
cargo add lool --registry=lugit cargo add lool --registry=lugit --features loggers
``` ```
# Setup # Setup

82
lib/macros/README.md Normal file
View File

@ -0,0 +1,82 @@
<p align="center"><img src="./../../.github/img/logo-macros.svg" height="192"></p>
<br>
<br>
<br>
<p align="center"><b>lool » <code>macros</code></b> is a collection of utility macros for rust.</p>
</p>
<br>
<br>
<br>
# Installation
This crate is for internal use. It's only published privately.
```bash
cargo add lool --registry=lugit --features macros
```
# Macros
## `s`
A macro to create a `String` from a `&str`.
```rs
use lool::s;
fn main() {
let s = s!("Hello, world!");
// now `s` is a `String`
}
```
## `f` or `fail`
A macro to use with the [`eyre`](https://crates.io/crates/eyre) crate.
It basically creates an `Err`.
It's almost the same as `bail!` from the `anyhow` crate, but this one doesn't explicitly returns (the `bail!` macro creates a `return` statement).
This macro is equivalent to: `Err(eyre!(<args>))`.
```rs
use lool::f;
fn main() -> eyre::Result<()> {
if (2 + 2) == 5 {
f!("Oh no! I'm bad at math!");
}
Ok(())
}
```
which is equivalent to:
```rs
use eyre::eyre;
fn main() -> eyre::Result<()> {
if (2 + 2) == 5 {
return Err(eyre!("Oh no! I'm bad at math!"));
}
Ok(())
}
```
It also works as a final statement:
```rs
use lool::f;
fn fail_always() -> eyre::Result<()> {
// no ; at the end
f!("I'm a failure")
}
```

83
lib/macros/mod.rs Normal file
View File

@ -0,0 +1,83 @@
/// 🧉 » create string
/// --
///
/// create a new `String` from a string literal or a string expression -
/// this macro is equivalent to `String::from(<args>)`.
///
/// ### Example
/// ```rust
/// use lool::s;
/// let s: String = s!("hello world");
/// ```
#[macro_export]
macro_rules! s {
($s:expr) => {
String::from($s)
};
}
/// 🧉 » fail macro
/// --
///
/// this macro is equivalent to `Err(eyre!(<args>))`.
///
/// same as `bail!` but without the explicit return
///
/// ### Example
///
/// ```should_panic
/// use lool::fail;
/// use eyre::Result;
/// # use eyre::{set_hook, DefaultHandler};
/// # fn setup_eyre() {
/// # let _ = set_hook(Box::new(DefaultHandler::default_with));
/// # }
///
/// fn main() -> Result<()> {
/// # setup_eyre();
/// fail!("permission denied")
/// }
/// ```
#[macro_export]
macro_rules! fail {
($msg:literal $(,)?) => {
Err(eyre::eyre!($msg))
};
($err:expr $(,)?) => {
Err(eyre::eyre!($err))
};
($fmt:expr, $($arg:tt)*) => {
Err(eyre::eyre!($fmt, $($arg)*))
};
}
/// 🧉 » fail macro
/// --
///
/// this macro is equivalent to `Err(eyre!(<args>))`.
///
/// same as `bail!` but without the explicit return
///
/// this is an alias for `fail!`
///
/// ### Example
///
/// ```should_panic
/// use lool::f;
/// use eyre::Result;
/// # use eyre::{set_hook, DefaultHandler};
/// # fn setup_eyre() {
/// # let _ = set_hook(Box::new(DefaultHandler::default_with));
/// # }
///
/// fn main() -> Result<()> {
/// # setup_eyre();
/// f!("permission denied")
/// }
/// ```
#[macro_export]
macro_rules! f {
($($tokens:tt)*) => {
lool::fail!($($tokens)*)
};
}