const std = @import("std");

const Data = enum { foo, bar };

pub fn optional_enum_error_union() !?Data {
    const state = struct {
        var flag: u2 = 0;
    };
    defer state.flag +%= 1;

    return switch (state.flag) {
        0 => null,
        1 => .foo,
        2 => .bar,
        3 => error.Fizz,
    };
}

pub fn main() !void {
    for (0..4) |_| {
        const r1 = if (optional_enum_error_union()) |opt|
            if (opt) |val|
                switch (val) {
                    .foo => "FOO",
                    .bar => "BAR",
                }
            else
                "NULL"
        else |err| switch (err) {
            error.Fizz => "FIZZ",
            else => return err,
        };

        const r2 = switch (optional_enum_error_union()) {
            .foo => "FOO",
            .bar => "BAR",
            null => "NULL",
            error.Fizz => "FIZZ",
            anyerror => |err| return err,
        };
    }
}
