{"id":21214914,"url":"https://github.com/hadronized/try-guard","last_synced_at":"2025-12-12T14:39:11.630Z","repository":{"id":57670689,"uuid":"185417669","full_name":"hadronized/try-guard","owner":"hadronized","description":"A guard! macro inspired by the guard Alternative function from Haskell.","archived":false,"fork":false,"pushed_at":"2019-06-07T08:33:24.000Z","size":22,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-14T22:10:23.176Z","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":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hadronized.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-07T14:23:26.000Z","updated_at":"2019-10-21T22:02:47.000Z","dependencies_parsed_at":"2022-09-26T20:41:15.600Z","dependency_job_id":null,"html_url":"https://github.com/hadronized/try-guard","commit_stats":null,"previous_names":["hadronized/try-guard","phaazon/try-guard"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadronized%2Ftry-guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadronized%2Ftry-guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadronized%2Ftry-guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadronized%2Ftry-guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hadronized","download_url":"https://codeload.github.com/hadronized/try-guard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225633208,"owners_count":17499925,"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-11-20T21:32:13.600Z","updated_at":"2025-12-12T14:39:06.567Z","avatar_url":"https://github.com/hadronized.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The `guard` Haskell `Alternative` function brought to Rust\n\n\u003c!-- cargo-sync-readme start --\u003e\n\nThe [`guard!`] macro.\n\nThe [`guard!`] macro implements a control-flow sugar that occurs very often in common Rust code:\n\n```rust\nfn foo(cond: bool) -\u003e Option\u003ci32\u003e {\n  if !cond {\n    return None;\n  }\n\n  // do something useful\n\n  Some(42)\n}\n```\n\nThis pattern of testing arguments and early-returning with an error is very typical.\nUnfortunately, the [`?`] operator doesn’t help us here because we want to early-return on a\nboolean value, not an error value.\n\nA not very idiomatic and weird way to rewrite that:\n\n```rust\nfn foo(cond: bool) -\u003e Option\u003ci32\u003e {\n  if cond { Some(()) } else { None }?;\n  Some(42)\n}\n```\n\nThis crate provides the [`guard!`] macro — analoguous to the [`guard`] Haskell `Alternative`\nfunction — that helps early-return from a function if a predicate is `false`:\n\n```rust\nuse try_guard::guard;\n\nfn foo(cond: bool) -\u003e Option\u003ci32\u003e {\n  guard!(cond);\n  Some(42)\n}\n```\n\n## Custom guard types\n\nThis crate also allows you to _guard_ to anything that implements [`Try\u003cError = NoneError\u003e`] or\n`From\u003cNoneError\u003e` (nightly only).\n\nFor instance, the following works:\n\n```rust\nuse std::ops::Try;\nuse std::option::NoneError;\nuse try_guard::guard;\n\n#[derive(Clone, Debug, Eq, PartialEq)]\nenum MyGuard\u003cT\u003e {\n  Just(T),\n  Nothing\n}\n\nimpl\u003cT\u003e MyGuard\u003cT\u003e {\n  fn new(x: T) -\u003e Self {\n    MyGuard::Just(x)\n  }\n\n  fn none() -\u003e Self {\n    MyGuard::Nothing\n  }\n}\n\nimpl\u003cT\u003e Try for MyGuard\u003cT\u003e {\n  type Ok = T;\n\n  type Error = NoneError;\n\n  fn from_error(_: Self::Error) -\u003e Self {\n    MyGuard::none()\n  }\n\n  fn from_ok(x: Self::Ok) -\u003e Self {\n    MyGuard::new(x)\n  }\n\n  fn into_result(self) -\u003e Result\u003cSelf::Ok, Self::Error\u003e {\n    match self {\n      MyGuard::Just(x) =\u003e Ok(x),\n      MyGuard::Nothing =\u003e Err(NoneError)\n    }\n  }\n}\n\nfn foo(cond: bool) -\u003e MyGuard\u003ci32\u003e {\n  guard!(cond);\n  MyGuard::new(42)\n}\n\nfn main() {\n  assert_eq!(foo(false), MyGuard::Nothing);\n}\n```\n\n## More control on the error type\n\nIf you’d rather manipulate the error type when the predicate is false, you might be interested\nin the [`verify!`] macro instead. That macro is akin to [`guard!`] but instead doesn’t exit the\ncurrent scope: it maps the predicate’s truth to either `Some(())` or `None`, allowing you to\ncall `Option::ok_or` or whatever error combinator you want to.\n\n```rust\nuse try_guard::verify;\n\nfn foo(cond: bool) -\u003e Result\u003cu32, String\u003e {\n  verify!(cond).ok_or(\"bad condition\".to_owned())?;\n  Ok(123)\n}\n```\n\n## Feature flags\n\n  - The `test-nightly` feature flag can be used to test nightly-related features that come\n    freely and don’t require a nightly build of rustc to compile this crate but require one at\n    use site.\n\n[`guard!`]: guard\n[`verify!`]: verify\n[`guard`]: http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#v:guard\n[`?`]: https://doc.rust-lang.org/std/ops/trait.Try.html\n[`Try\u003cError = NoneError\u003e`]: https://doc.rust-lang.org/std/ops/trait.Try.html\n\n\u003c!-- cargo-sync-readme end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadronized%2Ftry-guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhadronized%2Ftry-guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadronized%2Ftry-guard/lists"}