{"id":13595570,"url":"https://github.com/yoshuawuyts/context-attribute","last_synced_at":"2025-09-14T08:32:33.520Z","repository":{"id":56001349,"uuid":"177351786","full_name":"yoshuawuyts/context-attribute","owner":"yoshuawuyts","description":"Set the error context using doc comments","archived":false,"fork":false,"pushed_at":"2019-12-14T11:24:35.000Z","size":20,"stargazers_count":53,"open_issues_count":7,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-03T12:24:05.082Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.rs/context-attribute","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/yoshuawuyts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-23T23:49:38.000Z","updated_at":"2024-09-11T04:32:46.000Z","dependencies_parsed_at":"2022-08-15T11:10:28.376Z","dependency_job_id":null,"html_url":"https://github.com/yoshuawuyts/context-attribute","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fcontext-attribute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fcontext-attribute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fcontext-attribute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fcontext-attribute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yoshuawuyts","download_url":"https://codeload.github.com/yoshuawuyts/context-attribute/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232767950,"owners_count":18573676,"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-08-01T16:01:52.664Z","updated_at":"2025-01-08T01:48:47.029Z","avatar_url":"https://github.com/yoshuawuyts.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# context-attribute\n[![crates.io version][1]][2] [![build status][3]][4]\n[![downloads][5]][6] [![docs.rs docs][7]][8]\n\nSet the error [`context`] using doc comments.\n\nThis is useful because instead of writing manual error messages to provide context to an error, it\nautomatically derives it from doc comments. This works especially well for async contexts, where\nstack traces may not be persisted past yield points and thread boundaries. But contexts do.\n\n[`context`]: https://docs.rs/failure/0.1.5/failure/trait.ResultExt.html#tymethod.context\n\n- [Documentation][8]\n- [Crates.io][2]\n- [Releases][releases]\n\n## Examples\n```rust\nuse context_attribute::context;\nuse failure::{ensure, ResultExt};\n\n/// Square a number if it's less than 10.\n#[context]\nfn square(num: usize) -\u003e Result\u003cusize, failure::Error\u003e {\n    ensure!(num \u003c 10, \"Number was too large\");\n    Ok(num * num)\n}\n\nfn main() -\u003e Result\u003c(), failure::Error\u003e {\n    let args = std::env::args();\n    ensure!(args.len() == 2, \"usage: square \u003cnum\u003e\");\n    let input = args.skip(1).next().unwrap().parse()?;\n\n    println!(\"result is {}\", square(input)?);\n\n    Ok(())\n}\n```\n\n```txt\n$ cargo run --example square 12\nError: ErrorMessage { msg: \"Number was too large\" }\nSquare a number if it's less than 10.\n```\n\n## Installation\n```sh\n$ cargo add context-attribute\n```\n\n## FAQ\n### What does the code expand to?\nTake this piece of code:\n```rust\n/// Read address.txt from disk\n#[context]\npub fn read_file_2() -\u003e Result\u003cString, Error\u003e {\n    Ok(std::fs::read_to_string(\"address.txt\")?.trim().to_string())\n}\n```\nAny error that comes from it is tagged with the message `\"Read address.txt from disk\"`. To write it\nby hand we'd have to do:\n\n```rust\n/// Read address.txt from disk\npub fn read_file_1() -\u003e Result\u003cString, Error\u003e {\n    let res = std::fs::read_to_string(\"address.txt\")\n        .context(\"Read address.txt from disk\")?\n        .trim()\n        .to_string();\n    Ok(res)\n}\n```\n\n## Safety\nThis crate uses ``#![deny(unsafe_code)]`` to ensure everything is implemented in\n100% Safe Rust.\n\n## Contributing\nWant to join us? Check out our [\"Contributing\" guide][contributing] and take a\nlook at some of these issues:\n\n- [Issues labeled \"good first issue\"][good-first-issue]\n- [Issues labeled \"help wanted\"][help-wanted]\n\n## License\n[MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE)\n\n[1]: https://img.shields.io/crates/v/context-attribute.svg?style=flat-square\n[2]: https://crates.io/crates/context-attribute\n[3]: https://img.shields.io/travis/yoshuawuyts/context-attribute/master.svg?style=flat-square\n[4]: https://travis-ci.org/yoshuawuyts/context-attribute\n[5]: https://img.shields.io/crates/d/context-attribute.svg?style=flat-square\n[6]: https://crates.io/crates/context-attribute\n[7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square\n[8]: https://docs.rs/context-attribute\n\n[releases]: https://github.com/yoshuawuyts/context-attribute/releases\n[contributing]: https://github.com/yoshuawuyts/context-attribute/blob/master.github/CONTRIBUTING.md\n[good-first-issue]: https://github.com/yoshuawuyts/context-attribute/labels/good%20first%20issue\n[help-wanted]: https://github.com/yoshuawuyts/context-attribute/labels/help%20wanted\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshuawuyts%2Fcontext-attribute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyoshuawuyts%2Fcontext-attribute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshuawuyts%2Fcontext-attribute/lists"}