File: enums.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,245,420 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (111 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use self::inner::ColorWithCustomValues;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/enums.js")]
extern "C" {
    fn js_c_style_enum();
    fn js_c_style_enum_with_custom_values();
    fn js_handle_optional_enums(x: Option<Color>) -> Option<Color>;
    fn js_expect_enum(x: Color, y: Option<Color>);
    fn js_expect_enum_none(x: Option<Color>);
    fn js_renamed_enum(b: RenamedEnum);
    fn js_enum_with_error_variant();
}

#[wasm_bindgen]
#[derive(PartialEq, Debug)]
pub enum Color {
    Green,
    Yellow,
    Red,
}

pub mod inner {
    use wasm_bindgen::prelude::*;

    #[wasm_bindgen]
    pub enum ColorWithCustomValues {
        Green = 21,
        Yellow = 34,
        Red = 2,
    }
}

#[wasm_bindgen(js_name = JsRenamedEnum)]
#[derive(Copy, Clone)]
pub enum RenamedEnum {
    A = 10,
    B = 20,
}

#[wasm_bindgen]
pub fn enum_cycle(color: Color) -> Color {
    match color {
        Color::Green => Color::Yellow,
        Color::Yellow => Color::Red,
        Color::Red => Color::Green,
    }
}

#[wasm_bindgen]
pub fn enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues {
    match color {
        ColorWithCustomValues::Green => ColorWithCustomValues::Yellow,
        ColorWithCustomValues::Yellow => ColorWithCustomValues::Red,
        ColorWithCustomValues::Red => ColorWithCustomValues::Green,
    }
}

#[wasm_bindgen_test]
fn c_style_enum() {
    js_c_style_enum();
}

#[wasm_bindgen_test]
fn c_style_enum_with_custom_values() {
    js_c_style_enum_with_custom_values();
}

#[wasm_bindgen]
pub fn handle_optional_enums(x: Option<Color>) -> Option<Color> {
    x
}

#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum EnumWithErrorVariant {
    OK,
    Warning,
    Error,
}

#[wasm_bindgen_test]
fn test_optional_enums() {
    use self::Color::*;

    assert_eq!(js_handle_optional_enums(None), None);
    assert_eq!(js_handle_optional_enums(Some(Green)), Some(Green));
    assert_eq!(js_handle_optional_enums(Some(Yellow)), Some(Yellow));
    assert_eq!(js_handle_optional_enums(Some(Red)), Some(Red));
}

#[wasm_bindgen_test]
fn test_optional_enum_values() {
    use self::Color::*;

    js_expect_enum(Green, Some(Green));
    js_expect_enum(Yellow, Some(Yellow));
    js_expect_enum(Red, Some(Red));
    js_expect_enum_none(None);
}

#[wasm_bindgen_test]
fn test_renamed_enum() {
    js_renamed_enum(RenamedEnum::B);
}

#[wasm_bindgen_test]
fn test_enum_with_error_variant() {
    js_enum_with_error_variant();
}