| Crates.io | enum-update |
| lib.rs | enum-update |
| version | 0.1.1 |
| created_at | 2025-01-27 10:40:44.226557+00 |
| updated_at | 2025-01-31 00:15:07.912404+00 |
| description | A library for representing state changes as enums |
| homepage | |
| repository | https://github.com/Eisverygoodletter/enum-update |
| max_upload_size | |
| id | 1532300 |
| size | 10,834 |
enum-update is a set of macros and traits for representing state changes as enums.
Note:
enum-updateis currently atv0.1.0and is not at stablev1.0.0yet. Overall functionality should stay the same, but trait and macro names may change between versions.
enum-update to your projectImport enum-update into your project by running this command.
cargo add enum-update
All enum-update-derive macros are re-exported by enum-update. There is no need to manually add enum-update-derive to your project dependency list.
The following derive macros are provided by enum-update
| Derive macro | Description |
|---|---|
EnumUpdate |
Creates a new enum representing updates to a struct |
EnumUpdateSetters |
Add setter methods to a struct that also return enums generated by EnumUpdate |
EnumUpdateuse enum_update::*;
#[derive(Debug, PartialEq, EnumUpdate)]
pub struct MyState {
value: String,
another_value: u32,
}
// `EnumUpdate` will generate:
// pub enum MyStateUpdate {
// Value(String),
// AnotherValue(String),
// }
// impl EnumUpdate<MyStateUpdate> for MyState {
// fn apply(&mut self, update: MyStateUpdate) {
// match update {
// MyStateUpdate::Value(value) => {
// self.value = value;
// },
// MyStateUpdate::AnotherValue(another_value) => {
// self.another_value = another_value;
// }
// }
// }
// }
let mut state = MyState {
value: "initial_string_value".to_string(),
another_value: 123,
};
let first_change = MyStateUpdate::Value("new string value".to_string());
state.apply(first_change);
assert_eq!(state, MyState {
value: "new string value".to_string(),
another_value: 123
});
EnumUpdateSettersuse enum_update::*;
#[derive(Debug, PartialEq, EnumUpdate, EnumUpdateSetters)]
pub struct MyState<'a> {
value: String,
my_reference: &'a str
}
// `EnumUpdateSetters` will generate:
// impl<'a> MyState<'a> {
// fn modify_value(&mut self, value: String) -> MyStateUpdate {
// self.value = value.clone();
// }
// fn modify_my_reference(&mut self, my_reference: &'a str) -> MyStateUpdate {
// self.my_reference = my_reference;
// }
// }
let mut state: MyState<'static> = MyState {
value: "hello".to_string(),
my_reference: "world",
};
state.modify_my_reference("enum updates");
assert_eq!(state, MyState {
value: "hello".to_string(),
my_reference: "enum updates"
});
Contributions and bug fixes are always welcome. Please open an issue first before implementing a feature or fixing a bug. It is possible that I am already implementing it locally.