logic_mesh/base/input/props.rs
1// Copyright (c) 2022-2023, Radu Racariu.
2
3//!
4//! Defines the block input properties trait
5//!
6
7use libhaystack::val::{kind::HaystackKind, Value};
8use uuid::Uuid;
9
10use crate::base::link::{BaseLink, Link};
11
12/// A default set of values for an `Input`
13#[derive(Debug, Default)]
14pub struct InputDefault {
15 /// The default value
16 pub val: Value,
17 /// The default minimum value
18 pub min: Value,
19 /// The default maximum value
20 pub max: Value,
21}
22
23/// Defines the basic properties of a Block Input
24pub trait InputProps {
25 /// The input's read type
26 type Reader;
27 /// The input's write type
28 type Writer: Clone;
29
30 /// The input's name
31 fn name(&self) -> &str;
32
33 /// The kind of data this input can receive
34 fn kind(&self) -> &HaystackKind;
35
36 /// The block id of the block this input belongs to
37 fn block_id(&self) -> &Uuid;
38
39 /// True if this input is connected to at least one output or input
40 /// of another block
41 fn is_connected(&self) -> bool;
42
43 /// Get a list of links to this output
44 fn links(&self) -> Vec<&dyn Link>;
45
46 /// True if this input has at least one output
47 fn has_output(&self) -> bool {
48 !self.links().is_empty()
49 }
50
51 /// Adds a link to this output
52 fn add_link(&mut self, link: BaseLink<Self::Writer>);
53
54 /// Remove a link from this input
55 /// # Arguments
56 /// - link: The link to be removed
57 fn remove_link(&mut self, link: &dyn Link) {
58 self.remove_link_by_id(link.id())
59 }
60
61 /// Remove a link by id from this input
62 /// # Arguments
63 /// - link_id: The id of the link to be removed
64 fn remove_link_by_id(&mut self, link_id: &Uuid);
65
66 /// Remove all links to a specific block from this input
67 fn remove_target_block_links(&mut self, block_id: &Uuid);
68
69 /// Remove all links from this input
70 fn remove_all_links(&mut self);
71
72 /// This input's defaults
73 fn default(&self) -> &InputDefault;
74
75 /// Get a reference to this input reader type
76 fn reader(&mut self) -> &mut Self::Reader;
77
78 /// Get a reference to this input writer type
79 fn writer(&mut self) -> &mut Self::Writer;
80
81 /// Gets this input value
82 fn get_value(&self) -> Option<&Value>;
83
84 /// Increment the connection count when this input
85 /// is linked to another block's output.
86 fn increment_conn(&mut self) -> usize;
87
88 /// Decrement the connection count when the link
89 /// to another block output is removed.
90 fn decrement_conn(&mut self) -> usize;
91}