Struct MappedFactory

Source
pub struct MappedFactory<Inner: InputMap> { /* private fields */ }
Expand description

An input method for types that implement the InputMap 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::input::{EventFactoryError, InputMap};
use rtlola_interpreter::Value;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[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, EventFactoryError> {
        Ok(Value::from(rec.a))
    }

    // Generate a new value for input stream 'b'
    fn b(rec: &Self) -> Result<Value, EventFactoryError> {
        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, EventFactoryError> {
        Ok(Value::Str(rec.c.clone().into_boxed_str()))
    }
}

impl InputMap for MyType {
    type CreationData = ();
    type Error = EventFactoryError;

    fn func_for_input(
        name: &str,
        _data: Self::CreationData,
    ) -> Result<Box<dyn (Fn(&MyType) -> Result<Value, EventFactoryError>)>, EventFactoryError>
    {
        match name {
            "a" => Ok(Box::new(Self::a)),
            "b" => Ok(Box::new(Self::b)),
            "c" => Ok(Box::new(Self::c)),
            x => Err(EventFactoryError::InputStreamUnknown(vec![x.to_string()])),
        }
    }
}

Trait Implementations§

Source§

impl<Inner: InputMap> EventFactory for MappedFactory<Inner>

Source§

type CreationData = <Inner as InputMap>::CreationData

Arbitrary type of the data provided to the input source at creation time.
Source§

type Error = <Inner as InputMap>::Error

The error type returned by the input source on IO errors or parsing issues.
Source§

type Record = Inner

The type from which an event is generated by the input source.
Source§

fn try_new( map: HashMap<String, InputReference>, setup_data: Self::CreationData, ) -> Result<(Self, Vec<String>), EventFactoryError>

Construction the input for only a subset of the required input streams. Enables the combination of multiple Input implementors into one. The returned Vector contains the names of the input streams that can be served by this input. The constructed input should return Value::None for all inputs that are unknown to it.
Source§

fn get_event(&self, rec: Inner) -> Result<Event, EventFactoryError>

This function converts a record to an event.
Source§

fn new( map: HashMap<String, InputReference>, setup_data: Self::CreationData, ) -> Result<Self, EventFactoryError>

Creates a new input source from a HashMap mapping the names of the inputs in the specification to their position in the event.

Auto Trait Implementations§

§

impl<Inner> Freeze for MappedFactory<Inner>

§

impl<Inner> !RefUnwindSafe for MappedFactory<Inner>

§

impl<Inner> !Send for MappedFactory<Inner>

§

impl<Inner> !Sync for MappedFactory<Inner>

§

impl<Inner> Unpin for MappedFactory<Inner>

§

impl<Inner> !UnwindSafe for MappedFactory<Inner>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> CondDeserialize for T

Source§

impl<T> CondSerialize for T