protoflow_core/
block_descriptor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This is free and unencumbered software released into the public domain.

use crate::{
    prelude::{vec, MaybeLabeled, MaybeNamed, Vec},
    ParameterDescriptor, PortDescriptor,
};

/// A block is an autonomous unit of computation in a system.
pub trait BlockDescriptor: AsBlockDescriptor + MaybeNamed + MaybeLabeled {
    /// A description of this block's I/O ports.
    fn ports(&self) -> Vec<PortDescriptor> {
        let mut result = self.inputs();
        result.append(&mut self.outputs());
        result
    }

    /// A description of this block's input ports.
    fn inputs(&self) -> Vec<PortDescriptor> {
        vec![]
    }

    /// A description of this block's output ports.
    fn outputs(&self) -> Vec<PortDescriptor> {
        vec![]
    }

    /// A description of this block's parameters.
    fn parameters(&self) -> Vec<ParameterDescriptor> {
        vec![]
    }
}

pub trait AsBlockDescriptor {
    fn as_block_descriptor(&self) -> &dyn BlockDescriptor;
}

impl<T: BlockDescriptor + Sized> AsBlockDescriptor for T {
    fn as_block_descriptor(&self) -> &dyn BlockDescriptor {
        self
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for &dyn BlockDescriptor {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("BlockDescriptor", 5)?;
        state.serialize_field("name", &self.name())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("parameters", &self.parameters())?;
        state.serialize_field("inputs", &self.inputs())?;
        state.serialize_field("outputs", &self.outputs())?;
        state.end()
    }
}