| 1 |
use self::inner::ColorWithCustomValues; |
| 2 |
use wasm_bindgen::prelude::*; |
| 3 |
use wasm_bindgen_test::*; |
| 4 |
|
| 5 |
#[wasm_bindgen(module = "tests/wasm/enums.js")] |
| 6 |
extern "C" { |
| 7 |
fn js_c_style_enum(); |
| 8 |
fn js_c_style_enum_with_custom_values(); |
| 9 |
fn js_handle_optional_enums(x: Option<Color>) -> Option<Color>; |
| 10 |
fn js_expect_enum(x: Color, y: Option<Color>); |
| 11 |
fn js_expect_enum_none(x: Option<Color>); |
| 12 |
fn js_renamed_enum(b: RenamedEnum); |
| 13 |
fn js_enum_with_error_variant(); |
| 14 |
} |
| 15 |
|
| 16 |
#[wasm_bindgen] |
| 17 |
#[derive(PartialEq, Debug)] |
| 18 |
pub enum Color { |
| 19 |
Green, |
| 20 |
Yellow, |
| 21 |
Red, |
| 22 |
} |
| 23 |
|
| 24 |
pub mod inner { |
| 25 |
use wasm_bindgen::prelude::*; |
| 26 |
|
| 27 |
#[wasm_bindgen] |
| 28 |
pub enum ColorWithCustomValues { |
| 29 |
Green = 21, |
| 30 |
Yellow = 34, |
| 31 |
Red = 2, |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
#[wasm_bindgen(js_name = JsRenamedEnum)] |
| 36 |
#[derive(Copy, Clone)] |
| 37 |
pub enum RenamedEnum { |
| 38 |
A = 10, |
| 39 |
B = 20, |
| 40 |
} |
| 41 |
|
| 42 |
#[wasm_bindgen] |
| 43 |
pub fn enum_cycle(color: Color) -> Color { |
| 44 |
match color { |
| 45 |
Color::Green => Color::Yellow, |
| 46 |
Color::Yellow => Color::Red, |
| 47 |
Color::Red => Color::Green, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[wasm_bindgen] |
| 52 |
pub fn enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues { |
| 53 |
match color { |
| 54 |
ColorWithCustomValues::Green => ColorWithCustomValues::Yellow, |
| 55 |
ColorWithCustomValues::Yellow => ColorWithCustomValues::Red, |
| 56 |
ColorWithCustomValues::Red => ColorWithCustomValues::Green, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
#[wasm_bindgen_test] |
| 61 |
fn c_style_enum() { |
| 62 |
js_c_style_enum(); |
| 63 |
} |
| 64 |
|
| 65 |
#[wasm_bindgen_test] |
| 66 |
fn c_style_enum_with_custom_values() { |
| 67 |
js_c_style_enum_with_custom_values(); |
| 68 |
} |
| 69 |
|
| 70 |
#[wasm_bindgen] |
| 71 |
pub fn handle_optional_enums(x: Option<Color>) -> Option<Color> { |
| 72 |
x |
| 73 |
} |
| 74 |
|
| 75 |
#[wasm_bindgen] |
| 76 |
#[derive(Copy, Clone)] |
| 77 |
pub enum EnumWithErrorVariant { |
| 78 |
OK, |
| 79 |
Warning, |
| 80 |
Error, |
| 81 |
} |
| 82 |
|
| 83 |
#[wasm_bindgen_test] |
| 84 |
fn test_optional_enums() { |
| 85 |
use self::Color::*; |
| 86 |
|
| 87 |
assert_eq!(js_handle_optional_enums(None), None); |
| 88 |
assert_eq!(js_handle_optional_enums(Some(Green)), Some(Green)); |
| 89 |
assert_eq!(js_handle_optional_enums(Some(Yellow)), Some(Yellow)); |
| 90 |
assert_eq!(js_handle_optional_enums(Some(Red)), Some(Red)); |
| 91 |
} |
| 92 |
|
| 93 |
#[wasm_bindgen_test] |
| 94 |
fn test_optional_enum_values() { |
| 95 |
use self::Color::*; |
| 96 |
|
| 97 |
js_expect_enum(Green, Some(Green)); |
| 98 |
js_expect_enum(Yellow, Some(Yellow)); |
| 99 |
js_expect_enum(Red, Some(Red)); |
| 100 |
js_expect_enum_none(None); |
| 101 |
} |
| 102 |
|
| 103 |
#[wasm_bindgen_test] |
| 104 |
fn test_renamed_enum() { |
| 105 |
js_renamed_enum(RenamedEnum::B); |
| 106 |
} |
| 107 |
|
| 108 |
#[wasm_bindgen_test] |
| 109 |
fn test_enum_with_error_variant() { |
| 110 |
js_enum_with_error_variant(); |
| 111 |
} |