Struct Interface
pub struct Interface<'a> {
doc: Option<&'a Doc>,
members: &'a [Member<'a>],
name: &'a InterfaceName,
}Expand description
A Varlink interface.
Holding an Interface proves that it follows the rules of the
specification: names are unique, referenced types are declared and enums
have variants.
use varlet_idl::{Doc, Field, FieldName, Interface, InterfaceName, Member, Method, Name, Type};
const ADD: Method = Method::new(
Name::from_static("Add"),
&[
Field::new(FieldName::from_static("a"), Type::INT),
Field::new(FieldName::from_static("b"), Type::INT),
],
&[Field::new(FieldName::from_static("sum"), Type::INT)],
)
.with_doc(Doc::from_static("Adds numbers."));
static CALC: Interface<'static> = Interface::from_static(
InterfaceName::from_static("org.example.calc"),
&[Member::Method(ADD)],
);
let add = CALC
.methods()
.find(|method| method.name().as_str() == "Add");
assert!(add.is_some());Fields§
§doc: Option<&'a Doc>§members: &'a [Member<'a>]§name: &'a InterfaceNameImplementations§
§impl<'a> Interface<'a>
impl<'a> Interface<'a>
pub fn errors(self) -> impl Iterator<Item = ErrorDef<'a>>
pub fn errors(self) -> impl Iterator<Item = ErrorDef<'a>>
Returns the error declarations in the order they are declared.
pub const fn members(self) -> &'a [Member<'a>]
pub const fn members(self) -> &'a [Member<'a>]
Returns the types, methods and errors in the order they are declared.
pub fn methods(self) -> impl Iterator<Item = Method<'a>>
pub fn methods(self) -> impl Iterator<Item = Method<'a>>
Returns the method declarations in the order they are declared.
pub const fn name(self) -> &'a InterfaceName
pub const fn name(self) -> &'a InterfaceName
Returns the interface name.
pub const fn new(
name: &'a InterfaceName,
members: &'a [Member<'a>],
) -> Result<Self, ValidationError<'a>>
pub const fn new( name: &'a InterfaceName, members: &'a [Member<'a>], ) -> Result<Self, ValidationError<'a>>
Creates an interface from its declarations.
§Errors
Returns the first rule of the specification the declarations break.
§impl Interface<'static>
impl Interface<'static>
pub const fn from_static(
name: &'static InterfaceName,
members: &'static [Member<'static>],
) -> Self
pub const fn from_static( name: &'static InterfaceName, members: &'static [Member<'static>], ) -> Self
§impl<'a> Interface<'a>
impl<'a> Interface<'a>
pub fn parse(input: &'a str, arena: &'a Arena) -> Result<Self, ParseError>
Available on crate feature parse only.
pub fn parse(input: &'a str, arena: &'a Arena) -> Result<Self, ParseError>
parse only.Parses an interface definition.
The result borrows names and doc comments from input. Lists, nested
types and doc comments of several lines are stored in arena.
let arena = varlet_idl::Arena::new();
let interface = varlet_idl::Interface::parse(
"# Adds numbers.
interface org.example.calc
method Add(a: int, b: int) -> (sum: int)",
&arena,
)?;
assert_eq!(interface.name().as_str(), "org.example.calc");
assert_eq!(interface.methods().count(), 1);§Errors
Returns where and why the input isn’t a valid interface definition.
The parser follows the grammar of the specification with one exception: declarations don’t need to be on separate lines, as the specification also says that whitespace is ignored.