{"id":21038682,"url":"https://github.com/n0-computer/nested-enum-utils","last_synced_at":"2025-03-02T12:18:25.360Z","repository":{"id":246819296,"uuid":"823647692","full_name":"n0-computer/nested-enum-utils","owner":"n0-computer","description":"Macros to provide conversions for nested enums","archived":false,"fork":false,"pushed_at":"2024-07-03T16:28:39.000Z","size":13,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-24T00:11:35.767Z","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/n0-computer.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2024-07-03T12:40:38.000Z","updated_at":"2025-01-17T09:51:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"01ebf721-aee9-4f73-9c8e-7a7367897e6b","html_url":"https://github.com/n0-computer/nested-enum-utils","commit_stats":null,"previous_names":["n0-computer/nested-enum-utils"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n0-computer%2Fnested-enum-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n0-computer%2Fnested-enum-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n0-computer%2Fnested-enum-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n0-computer%2Fnested-enum-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n0-computer","download_url":"https://codeload.github.com/n0-computer/nested-enum-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241503019,"owners_count":19972970,"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-19T13:34:11.670Z","updated_at":"2025-03-02T12:18:25.336Z","avatar_url":"https://github.com/n0-computer.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nested enum utils\n\nThis crate provides a single attribute macro to provide conversions from enum cases to the enum itself or to some other type.\n\nIt only works with enums where each variant has a single unnamed element, and if each variant has a distinct type.\n\nThe most basic use is to provide conversions between the enum cases and the enum type itself. You could achieve something similar with the popular [derive_more] crate.\n\n```rust\n#[enum_conversions()]\nenum Request {\n  Get(GetRequest),\n  Put(PutRequest),\n}\n```\n\nA more advanced use, and the reason for this crate to exist, is to provide conversions between enum variants and any type that itself has a *conversion* to the enum. This allows to use nested enums, like you would in a complex protocol that has several subsystems.\n\n```rust\n#[enum_conversions(Request)]\nenum StoreRequest {\n  Get(GetRequest),\n  Put(PutRequest),\n}\n\n#[enum_conversions(Request)]\nenum NetworkRequest {\n  Ping(PingRequest),\n}\n\n#[enum_conversions()]\nenum Request {\n  Store(StoreRequest),\n  Network(NetworkRequest),\n}\n```\n\nHere we define conversions from `GetRequest` to `StoreRequest`, from `StoreRequest` to `Request`, and then directly from `GetRequest` to `Request`, and corresponding [TryFrom] conversions in the other direction.\n\n## Generated conversions\n\nThe generated [From] conversions are straightforward. Obviously it is always possible to convert from an enum case to the enum itself.\n\nWe also generate [TryFrom] conversions from the enum to each variant, as well as from a reference to the enum to a reference to the variant.\n\nThe conversions that take a value are different than the ones from [derive_more]: they return the unmodified input in the error case, allowing to chain conversion attempts.\n\n```rust\nlet request = ...\nmatch GetRequest::try_from(request) {\n  Ok(get) =\u003e // handle get request\n  Err(request) =\u003e {\n    // I still got the request and can try something else\n    match PutRequest::try_from(request) {\n      ...\n    }\n  }\n}\n```\n\nThe conversions that take a reference just return a `\u0026'static str` as the error type. References are `Copy`, so we can always retry anyway.\n\n[From]: https://doc.rust-lang.org/std/convert/trait.From.html\n[TryFrom]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html\n[derive_more]: https://crates.io/crates/derive_more","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn0-computer%2Fnested-enum-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn0-computer%2Fnested-enum-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn0-computer%2Fnested-enum-utils/lists"}