use crate::error::{ElementAppendError, ElementBuildError, UnknownElementError};
use crate::ElementConf;
use common::{DcElement, DcMsgReceiver, DcPipeline, MsgReceiver, Pipeline};
pub use common::{ElementResult, ElementValue, MsgType, Port};
use serde::de::DeserializeOwned;
use std::collections::HashMap;
pub(crate) type ElementNextBoxed =
Box<dyn FnMut(*mut DcPipeline, *mut DcMsgReceiver) -> ElementResult + Send>;
pub type ElementFinalizer = Box<dyn FnOnce() -> Result<(), crate::error::Error> + Send>;
pub struct ElementExecutable {
pub(crate) next_boxed: ElementNextBoxed,
pub(crate) finalizer: Option<ElementFinalizer>,
}
#[derive(Clone)]
pub enum ElementBuilder {
Native(fn(ElementConf) -> Result<ElementExecutable, crate::error::Error>),
Plugin(DcElement),
}
impl ElementBuilder {
pub(crate) fn build(
&self,
conf: ElementConf,
) -> Result<ElementExecutable, crate::error::Error> {
match self {
ElementBuilder::Native(build_executable) => build_executable(conf),
ElementBuilder::Plugin(element) => crate::plugin::build_plugin_element(*element, &conf),
}
}
}
pub struct ElementPreBuild {
conf: ElementConf,
builder: ElementBuilder,
}
impl ElementPreBuild {
pub(crate) fn build(&mut self) -> Result<ElementExecutable, crate::error::Error> {
self.builder.build(self.conf.clone())
}
}
pub trait ElementBuildable: Sized + Send + 'static {
type Config: DeserializeOwned;
const NAME: &'static str;
const RECV_PORTS: Port = 0;
const SEND_PORTS: Port = 0;
fn acceptable_msg_types() -> Vec<Vec<MsgType>> {
Vec::new()
}
fn new(conf: Self::Config) -> Result<Self, crate::error::Error>;
fn next(&mut self, pipeline: &mut Pipeline, receiver: &mut MsgReceiver) -> ElementResult;
fn finalizer(&mut self) -> Result<Option<ElementFinalizer>, crate::error::Error> {
Ok(None)
}
#[doc(hidden)]
fn element_conf_to_executable(
conf: ElementConf,
) -> Result<ElementExecutable, crate::error::Error> {
let conf: Self::Config = conf.to_conf()?;
let mut element = Self::new(conf)?;
let finalizer = element.finalizer()?;
let next_boxed: ElementNextBoxed = Box::new(move |pipeline, recv| {
let mut pipeline = unsafe { Pipeline::new(pipeline) };
let mut recv = unsafe { MsgReceiver::new(recv) };
element.next(&mut pipeline, &mut recv)
});
Ok(ElementExecutable {
next_boxed,
finalizer,
})
}
#[doc(hidden)]
fn builder() -> ElementBuilder {
ElementBuilder::Native(Self::element_conf_to_executable)
}
}
type RecvMsgTypeGetter = Box<dyn Fn() -> Vec<Vec<MsgType>>>;
pub struct ElementBank {
builders: HashMap<String, ElementBuilder>,
acceptable_msg_types: HashMap<String, RecvMsgTypeGetter>,
ports: HashMap<String, (Port, Port)>,
}
impl Default for ElementBank {
fn default() -> Self {
ElementBank::new()
}
}
impl ElementBank {
pub fn empty() -> Self {
ElementBank {
builders: HashMap::default(),
acceptable_msg_types: HashMap::default(),
ports: HashMap::default(),
}
}
pub(crate) fn append(
&mut self,
name: &str,
builder: ElementBuilder,
acceptable_msg_types: RecvMsgTypeGetter,
ports: (Port, Port),
) -> Result<(), ElementAppendError> {
if self.builders.contains_key(name) {
return Err(ElementAppendError(format!(
"element name duplication detected for \"{}\"",
name
)));
}
self.builders.insert(name.to_owned(), builder);
self.acceptable_msg_types
.insert(name.to_owned(), acceptable_msg_types);
self.ports.insert(name.to_owned(), ports);
log::trace!("append \"{}\" to element bank", name);
Ok(())
}
pub fn append_from_buildable<E: ElementBuildable + 'static>(
&mut self,
) -> Result<(), ElementAppendError> {
self.append(
E::NAME,
ElementBuilder::Native(E::element_conf_to_executable),
Box::new(E::acceptable_msg_types),
(E::RECV_PORTS, E::SEND_PORTS),
)
}
pub fn pre_build(
&self,
name: &str,
conf: ElementConf,
) -> Result<ElementPreBuild, ElementBuildError> {
if let Some(builder) = self.builders.get(name) {
Ok(ElementPreBuild {
conf,
builder: builder.clone(),
})
} else {
Err(ElementBuildError::UnknownElement(UnknownElementError(
name.into(),
)))
}
}
pub fn acceptable_msg_types(
&self,
name: &str,
) -> Result<Vec<Vec<MsgType>>, UnknownElementError> {
self.acceptable_msg_types
.get(name)
.map(|f| f())
.ok_or_else(|| UnknownElementError(name.into()))
}
pub fn ports(&self, name: &str) -> Result<(Port, Port), UnknownElementError> {
self.ports
.get(name)
.copied()
.ok_or_else(|| UnknownElementError(name.into()))
}
pub fn new() -> Self {
let mut bank = Self::empty();
crate::base::append_to_bank(&mut bank);
bank
}
}