use libhaystack::val::{Value, kind::HaystackKind};
use uuid::Uuid;
use crate::base::Status;
use crate::base::link::{BaseLink, Link};
use super::InputProps;
#[derive(Debug, Default)]
pub struct BaseInput<Reader, Writer> {
pub name: String,
pub kind: HaystackKind,
pub block_id: Uuid,
pub connection_count: usize,
pub reader: Reader,
pub writer: Writer,
pub val: Option<Value>,
pub status: Status,
pub links: Vec<BaseLink<Writer>>,
}
impl<Reader, Writer: Clone + Send> InputProps for BaseInput<Reader, Writer> {
type Reader = Reader;
type Writer = Writer;
fn name(&self) -> &str {
&self.name
}
fn kind(&self) -> &HaystackKind {
&self.kind
}
fn block_id(&self) -> &Uuid {
&self.block_id
}
fn is_connected(&self) -> bool {
self.connection_count > 0
}
fn links(&self) -> Vec<&(dyn Link + Send)> {
self.links.iter().map(|l| l as &(dyn Link + Send)).collect()
}
fn add_link(&mut self, link: BaseLink<Self::Writer>) {
self.links.push(link)
}
fn remove_link_by_id(&mut self, link_id: &Uuid) {
self.links.retain(|l| l.id() != link_id)
}
fn remove_target_block_links(&mut self, block_id: &Uuid) {
self.links.retain(|l| l.target_block_id() != block_id)
}
fn remove_all_links(&mut self) {
self.links.clear()
}
fn reader(&mut self) -> &mut Self::Reader {
&mut self.reader
}
fn writer(&mut self) -> &mut Self::Writer {
&mut self.writer
}
fn get_value(&self) -> Option<&Value> {
self.val.as_ref()
}
fn increment_conn(&mut self) -> usize {
self.connection_count += 1;
self.connection_count
}
fn decrement_conn(&mut self) -> usize {
if self.connection_count > 0 {
self.connection_count -= 1;
if self.connection_count == 0 {
self.val = None;
self.status = Status::Ok;
}
}
self.connection_count
}
}