Trait XmlComponentTrait

Source
pub trait XmlComponentTrait {
    // Required method
    fn render_dom(
        &self,
        components: &XmlComponentMap,
        arguments: &FilteredComponentArguments,
        content: &OptionAzString,
    ) -> Result<StyledDom, RenderDomError>;

    // Provided methods
    fn get_type_id(&self) -> String { ... }
    fn get_xml_node(&self) -> XmlNode { ... }
    fn get_available_arguments(&self) -> ComponentArguments { ... }
    fn compile_to_rust_code(
        &self,
        components: &XmlComponentMap,
        attributes: &ComponentArguments,
        content: &OptionAzString,
    ) -> Result<String, CompileError> { ... }
}
Expand description

Specifies a component that reacts to a parsed XML node

Required Methods§

Source

fn render_dom( &self, components: &XmlComponentMap, arguments: &FilteredComponentArguments, content: &OptionAzString, ) -> Result<StyledDom, RenderDomError>

Given a root node and a list of possible arguments, returns a DOM or a syntax error

Provided Methods§

Source

fn get_type_id(&self) -> String

Returns the type ID of this component, default = div

Source

fn get_xml_node(&self) -> XmlNode

Returns the XML node for this component, used in the get_html_string debugging code (necessary to compile the component into a function during the Rust compilation stage)

Source

fn get_available_arguments(&self) -> ComponentArguments

(Optional): Should return all arguments that this component can take - for example if you have a component called Calendar, which can take a selectedDate argument:

<Calendar
    selectedDate='01.01.2018'
    minimumDate='01.01.1970'
    maximumDate='31.12.2034'
    firstDayOfWeek='sunday'
    gridVisible='false'
/>

… then the ComponentArguments returned by this function should look something like this:

impl XmlComponentTrait for CalendarRenderer {
    fn get_available_arguments(&self) -> ComponentArguments {
        btreemap![
            "selected_date" => "DateTime",
            "minimum_date" => "DateTime",
            "maximum_date" => "DateTime",
            "first_day_of_week" => "WeekDay",
            "grid_visible" => "bool",
            /* ... */
        ]
    }
}

If a user instantiates a component with an invalid argument (i.e. <Calendar asdf="false">), the user will get an error that the component can’t handle this argument. The types are not checked, but they are necessary for the XML-to-Rust compiler.

When the XML is then compiled to Rust, the generated Rust code will look like this:

calendar(&CalendarRendererArgs {
    selected_date: DateTime::from("01.01.2018")
    minimum_date: DateTime::from("01.01.2018")
    maximum_date: DateTime::from("01.01.2018")
    first_day_of_week: WeekDay::from("sunday")
    grid_visible: false,
    .. Default::default()
})

Of course, the code generation isn’t perfect: For non-builtin types, the compiler will use Type::from to make the conversion. You can then take that generated Rust code and clean it up, put it somewhere else and create another component out of it - XML should only be seen as a high-level prototyping tool (to get around the problem of compile times), not as the final data format.

Source

fn compile_to_rust_code( &self, components: &XmlComponentMap, attributes: &ComponentArguments, content: &OptionAzString, ) -> Result<String, CompileError>

(Optional): Used to compile the XML component to Rust code - input

Implementors§