Struct rtlola_interpreter::monitor::RecordInput
source · 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§
source§impl<Inner: Record> Input for RecordInput<Inner>
impl<Inner: Record> Input for RecordInput<Inner>
§type CreationData = <Inner as Record>::CreationData
type CreationData = <Inner as Record>::CreationData
Arbitrary type of the data provided to the input source at creation time.
§type Error = <Inner as Record>::Error
type Error = <Inner as Record>::Error
The error type returned by the input source on IO errors or parsing issues.
source§fn new(
map: HashMap<String, InputReference>,
setup_data: Self::CreationData
) -> Result<Self, Self::Error>
fn new(
map: HashMap<String, InputReference>,
setup_data: Self::CreationData
) -> Result<Self, Self::Error>
Creates a new input source from a HashMap mapping the names of the inputs in the specification to their position in the event.