Skip to main content

tydi/design/
streamlet.rs

1//! This module contains the Streamlet structure.
2//!
3//! A streamlet is a component where every [Interface] has a [LogicalStreamType].
4
5use crate::logical::LogicalStreamType;
6use crate::traits::Identify;
7use crate::util::UniquelyNamedBuilder;
8use crate::{Error, Name, Result};
9use std::convert::TryInto;
10use std::str::FromStr;
11
12/// Streamlet interface definition.
13#[derive(Clone, Debug, PartialEq)]
14pub struct Streamlet {
15    name: Name,
16    interfaces: Vec<Interface>,
17}
18
19impl Streamlet {
20    pub fn interfaces(&self) -> Vec<Interface> {
21        self.interfaces.clone()
22    }
23
24    pub fn from_builder(name: Name, builder: UniquelyNamedBuilder<Interface>) -> Result<Self> {
25        Ok(Streamlet {
26            name,
27            interfaces: builder.finish()?,
28        })
29    }
30}
31
32impl Identify for Streamlet {
33    fn identifier(&self) -> &str {
34        self.name.as_ref()
35    }
36}
37
38/// Streamlet interface mode.
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub enum Mode {
41    /// The interface is an output of the streamlet.
42    Out,
43    /// The interface is an input of the streamlet.
44    In,
45}
46
47impl FromStr for Mode {
48    type Err = Error;
49
50    fn from_str(input: &str) -> Result<Self> {
51        match input {
52            "in" => Ok(Mode::In),
53            "out" => Ok(Mode::Out),
54            _ => Err(Error::InvalidArgument(format!(
55                "{} is not a valid interface Mode. Expected \"in\" or \"out\"",
56                input
57            ))),
58        }
59    }
60}
61
62#[derive(Clone, Debug, PartialEq)]
63pub struct Interface {
64    name: Name,
65    mode: Mode,
66    typ: LogicalStreamType,
67}
68
69impl Interface {
70    pub fn mode(&self) -> Mode {
71        self.mode
72    }
73
74    pub fn typ(&self) -> LogicalStreamType {
75        self.typ.clone()
76    }
77}
78
79impl crate::traits::Identify for Interface {
80    fn identifier(&self) -> &str {
81        self.name.as_ref()
82    }
83}
84
85type BoxedStdError = Box<dyn std::error::Error>;
86
87impl Interface {
88    pub fn new(name: impl Into<Name>, mode: Mode, typ: impl Into<LogicalStreamType>) -> Self {
89        Interface {
90            name: name.into(),
91            mode,
92            typ: typ.into(),
93        }
94    }
95    pub fn try_new(
96        name: impl TryInto<Name, Error = impl Into<BoxedStdError>>,
97        mode: Mode,
98        typ: impl TryInto<LogicalStreamType, Error = impl Into<BoxedStdError>>,
99    ) -> Result<Self> {
100        Ok(Interface {
101            name: name.try_into().map_err(Into::into)?,
102            mode,
103            typ: typ.try_into().map_err(Into::into)?,
104        })
105    }
106}