An open API service indexing awesome lists of open source software.

https://github.com/luleyleo/druid-enum-helpers


https://github.com/luleyleo/druid-enum-helpers

Last synced: 3 months ago
JSON representation

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))
})
}
}
```