use libhaystack::val::{Value, kind::HaystackKind};
use uuid::Uuid;
use crate::base::Status;
use crate::base::link::Link;
use super::{OutputProps, props::OutDesc};
#[derive(Debug)]
pub struct BaseOutput<L: Link> {
desc: OutDesc,
pub value: Value,
pub links: Vec<L>,
pub block_id: Uuid,
pub effective_status: Status,
}
impl<L: Link + Send> OutputProps for BaseOutput<L> {
fn desc(&self) -> &OutDesc {
&self.desc
}
fn block_id(&self) -> &Uuid {
&self.block_id
}
fn value(&self) -> &Value {
&self.value
}
fn is_connected(&self) -> bool {
!self.links.is_empty()
}
fn links(&self) -> Vec<&(dyn Link + Send)> {
self.links
.iter()
.map(|link| link as &(dyn Link + Send))
.collect()
}
fn remove_link_by_id(&mut self, link_id: &Uuid) {
self.links.retain(|link| link.id() != link_id);
}
fn remove_target_block_links(&mut self, block_id: &Uuid) {
self.links.retain(|link| link.target_block_id() != block_id);
}
fn remove_all_links(&mut self) {
self.links.clear();
}
}
impl<L: Link> BaseOutput<L> {
pub fn new_named(name: &str, kind: HaystackKind, block_id: Uuid) -> Self {
Self {
desc: OutDesc {
name: name.to_string(),
kind,
},
value: Value::default(),
links: Vec::new(),
block_id,
effective_status: Status::Ok,
}
}
pub fn new(kind: HaystackKind, block_id: Uuid) -> Self {
Self::new_named("out", kind, block_id)
}
}