use std::cell::RefCell;
use std::time::Duration;
use askama::Template;
#[derive(Debug, PartialEq)]
pub(crate) enum ActorDriver {
AtLeastEvery(Duration),
AtMostEvery(Duration),
EventDriven(Vec<Vec<String>>), CapacityDriven(Vec<Vec<String>>), Other(Vec<String>),
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) enum ConsumePattern {
PeekCopy, TakeCopy, Take, }
#[derive(Eq, PartialEq, Clone)]
pub(crate) struct Channel {
pub(crate) name: String,
pub(crate) from_mod: String, pub(crate) to_mod: String,
pub(crate) message_type: String,
pub(crate) peek: bool,
pub(crate) copy: bool,
pub(crate) batch_read: usize,
pub(crate) to_node: String,
pub(crate) from_node: String,
pub(crate) is_unbundled: bool,
pub(crate) batch_write: usize,
pub(crate) capacity: usize,
pub(crate) bundle_index: isize,
pub(crate) rebundle_index: isize,
pub(crate) bundle_on_from: RefCell<bool>,
}
impl Channel {
pub fn needs_tx_single_clone(&self) -> bool {
!self.is_unbundled
}
pub fn needs_rx_single_clone(&self) -> bool {
!self.is_unbundled
}
pub fn has_bundle_index(&self) -> bool {
self.bundle_index>=0
}
pub fn bundle_index(&self) -> isize {
self.bundle_index
}
pub fn tx_prefix_name(&self, channels:&[Channel]) -> String {
if *self.bundle_on_from.borrow() || channels.len()<=1 {
self.from_node.to_lowercase()
} else {
self.tx_prefix_distributed_name()
}
}
pub fn tx_prefix_distributed_name(&self) -> String {
format!("{}n_to_{}", self.from_mod.to_lowercase(), self.to_node.to_lowercase())
}
pub fn rx_prefix_name(&self, channels:&[Channel]) -> String {
if !*self.bundle_on_from.borrow() || channels.len()<=1 {
self.to_node.to_lowercase()
} else {
self.rx_prefix_distributed_name()
}
}
pub fn rx_prefix_distributed_name(&self) -> String {
format!("{}_to_{}",self.from_node.to_lowercase(), self.to_mod.to_lowercase())
}
pub fn restructured_bundle_rx(&self, _channels:&[Channel]) -> bool {
*self.bundle_on_from.borrow() &&
-1==self.bundle_index && self.rebundle_index>=0
}
pub fn restructured_bundle(&self) -> bool { -1==self.bundle_index && self.rebundle_index>=0 }
pub fn rebundle_index(&self) -> isize { self.rebundle_index }
pub fn should_build_read_buffer(&self) -> bool {
self.batch_read > 1 && self.copy
}
pub fn should_build_write_buffer(&self) -> bool {
self.batch_write > 1 && self.copy
}
}
pub(crate) struct Actor {
pub(crate) display_name: String,
pub(crate) mod_name: String,
pub(crate) rx_channels: Vec<Vec<Channel>>,
pub(crate) tx_channels: Vec<Vec<Channel>>,
pub(crate) driver: Vec<ActorDriver>,
}
#[derive(Template)]
#[template(path = "file_cargo.txt")]
pub(crate) struct CargoTemplate<'a> {
pub(crate) name: &'a str,
}
#[derive(Template)]
#[template(path = "dockerfile.txt")]
pub(crate) struct DockerFileTemplate<'a> {
pub(crate) name: &'a str,
}
#[derive(Template)]
#[template(path = "file_gitignore.txt")]
pub(crate) struct GitIgnoreTemplate {
}
#[derive(Template)]
#[template(path = "file_args.txt")]
pub(crate) struct ArgsTemplate {
}
#[derive(Template)]
#[template(path = "file_main.txt")]
pub(crate) struct MainTemplate<'a> {
pub(crate) test_only: &'static str,
pub(crate) actors: &'a Vec<Actor>,
pub(crate) actor_mods: Vec<String>,
pub(crate) channels: &'a Vec<Vec<Channel>>,
}
#[derive(Template)]
#[template(path = "file_actor.txt")]
pub(crate) struct ActorTemplate {
pub(crate) note_for_the_user: String,
pub(crate) display_name: String,
pub(crate) has_bundles: bool,
pub(crate) rx_channels: Vec<Vec<Channel>>,
pub(crate) tx_channels: Vec<Vec<Channel>>,
pub(crate) rx_monitor_defs: Vec<String>,
pub(crate) tx_monitor_defs: Vec<String>,
pub(crate) full_driver_block: String,
pub(crate) full_process_example_block: String,
pub(crate) message_types_to_use: Vec<String>,
pub(crate) message_types_to_define: Vec<String>,
}