{"id":16752603,"url":"https://github.com/azriel91/enum_variant_type","last_synced_at":"2025-03-17T02:31:01.791Z","repository":{"id":45311780,"uuid":"232982658","full_name":"azriel91/enum_variant_type","owner":"azriel91","description":"Proc macro derive to generate structs from enum variants.","archived":false,"fork":false,"pushed_at":"2023-06-27T04:42:06.000Z","size":47,"stargazers_count":28,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-16T07:22:50.296Z","etag":null,"topics":["derive","enum","proc-macro","rust"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azriel91.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2020-01-10T06:37:56.000Z","updated_at":"2025-03-02T12:36:02.000Z","dependencies_parsed_at":"2024-10-27T12:10:40.074Z","dependency_job_id":null,"html_url":"https://github.com/azriel91/enum_variant_type","commit_stats":{"total_commits":27,"total_committers":2,"mean_commits":13.5,"dds":0.2222222222222222,"last_synced_commit":"99552bd0b0bff1c4f6a5dad716b90b21e856b237"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azriel91%2Fenum_variant_type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azriel91%2Fenum_variant_type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azriel91%2Fenum_variant_type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azriel91%2Fenum_variant_type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azriel91","download_url":"https://codeload.github.com/azriel91/enum_variant_type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243961665,"owners_count":20375285,"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":["derive","enum","proc-macro","rust"],"created_at":"2024-10-13T02:47:34.359Z","updated_at":"2025-03-17T02:31:01.524Z","avatar_url":"https://github.com/azriel91.png","language":"Rust","readme":"[![Crates.io](https://img.shields.io/crates/v/enum_variant_type.svg)](https://crates.io/crates/enum_variant_type)\n[![docs.rs](https://img.shields.io/docsrs/enum_variant_type)](https://docs.rs/enum_variant_type)\n[![CI](https://github.com/azriel91/enum_variant_type/workflows/CI/badge.svg)](https://github.com/azriel91/enum_variant_type/actions/workflows/ci.yml)\n[![Coverage Status](https://codecov.io/gh/azriel91/enum_variant_type/branch/main/graph/badge.svg)](https://codecov.io/gh/azriel91/enum_variant_type)\n\n# Enum Variant Type\n\nProc macro derive to generate structs from enum variants.\n\nThis is a poor-man's implementation of \u003chttps://github.com/rust-lang/rfcs/pull/2593\u003e.\n\n```toml\n[dependencies]\nenum_variant_type = \"0.3.1\"\n```\n\n## Examples\n\n```rust,edition2018\nuse enum_variant_type::EnumVariantType;\n\n#[derive(Debug, EnumVariantType, PartialEq)]\npub enum MyEnum {\n    /// Unit variant.\n    #[evt(derive(Clone, Copy, Debug, PartialEq))]\n    Unit,\n    /// Tuple variant.\n    #[evt(derive(Debug, PartialEq))]\n    Tuple(u32, u64),\n    /// Struct variant.\n    #[evt(derive(Debug))]\n    Struct { field_0: u32, field_1: u64 },\n    /// Skipped variant.\n    #[evt(skip)]\n    Skipped,\n}\n\n// Now you can do the following:\nuse core::convert::TryFrom;\nlet unit: Unit = Unit::try_from(MyEnum::Unit).unwrap();\nlet tuple: Tuple = Tuple::try_from(MyEnum::Tuple(12, 34)).unwrap();\nlet named: Struct = Struct::try_from(MyEnum::Struct {\n    field_0: 12,\n    field_1: 34,\n})\n.unwrap();\n\nlet enum_unit = MyEnum::from(unit);\nlet enum_tuple = MyEnum::from(tuple);\nlet enum_struct = MyEnum::from(named);\n\n// If the enum variant doesn't match the variant type, then the original variant is returned in\n// the `Result`'s `Err` variant.\nassert_eq!(Err(MyEnum::Unit), Tuple::try_from(MyEnum::Unit));\n```\n\n\u003cdetails\u003e\n\n\u003csummary\u003eGenerated code\u003c/summary\u003e\n\n```rust,edition2018\nuse core::convert::TryFrom;\n\n/// Unit variant.\n#[derive(Clone, Copy, Debug, PartialEq)]\npub struct Unit;\n\n/// Tuple variant.\n#[derive(Debug, PartialEq)]\npub struct Tuple(pub u32, pub u64);\n\n/// Struct variant.\n#[derive(Debug)]\npub struct Struct {\n    pub field_0: u32,\n    pub field_1: u64,\n}\n\nimpl From\u003cUnit\u003e for MyEnum {\n    fn from(variant_struct: Unit) -\u003e Self {\n        MyEnum::Unit\n    }\n}\n\nimpl TryFrom\u003cMyEnum\u003e for Unit {\n    type Error = MyEnum;\n    fn try_from(enum_variant: MyEnum) -\u003e Result\u003cSelf, Self::Error\u003e {\n        if let MyEnum::Unit = enum_variant {\n            Ok(Unit)\n        } else {\n            Err(enum_variant)\n        }\n    }\n}\n\nimpl From\u003cTuple\u003e for MyEnum {\n    fn from(variant_struct: Tuple) -\u003e Self {\n        let Tuple(_0, _1) = variant_struct;\n        MyEnum::Tuple(_0, _1)\n    }\n}\n\nimpl TryFrom\u003cMyEnum\u003e for Tuple {\n    type Error = MyEnum;\n    fn try_from(enum_variant: MyEnum) -\u003e Result\u003cSelf, Self::Error\u003e {\n        if let MyEnum::Tuple(_0, _1) = enum_variant {\n            Ok(Tuple(_0, _1))\n        } else {\n            Err(enum_variant)\n        }\n    }\n}\n\nimpl From\u003cStruct\u003e for MyEnum {\n    fn from(variant_struct: Struct) -\u003e Self {\n        let Struct { field_0, field_1 } = variant_struct;\n        MyEnum::Struct { field_0, field_1 }\n    }\n}\n\nimpl TryFrom\u003cMyEnum\u003e for Struct {\n    type Error = MyEnum;\n    fn try_from(enum_variant: MyEnum) -\u003e Result\u003cSelf, Self::Error\u003e {\n        if let MyEnum::Struct { field_0, field_1 } = enum_variant {\n            Ok(Struct { field_0, field_1 })\n        } else {\n            Err(enum_variant)\n        }\n    }\n}\n\n# pub enum MyEnum {\n#     /// Unit variant.\n#     Unit,\n#     /// Tuple variant.\n#     Tuple(u32, u64),\n#     /// Struct variant.\n#     Struct {\n#         field_0: u32,\n#         field_1: u64,\n#     },\n# }\n#\n```\n\n\u003c/details\u003e\n\n#### Additional options specified by an `evt` attribute on enum:\n\n* `#[evt(derive(Clone, Copy))]`: Derives `Clone`, `Copy` on **every** variant.\n* `#[evt(module = \"module1\")]`: Generated structs are placed into `mod module1 { ... }`.\n* `#[evt(implement_marker_traits(MarkerTrait1))]`: Generated structs all `impl MarkerTrait1`.\n\n## License\n\nLicensed under either of\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)\n\nat your option.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally\nsubmitted for inclusion in the work by you, as defined in the Apache-2.0\nlicense, shall be dual licensed as above, without any additional terms or\nconditions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazriel91%2Fenum_variant_type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazriel91%2Fenum_variant_type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazriel91%2Fenum_variant_type/lists"}