logic_mesh/base/link/
base.rs

1// Copyright (c) 2022-2023, Radu Racariu.
2
3//!
4//! Defines the base link type.
5//!
6
7use uuid::Uuid;
8
9use super::{Link, LinkState};
10
11/// Base Link that uses an abstract
12/// optional transmitter type `Tx`.
13///
14/// Links connect a block output to another block's input.
15/// Or, a block input to another block's input.
16/// A block output can have multiple links to multiple block inputs.
17/// A block input can have multiple links to multiple block inputs.
18#[derive(Debug, Default, Clone, PartialEq)]
19pub struct BaseLink<Tx> {
20    /// Unique link id
21    pub id: Uuid,
22    /// The target block id
23    pub target_block_id: Uuid,
24    /// The target input name
25    pub target_input: String,
26    /// Optional transmitter type
27    pub tx: Option<Tx>,
28    /// The current link state
29    pub state: LinkState,
30}
31
32impl<Tx: Clone> Link for BaseLink<Tx> {
33    fn id(&self) -> &Uuid {
34        &self.id
35    }
36
37    fn target_block_id(&self) -> &Uuid {
38        &self.target_block_id
39    }
40
41    fn target_input(&self) -> &str {
42        &self.target_input
43    }
44
45    fn state(&self) -> LinkState {
46        self.state
47    }
48}
49
50impl<Tx> BaseLink<Tx> {
51    pub fn new(target_block_id: Uuid, target_input: String) -> Self {
52        Self {
53            id: Uuid::new_v4(),
54            target_block_id,
55            target_input,
56            tx: None,
57            state: LinkState::Disconnected,
58        }
59    }
60}