draklor40
draklor40

Reputation: 483

How can I structure/destructure an enum of enums?

I am running into issues when matching on an enum of enums. The structure is similar to this:

enum WebEvent {
    PageLoad,
    PageUnload,
    KeyPress(char),
    Paste(String),
    Click { x: i64, y: i64 },
}
enum KeyEvent {
    UpPressed,
    DownPressed,
}

enum Event {
    WebEvent,
    KeyEvent,
}

fn inspect(event: Event) {
    match event {
        Event::WebEvent::PageLoad => println!("page loaded"),
        Event::WebEvent::PageUnload => println!("page unloaded"),
        Event::WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
        Event::WebEvent::Paste(s) => println!("pasted \"{}\".", s),
        Event::WebEvent::Click { x, y } => {
            println!("clicked at x={}, y={}.", x, y);
        }
        Event::KeyEvent => {
            println!("got a key event");
        }
    }
}

fn main() {
    let pressed = WebEvent::KeyPress('x');
    inspect(pressed);
}

playground

This is the error I get from the compiler:

error[E0433]: failed to resolve. Not a module `WebEvent`
  --> src/main.rs:20:16
   |
20 |         Event::WebEvent::PageLoad => println!("page loaded"),
   |                ^^^^^^^^ Not a module `WebEvent`

error[E0433]: failed to resolve. Not a module `WebEvent`
  --> src/main.rs:21:16
   |
21 |         Event::WebEvent::PageUnload => println!("page unloaded"),
   |                ^^^^^^^^ Not a module `WebEvent`

error[E0433]: failed to resolve. Not a module `WebEvent`
  --> src/main.rs:22:16
   |
22 |         Event::WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
   |                ^^^^^^^^ Not a module `WebEvent`

error[E0433]: failed to resolve. Not a module `WebEvent`
  --> src/main.rs:23:16
   |
23 |         Event::WebEvent::Paste(s) => println!("pasted \"{}\".", s),
   |                ^^^^^^^^ Not a module `WebEvent`

error[E0433]: failed to resolve. Not a module `WebEvent`
  --> src/main.rs:24:16
   |
24 |         Event::WebEvent::Click { x, y } => {
   |                ^^^^^^^^ Not a module `WebEvent`

error[E0308]: mismatched types
  --> src/main.rs:35:13
   |
35 |     inspect(pressed);
   |             ^^^^^^^ expected enum `Event`, found enum `WebEvent`
   |
   = note: expected type `Event`
              found type `WebEvent`

What is the right way to structure/destructure an enum of enums? Is there any way I can do this?

Upvotes: 4

Views: 7367

Answers (1)

ljedrz
ljedrz

Reputation: 22193

You forgot that enum variants are not types - you need to introduce wrappers in order for an enum to be able to contain other enums:

enum Event {
    WebEvent(WebEvent),
    KeyEvent(KeyEvent)
}

The rest is pretty straightforward:

enum WebEvent {
    PageLoad,
    PageUnload,
    KeyPress(char),
    Paste(String),
    Click { x: i64, y: i64 },
}

enum KeyEvent {
    UpPressed,
    DownPressed,
}

enum Event {
    WebEvent(WebEvent),
    KeyEvent(KeyEvent)
}

fn inspect(event: Event) {
    if let Event::WebEvent(webevt) = event {
        match webevt {
            WebEvent::PageLoad => println!("page loaded"),
            WebEvent::PageUnload => println!("page unloaded"),
            WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
            WebEvent::Paste(s) => println!("pasted \"{}\".", s),
            WebEvent::Click { x, y } => println!("clicked at x={}, y={}.", x, y)
        }
    } else {
        println!("got a key event")
    }
}

fn main() {
    let pressed = Event::WebEvent(WebEvent::KeyPress('x')); // note the extra wrap here
    inspect(pressed);
}

Upvotes: 16

Related Questions