{"id":17874261,"url":"https://github.com/modprog/manyhow","last_synced_at":"2025-03-21T22:31:49.812Z","repository":{"id":153215108,"uuid":"623078493","full_name":"ModProg/manyhow","owner":"ModProg","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-31T10:59:07.000Z","size":167,"stargazers_count":10,"open_issues_count":12,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T05:43:58.584Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ModProg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-03T16:45:16.000Z","updated_at":"2024-12-12T12:54:00.000Z","dependencies_parsed_at":"2023-10-20T18:31:38.971Z","dependency_job_id":"c77b6e15-426a-4e57-ae77-0a179c960679","html_url":"https://github.com/ModProg/manyhow","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ModProg%2Fmanyhow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ModProg%2Fmanyhow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ModProg%2Fmanyhow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ModProg%2Fmanyhow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ModProg","download_url":"https://codeload.github.com/ModProg/manyhow/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244880312,"owners_count":20525506,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T11:08:13.785Z","updated_at":"2025-03-21T22:31:49.267Z","avatar_url":"https://github.com/ModProg.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# manyhow\n## anyhow for proc macros\n[![CI Status](https://github.com/ModProg/manyhow/actions/workflows/test.yaml/badge.svg)](https://github.com/ModProg/manyhow/actions/workflows/test.yaml)\n[![Crates.io](https://img.shields.io/crates/v/manyhow)](https://crates.io/crates/manyhow)\n[![Docs.rs](https://img.shields.io/crates/v/template?color=informational\u0026label=docs.rs)](https://docs.rs/manyhow)\n[![Documentation for `main`](https://img.shields.io/badge/docs-main-informational)](https://modprog.github.io/manyhow/manyhow/)\n\nProc **m**acro **anyhow**, a combination of ideas from\n[`anyhow`](https://docs.rs/anyhow) and\n[`proc-macro-error`](https://docs.rs/proc-macro-error) to improve proc macro\ndevelopment, especially focused on the error handling.\n\n## Motivation\nError handling in proc-macros is unideal, as the top level functions of proc\nmacros can only return `TokenStreams` both in success and failure case. This\nmeans that I often write code like this, moving the actual implementation in\na separate function to be able to use the ergonomic rust error handling with\ne.g., `?`.\n```rust\nuse proc_macro2::TokenStream as TokenStream2;\n                                                                                           \n#[proc_macro]\npub fn my_macro(input: TokenStream) -\u003e TokenStream {\n    match actual_implementation(input.into()) {\n        Ok(output) =\u003e output,\n        Err(error) =\u003e error.into_compile_error(),\n    }\n    .into()\n}\n                                                                                           \nfn actual_implementation(input: TokenStream2) -\u003e syn::Result\u003cTokenStream2\u003e {\n    // ..\n}\n```\n\n## Using the `#[manyhow]` macro\nTo activate the error handling, just add `#[manyhow]` above any\nproc macro implementation, reducing the above example to:\n\n```rust\nuse manyhow::manyhow;\nuse proc_macro2::TokenStream as TokenStream2;\n                                                                                           \n#[manyhow]\n#[proc_macro]\nfn my_macro(input: TokenStream2) -\u003e syn::Result\u003cTokenStream2\u003e {\n    // ..\n}\n```\n\nSee [Without macros](#without-macros) to see what this expands to under the\nhood.\n\nA proc macro function marked as `#[manyhow]` can take and return any\n`TokenStream` and can also return `Result\u003cTokenStream,\nE\u003e` where `E` implements `ToTokensError`. As additional parameters a\n[dummy](#dummy-mut-tokenstream) and/or [emitter](#emitter-mut-emitter) can\nbe specified.\n\nThe `manyhow` attribute takes one optional flag when used for `proc_macro`\nand `proc_macro_attribute`. `#[manyhow(input_as_dummy)]` will take the input\nof a function like `proc_macro` to initialize the [dummy `\u0026mut\nTokenStream`](#dummy-mut-tokenstream) while `#[manyhow(item_as_dummy)]` on\n`proc_macro_attribute` will initialize the dummy with the annotated item.\n\n## Without macros\n`manyhow` can be used without proc macros, and they can be disabled by\nadding `manyhow` with `default-features=false`.\n\nThe usage is more or less the same, though with some added boilerplate from\nneeding to invoke one of `function`, `attribute` or `derive`\ndirectly.\n\nWhile the examples use closures, functions can be passed in as well. The\nabove example would then change to:\n```rust\nuse proc_macro2::TokenStream as TokenStream2;\n                                                                                           \n#[proc_macro]\npub fn my_macro(input: TokenStream) -\u003e TokenStream {\n    manyhow::function(\n        input,\n        false,\n        |input: TokenStream2| -\u003e syn::Result\u003cTokenStream2\u003e {\n            // ..\n        },\n    )\n}\n```\n[`Emitter`](#emitter-mut-emitter) and [dummy\n`TokenStream`](#dummy-mut-tokenstream) can also be used. `function` and\n`attribute` take an additional boolean parameter controlling whether the\ninput/item will be used as initial dummy.\n\n## `emitter: \u0026mut Emitter`\n`MacroHandler`s (the trait defining what closures/functions can be used\nwith `manyhow`) can take a mutable reference to an `Emitter`. This\nallows to collect errors, but not fail immediately.\n\n`Emitter::into_result` can be used to return if an `Emitter` contains\nany values.\n\n```rust\nuse manyhow::{manyhow, Emitter, ErrorMessage};\nuse proc_macro2::TokenStream as TokenStream2;\n                                                                                           \n#[manyhow]\n#[proc_macro]\nfn my_macro(input: TokenStream2, emitter: \u0026mut Emitter) -\u003e manyhow::Result\u003cTokenStream2\u003e {\n    // ..\n    emitter.emit(ErrorMessage::call_site(\"A fun error!\"));\n    emitter.into_result()?;\n    // ..\n}\n```\n\n## `dummy: \u0026mut TokenStream`\n`MacroHandler`s also take a mutable reference to a `TokenStream`, to\nenable emitting some dummy code to be used in case the macro errors.\n\nThis allows either appending tokens e.g., with `ToTokens::to_tokens` or\ndirectly setting the dummy code e.g., `*dummy = quote!{some tokens}`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodprog%2Fmanyhow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmodprog%2Fmanyhow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodprog%2Fmanyhow/lists"}