rtlola_streamir/formatter/
names.rs

1//! Convenient macro to quickly define a list of name formattings for streams and windows
2
3use crate::ir::{InputReference, OutputReference, StreamReference, WindowReference};
4
5/// Defines how the names of streams and windows are represented in the target language
6pub trait GetStreamName {
7    /// Returns the name of the given stream in the target language
8    fn stream_name(&self, sr: StreamReference) -> String;
9
10    /// Returns the name of the given input stream in the target language
11    fn input_name(&self, sr: InputReference) -> String {
12        self.stream_name(StreamReference::In(sr))
13    }
14
15    /// Returns the name of the given output stream in the target language
16    fn output_name(&self, sr: OutputReference) -> String {
17        self.stream_name(sr.sr())
18    }
19
20    /// Returns the name of the given window in the target language
21    fn window_name(&self, sref: WindowReference) -> String;
22}
23
24#[macro_export]
25/// Mapping of argument names to references
26macro_rules! arg_ty {
27    (window) => {
28        $crate::ir::WindowReference
29    };
30    (stream) => {
31        $crate::ir::StreamReference
32    };
33    (num) => {
34        usize
35    };
36}
37
38#[macro_export]
39/// Returns the text representation of the reference
40macro_rules! arg_text {
41    (stream, $self:expr, $stream:expr) => {
42        $self.stream_name($stream)
43    };
44    (window, $self:expr, $window:expr) => {
45        $self.window_name($window)
46    };
47    (num, $self:expr, $num:expr) => {
48        $num.to_string()
49    };
50}
51
52#[macro_export]
53/// Allows for easy definition of function names based on streams and windows
54macro_rules! function_names {
55	($self:ty, $($(#[$($attrss:tt)*])* $name:ident($($arg:ident),*): $format_str:literal),*) => {
56        use $crate::{arg_text, arg_ty};
57		impl $self {
58			$(
59				$(#[$($attrss)*])*
60				pub(crate) fn $name(&self, $($arg: arg_ty!($arg)),*) -> String {
61                    use $crate::formatter::names::GetStreamName;
62					$(
63                        let $arg = arg_text!($arg, self, $arg);
64                    )*
65					format!($format_str, $($arg=$arg),*)
66				}
67			)*
68		}
69    }
70}