logic_mesh/base/block/
props.rs

1// Copyright (c) 2022-2023, Radu Racariu.
2
3//!
4//! Defines the block properties
5//!
6
7use uuid::Uuid;
8
9use crate::base::{input::Input, link::Link, output::Output};
10
11use super::{desc::BlockDesc, BlockState};
12
13/// Defines the the Block properties
14/// that are common to all blocks.
15pub trait BlockProps {
16    /// The block's read type
17    /// This is the type used to read from the block's inputs
18    type Reader;
19    /// The block's write type
20    /// This is the type used to write to the block's outputs
21    type Writer: Clone;
22
23    /// Blocks unique id
24    fn id(&self) -> &Uuid;
25
26    /// Blocks name
27    fn name(&self) -> &str;
28
29    /// Block's static description
30    fn desc(&self) -> &'static BlockDesc;
31
32    /// Blocks state
33    fn state(&self) -> BlockState;
34
35    /// Set the blocks state
36    fn set_state(&mut self, state: BlockState) -> BlockState;
37
38    /// List all the block inputs
39    fn inputs(&self) -> Vec<&dyn Input<Reader = Self::Reader, Writer = Self::Writer>>;
40
41    /// Get block input by name
42    fn get_input(
43        &self,
44        name: &str,
45    ) -> Option<&dyn Input<Reader = Self::Reader, Writer = Self::Writer>> {
46        self.inputs().iter().find(|i| i.name() == name).cloned()
47    }
48
49    /// Get block mutable input by name
50    fn get_input_mut(
51        &mut self,
52        name: &str,
53    ) -> Option<&mut dyn Input<Reader = Self::Reader, Writer = Self::Writer>> {
54        self.inputs_mut().into_iter().find(|i| i.name() == name)
55    }
56
57    /// List all the block inputs
58    fn inputs_mut(&mut self) -> Vec<&mut dyn Input<Reader = Self::Reader, Writer = Self::Writer>>;
59
60    /// The block outputs
61    fn outputs(&self) -> Vec<&dyn Output<Writer = Self::Writer>>;
62
63    /// Get block output by name
64    fn get_output(&self, name: &str) -> Option<&dyn Output<Writer = Self::Writer>> {
65        self.outputs().iter().find(|i| i.name() == name).cloned()
66    }
67
68    /// Get block mutable output by name
69    fn get_output_mut(&mut self, name: &str) -> Option<&mut dyn Output<Writer = Self::Writer>> {
70        self.outputs_mut().into_iter().find(|i| i.name() == name)
71    }
72
73    /// Mutable reference to the block's output
74    fn outputs_mut(&mut self) -> Vec<&mut dyn Output<Writer = Self::Writer>>;
75
76    /// List all the links this block has
77    fn links(&self) -> Vec<(&str, Vec<&dyn crate::base::link::Link>)>;
78
79    /// Remove a link from the link collection
80    fn remove_link(&mut self, link: &dyn Link) {
81        self.remove_link_by_id(link.id())
82    }
83
84    /// Remove a link by its id from the link collection
85    fn remove_link_by_id(&mut self, link_id: &Uuid);
86
87    /// Remove all links from this block
88    fn remove_all_links(&mut self);
89}