Expand description

hooq
A simple macro that inserts a method before `?`.
🪝 The crate name comes from the acronym “HOOk for Question mark operator”. 🪝
Documentations:
- tutorial: (mdBook coming soon.)
- docs.rs: https://docs.rs/hooq/0.1.2/hooq/
[!NOTE] 日本語版ドキュメントはこちら: docs/ja/README.md
use hooq::hooq;
#[hooq]
#[hooq::method(.ok_or_else(|| format!("{} (L{}, {})", $expr_str, $line, $nth)))]
fn display_name(val: &toml::Value) -> Result<(), String> {
let name = val.get("package")?.get("name")?.as_str()?;
#[hooq::skip_all]
if name.contains("4") {
return Err("name contains '4'. Guido Mista disallow this.".to_string());
}
println!("name: {name}");
Ok(())
}
#[hooq]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cargo_toml = toml::from_str(&std::fs::read_to_string("Cargo.toml")?)?;
display_name(&cargo_toml)?;
Ok(())
}
The above expands to the following.
ⓘ
use hooq::hooq;
fn display_name(val: &toml::Value) -> Result<(), String> {
let name = val
.get("package")
.ok_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(
format_args!("{0} (L{1}, {2})", "val.get(\"package\")", 6usize, "1st ?"),
)
}))?
.get("name")
.ok_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(
format_args!(
"{0} (L{1}, {2})", "val.get(\"package\") ? .get(\"name\")", 6usize,
"2nd ?",
),
)
}))?
.as_str()
.ok_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(
format_args!(
"{0} (L{1}, {2})",
"val.get(\"package\") ? .get(\"name\") ? .as_str()", 6usize, "3rd ?",
),
)
}))?;
if name.contains("4") {
return Err("name contains '4'. Guido Mista disallow this.".to_string());
}
{
::std::io::_print(format_args!("name: {0}\n", name));
};
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cargo_toml = toml::from_str(
&std::fs::read_to_string("Cargo.toml")
.inspect_err(|e| {
let path = "/path/to/your/project/src/main.rs";
let line = 20usize;
{
::std::io::_eprint(
format_args!("[{0}:L{1}] {2:?}\n", path, line, e),
);
};
})?,
)
.inspect_err(|e| {
let path = "/path/to/your/project/src/main.rs";
let line = 20usize;
{
::std::io::_eprint(format_args!("[{0}:L{1}] {2:?}\n", path, line, e));
};
})?;
display_name(&cargo_toml)
.inspect_err(|e| {
let path = "/path/to/your/project/src/main.rs";
let line = 22usize;
{
::std::io::_eprint(format_args!("[{0}:L{1}] {2:?}\n", path, line, e));
};
})?;
Ok(())
}
§Quick Start
Add it with cargo add
as shown below,
cargo add hooq
or add it to your Cargo.toml
.
[dependencies]
hooq = "0.1.2"
§Method inserted by default
If you don’t specify anything for #[hooq]
, the following method is inserted by default.
ⓘ
.inspect_err(|e| {
let path = $path;
let line = $line;
::std::eprintln!("[{path}:L{line}] {e:?}");
})
You can switch the method to hook with the inert attribute #[hooq::method(...)]
. Also, when you specify a flavor at the call site such as #[hooq(log)]
or #[hooq(anyhow)]
(the anyhow
feature is required), the inserted method will change according to that flavor!
You can find the available flavors here: hooq-macros/src/impls/flavor/presets/
(More to come!)
§Attributes
(WIP)
§Meta variables
(WIP)
§Flavor
(WIP)
Macros§
- toml_
load - Prepare flavors by toml file loading.
Structs§
- Binding
Payload - A binding payload that contains the expression string and its value.
- Hooq
Meta - Metadata about the invocation of the
hooq
macro.
Attribute Macros§
- hooq
- hooq macro attribute. Methods are hooked to ? etc. in the function of the item with this macro.