pub struct RecordInput<Inner: Record> { /* private fields */ }
Expand description

An input method for types that implement the Record trait. Useful if you do not want to bother with the order of the input streams in an event. Assuming the specification has 3 inputs: ‘a’, ‘b’ and ‘c’. You could implement this trait for your custom ‘MyType’ as follows:

use std::fmt::Formatter;

use rtlola_interpreter::monitor::Record;
use rtlola_interpreter::Value;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
struct MyError(String);
impl std::fmt::Display for MyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "An error occurred: {}", self.0)
    }
}
impl std::error::Error for MyError {}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct MyType {
    a: u64,
    b: Option<bool>,
    c: String,
}

impl MyType {
    // Generate a new value for input stream 'a'
    fn a(rec: &Self) -> Result<Value, MyError> {
        Ok(Value::from(rec.a))
    }

    // Generate a new value for input stream 'b'
    fn b(rec: &Self) -> Result<Value, MyError> {
        Ok(rec.b.map(|b| Value::from(b)).unwrap_or(Value::None))
    }

    // Generate a new value for input stream 'c'
    fn c(rec: &Self) -> Result<Value, MyError> {
        Ok(Value::Str(rec.c.clone().into_boxed_str()))
    }
}

impl Record for MyType {
    type CreationData = ();
    type Error = MyError;

    fn func_for_input(
        name: &str,
        _data: Self::CreationData,
    ) -> Result<Box<dyn (Fn(&MyType) -> Result<Value, MyError>)>, MyError> {
        match name {
            "a" => Ok(Box::new(Self::a)),
            "b" => Ok(Box::new(Self::b)),
            "c" => Ok(Box::new(Self::c)),
            x => {
                Err(MyError(format!(
                    "Unexpected input stream {} in specification.",
                    x
                )))
            },
        }
    }
}

Trait Implementations§

Arbitrary type of the data provided to the input source at creation time.
The error type returned by the input source on IO errors or parsing issues.
The type from which an event is generated by the input source.
Creates a new input source from a HashMap mapping the names of the inputs in the specification to their position in the event.
This function converts a record to an event.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.