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>
impl<Inner: InputMap> EventFactory for MappedFactory<Inner>
Source§type CreationData = <Inner as InputMap>::CreationData
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
type Error = <Inner as InputMap>::Error
The error type returned by the input source on IO errors or parsing issues.
Source§fn try_new(
map: HashMap<String, InputReference>,
setup_data: Self::CreationData,
) -> Result<(Self, Vec<String>), EventFactoryError>
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>
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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