Expand description

Enum definitions for a ComponentInterface.

This module converts enum definition from UDL into structures that can be added to a ComponentInterface. A declaration in the UDL like this:

enum Example {
  "one",
  "two"
};

Will result in a Enum member being added to the resulting crate::ComponentInterface:

let e = ci.get_enum_definition("Example").unwrap();
assert_eq!(e.name(), "Example");
assert_eq!(e.variants().len(), 2);
assert_eq!(e.variants()[0].name(), "one");
assert_eq!(e.variants()[1].name(), "two");

Like in Rust, UniFFI enums can contain associated data, but this needs to be declared with a different syntax in order to work within the restrictions of WebIDL. A declaration like this:

[Enum]
interface Example {
  Zero();
  One(u32 first);
  Two(u32 first, string second);
};

Will result in an Enum member whose variants have associated fields:

let e = ci.get_enum_definition("ExampleWithData").unwrap();
assert_eq!(e.name(), "ExampleWithData");
assert_eq!(e.variants().len(), 3);
assert_eq!(e.variants()[0].name(), "Zero");
assert_eq!(e.variants()[0].fields().len(), 0);
assert_eq!(e.variants()[1].name(), "One");
assert_eq!(e.variants()[1].fields().len(), 1);
assert_eq!(e.variants()[1].fields()[0].name(), "first");

Enums are also used to represent error definitions for a ComponentInterface.

[Error]
enum Example {
  "one",
  "two"
};

Will result in an Enum member with fieldless variants being added to the resulting crate::ComponentInterface:

let err = ci.get_enum_definition("Example").unwrap();
assert_eq!(err.name(), "Example");
assert_eq!(err.variants().len(), 2);
assert_eq!(err.variants()[0].name(), "one");
assert_eq!(err.variants()[1].name(), "two");
assert_eq!(err.is_flat(), true);
assert!(ci.is_name_used_as_error(&err.name()));

A declaration in the UDL like this:

[Error]
interface Example {
  one(i16 code);
  two(string reason);
  three(i32 x, i32 y);
};

Will result in an Enum member with variants that have fields being added to the resulting crate::ComponentInterface:

let err = ci.get_enum_definition("Example").unwrap();
assert_eq!(err.name(), "Example");
assert_eq!(err.variants().len(), 3);
assert_eq!(err.variants()[0].name(), "one");
assert_eq!(err.variants()[1].name(), "two");
assert_eq!(err.variants()[2].name(), "three");
assert_eq!(err.variants()[0].fields().len(), 0);
assert_eq!(err.variants()[1].fields().len(), 1);
assert_eq!(err.variants()[1].fields()[0].name(), "reason");
assert_eq!(err.variants()[2].fields().len(), 2);
assert_eq!(err.variants()[2].fields()[0].name(), "x");
assert_eq!(err.variants()[2].fields()[1].name(), "y");
assert_eq!(err.is_flat(), false);
assert!(ci.is_name_used_as_error(err.name()));

Structs

  • Represents an enum with named variants, each of which may have named and typed fields.
  • Represents an individual variant in an Enum.