https://github.com/luleyleo/druid-enum-helpers
https://github.com/luleyleo/druid-enum-helpers
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/luleyleo/druid-enum-helpers
- Owner: luleyleo
- License: mit
- Archived: true
- Created: 2020-04-05T14:20:26.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-16T06:22:14.000Z (almost 5 years ago)
- Last Synced: 2025-02-13T14:44:16.625Z (4 months ago)
- Language: Rust
- Size: 43.9 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Druid enum helpers
NOTE: If you are only interested in the derive macro, check out [`druid-enums`](https://github.com/Finnerale/druid-enums). It is more advanced than the derive macro in this repo, and the derive macro is IMO much nicer to use than the match macro.
In this repo I work on implementing the ideas mentioned in [druid#789](https://github.com/linebender/druid/issues/789)
The two sub-crates implement the macros while the main crate is the testing ground.
## match-macro
This works already!
Type inference is kinda broken :(
Also, using a `{}` macro inside the ui declaration seems to disable formatting.Error messages are still a bit crappy when messing up types.
```rust
#[derive(Clone, Data)]
enum Event {
Click(u32, u32),
Key(char),
}fn event_widget() -> druid::WidgetMatcher {
match_widget! { Event,
Event::Click(u32, u32) => Label::dynamic(|data, _| {
format!("x: {}, y: {}", data.0, data.1)
}),
Event::Key(char) => Label::dynamic(|data, _| format!("key: {}", data))),
}
}fn event_widget() -> impl Widget {
match_widget! { Event,
Event::Click(u32, u32) => Label::dynamic(|data, _| {
format!("x: {}, y: {}", data.0, data.1)
}),
_ => Label::dynamic(|data: &(), _| format!("key: unhandled"))),
}
}
```## match-derive
```rust
#[derive(Clone, Data, Match)]
enum Event { .. }fn event_widget() -> impl Widget {
Event::matcher()
.click(Label::dynamic(|data, _| {
format!("x: {}, y: {}", data.0, data.1)
))
.key(Label::dynamic(|data, _| {
format!("key: {}", data))
})
}fn event_widget() -> impl Widget {
Event::matcher()
.key(Label::dynamic(|data, _| {
format!("key: {}", data))
})
.default(Label::new("Unhandled Event"))
}
}fn event_widget() -> impl Widget {
// Will emit warning for missing variant
// Event::Click at runtime
Event::matcher()
.key(Label::dynamic(|data, _| {
format!("key: {}", data))
})
}
}
```