Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

luleyleo/druid-enum-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Druid enum helpers

NOTE: If you are only interested in the derive macro, check out 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

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.

#[derive(Clone, Data)]
enum Event {
    Click(u32, u32),
    Key(char),
}

fn event_widget() -> druid::WidgetMatcher<Event> {
    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<Event> {
    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

#[derive(Clone, Data, Match)]
enum Event { .. }

fn event_widget() -> impl Widget<Event> {
    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> {
    Event::matcher()
        .key(Label::dynamic(|data, _| {
            format!("key: {}", data))
        })
        .default(Label::new("Unhandled Event"))
    }
}

fn event_widget() -> impl Widget<Event> {
    // Will emit warning for missing variant
    // Event::Click at runtime
    Event::matcher()
        .key(Label::dynamic(|data, _| {
            format!("key: {}", data))
        })
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages