https://github.com/metaswitch/frunk-enum
Extensions to frunk (https://github.com/lloydmeta/frunk) to allow transmogrification of `enum` types
https://github.com/metaswitch/frunk-enum
Last synced: 5 months ago
JSON representation
Extensions to frunk (https://github.com/lloydmeta/frunk) to allow transmogrification of `enum` types
- Host: GitHub
- URL: https://github.com/metaswitch/frunk-enum
- Owner: Metaswitch
- License: other
- Created: 2019-05-13T14:48:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-01-01T08:22:44.000Z (about 1 year ago)
- Last Synced: 2025-02-01T14:17:45.213Z (about 1 year ago)
- Language: Rust
- Size: 66.4 KB
- Stars: 2
- Watchers: 6
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE-APACHE-2.0
Awesome Lists containing this project
README
# Frunk Enum Support
[](https://crates.io/crates/frunk-enum-core) [](https://opensource.org/licenses/MIT)
## Usage
These crates augment the [frunk](https://docs.rs/frunk/latest/frunk/) crate to
allow transmogrification of `enum`s as well as `struct`s. For more information
on transmogrification and the `LabelledGeneric` trait it's based around, see
https://docs.rs/frunk/latest/frunk/#transmogrifying and
https://docs.rs/frunk/latest/frunk/labelled/trait.LabelledGeneric.html.
To take advantage of this feature for your own enum you need to:
* Add the `frunk-enum-derive` as a `[dev-dependency]` to your `Cargo.toml`
* Mark the enum with the custom derive:
```
#[derive(LabelledGenericEnum)]
enum Foo {
Bar(String),
Baz { name: String, id: Identity },
}
```
* Add the `frunk-enum-core` and `frunk-core` crates as `[dependency]` entries
to your `Cargo.toml`
* Then (assuming there's a `NewFoo` enum with the same structure as `Foo`) you can write:
```
let foo = Foo::Baz { name: "Andy".into(), id: Identity };
let new_foo: NewFoo = foo.transmogrify();
```
This works by deriving an implementation of `LabelledGeneric` for `Foo` which
allows conversion to and from an instance of a generic sum-type. The core
crate provides tools for converting between these generic sum-types where the
bodies of the variants are recursively transmogrifiable. This allows for
arbitrarily deep conversion between types, especially useful where the two
types in question are autogenerated from some common input file, but are deemed
to be different types by the rust compiler (e.g. because they're in separate
crates).
## Contributing
Thank you for your interest in frunk-enum, all contributions are welcome whether
they be reports of issues, feature requests or code contributions.
### Issue Contribution
When opening issues please provide a description of the issue, the version of
frunk and frunk-enum plus example code which exhibits the issue.
### Code Contribution
If you wish to contribute code to frunk-enum, you are welcome to do so using a
github pull request, however you will need to sign each commit, to indicate
that you are abiding to the terms of the [DCO](DCO).