hooq-helpers

Crates.iohooq-helpers
lib.rshooq-helpers
version0.1.2
created_at2025-09-08 18:09:53.16595+00
updated_at2025-09-25 16:00:01.303887+00
descriptionHooqMeta struct provider for hooq. (hooq 向けの HooqMeta 構造体を提供します)
homepage
repositoryhttps://github.com/anotherhollow1125/hooq
max_upload_size
id1829675
size16,730
namnium (anotherhollow1125)

documentation

README

This is sub-crate for hooq crate. Please use hooq crate instead of using this crate directly.

hooq

A simple macro that inserts a method before `?`.

crate docs Rust

🪝 The crate name comes from the acronym "HOOk for Question mark operator". 🪝

Documentations:

[!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)

Commit count: 101

cargo fmt