{"id":24978528,"url":"https://github.com/dowlandaiello/convertable-errors","last_synced_at":"2025-03-29T09:26:32.758Z","repository":{"id":57610702,"uuid":"379679089","full_name":"dowlandaiello/convertable-errors","owner":"dowlandaiello","description":"A declarative Rust macro for ergonomically deriving From\u003cE\u003e implementations for error Enum variants.","archived":false,"fork":false,"pushed_at":"2021-06-23T18:24:39.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-04T10:48:28.457Z","etag":null,"topics":["rust","rust-macro"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dowlandaiello.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-06-23T17:20:44.000Z","updated_at":"2021-06-23T18:24:42.000Z","dependencies_parsed_at":"2022-08-27T11:31:47.977Z","dependency_job_id":null,"html_url":"https://github.com/dowlandaiello/convertable-errors","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/dowlandaiello%2Fconvertable-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dowlandaiello%2Fconvertable-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dowlandaiello%2Fconvertable-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dowlandaiello%2Fconvertable-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dowlandaiello","download_url":"https://codeload.github.com/dowlandaiello/convertable-errors/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246164225,"owners_count":20733750,"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":["rust","rust-macro"],"created_at":"2025-02-03T23:59:35.876Z","updated_at":"2025-03-29T09:26:32.737Z","avatar_url":"https://github.com/dowlandaiello.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# convertable-errors \u0026emsp; [![Latest Version]][crates.io]\n\n[Latest Version]: https://img.shields.io/crates/v/convertable-errors.svg\n[crates.io]: https://crates.io/crates/convertable-errors\n\n## Usage\n\nSimply add this to your `Cargo.toml`\n\n```toml\nconvertable-errors = \"0.1.0\"\n```\n\nand (optionally) to your `lib.rs`:\n\n```rust\n#[macro_use]\nextern crate convertable_errors;\n```\n\n## About\n\nThis crate defines a macro for deriving From\u003cT\u003e conversions for variants of Rust enums. For example:\n\n```rust\nenum ForeignType;\n\nenum MyEnum {\n    Variant(ForeignType)\n}\n\nimpl From\u003cForeignType\u003e for MyEnum {\n    fn from(v: ForeignType) -\u003e Self {\n        Self::Variant(v)\n    }\n}\n```\n\nThis is how we would typically define conversions from foreign types to enum variants that contain those foreign types. But [**convertable-errors**](https://github.com/dowlandaiello/convertable-errors) provides a declarative Rust macro to generate these conversions for us. Convertable errors can be used to generate these types of conversions for any type of Rust enum (excluding enums with struct variants, for now), but my main use case is error-like enums.\n\nUsing convertable-errors, we can generate `From\u003cT\u003e` definitions for our enum variants like so:\n\n```rust\nenum ForeignType;\n\nconvertable_error! {\n    enum MyEnum {\n        (Variant(ForeignType), [(ForeignType, Self::Variant)]\n    }\n}\n```\nThe syntax for defining a convertable enum with convertable-errors is as follows:\n- Each variant of an enum must be wrapped in a tuple: `enum MyError { (Variant(ForeignType)), (Variant1) }`\n- The first member of the tuple represents your variant. At the moment, only tuple variants and\nunit variants are supported bc I'm a lazy fuck.\n- The second member of the tuple (optional) represents the types that can be converted into\nthat variant: `enum MyError { (Variant(ForeignType), [ ... ]), (Variant1) }`\n- The members of the convertable types array are each tuples representing the foreign type that\ncan be converted into your enum and the closure or variant to apply the foreign value to:\n`[(ForeignType, Self::Variant)]`. Internally, this second member can be a closure `|x|\nSelf::Variant(x)`, a unit variant closure `|_| Self::Variant1`, or simply a variant identifier\nwhere the value of the foreign type will be stored: `Self::Variant`. In practice, you can use\nthis macro for any enum, but I find it most useful for Error-like enums.\n\nNOTE: This isn't a serious project, I might have made some mistakes, so feel free to open a PR\n:) This is just a helpful snippet that I use and felt like sharing.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdowlandaiello%2Fconvertable-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdowlandaiello%2Fconvertable-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdowlandaiello%2Fconvertable-errors/lists"}