workflow/
visualize.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//  VISUALIZE.rs
//    by Lut99
//
//  Created:
//    31 Oct 2023, 14:30:00
//  Last edited:
//    13 Dec 2023, 08:44:28
//  Auto updated?
//    Yes
//
//  Description:
//!   Implements a simple traversal over the [`Workflow`] to print it
//!   neatly to some writer.
//

use std::fmt::{Display, Formatter, Result as FResult};

use super::spec::{Elem, ElemBranch, ElemCommit, ElemLoop, ElemParallel, ElemTask, Workflow};

/***** HELPER MACROS *****/
/// Prints a given iterator somewhat nicely to a string.
macro_rules! write_iter {
    ($iter:expr, $conn:literal) => {{
        let mut iter = $iter.peekable();
        format!(
            "{}",
            if let Some(first) = iter.next() {
                if iter.peek().is_some() {
                    format!(concat!("{}", $conn, "{}"), first, iter.collect::<Vec<String>>().join($conn))
                } else {
                    format!("{}", first)
                }
            } else {
                String::from("<none>")
            }
        )
    }};
}

/***** HELPER FUNCTIONS *****/
/// Writes an [`Elem`] to the given formatter.
///
/// # Arguments
/// - `funcs`: A map of function IDs to metadata.
/// - `f`: The [`Formatter`] to write to.
/// - `elem`: The [`Elem`] to write.
/// - `prefix`: Some prefix to write before every line.
///
/// # Errors
/// This function only errors if we failed to write to the given `f`.
fn print_elem(f: &mut Formatter, elem: &Elem, prefix: &dyn Display) -> FResult {
    // Print the element
    match elem {
        Elem::Task(ElemTask { id, name, package, version, input, output, location, metadata, next }) => {
            writeln!(f, "{prefix}task")?;
            writeln!(f, "{prefix}  - id : {id}")?;
            writeln!(f, "{prefix}")?;
            writeln!(f, "{prefix}  - name    : {name}")?;
            writeln!(f, "{prefix}  - package : {package}")?;
            writeln!(f, "{prefix}  - version : {version}")?;
            writeln!(f, "{prefix}")?;
            writeln!(f, "{prefix}  - input  : {}", write_iter!(input.iter().map(|data| format!("'{}'", data.name)), " or "))?;
            writeln!(
                f,
                "{}  - output : {}",
                prefix,
                if let Some(output) = &output { format!("'{}'", output.name.as_str()) } else { "<none>".into() }
            )?;
            writeln!(f, "{prefix}")?;
            writeln!(f, "{prefix}  - location  : {}", if let Some(location) = &location { location.as_str() } else { "<unplanned>" })?;
            writeln!(
                f,
                "{}  - metadata  : {}",
                prefix,
                write_iter!(
                    metadata.iter().map(|metadata| format!(
                        "#{}.{}{}",
                        metadata.owner,
                        metadata.tag,
                        if let Some((assigner, signature)) = &metadata.signature { format!("#{assigner}:{signature}") } else { String::new() }
                    )),
                    ", "
                )
            )?;

            // Do next
            print_elem(f, next, prefix)
        },

        Elem::Branch(ElemBranch { branches, next }) => {
            writeln!(f, "{prefix}branch")?;

            // Write the branches
            for (i, branch) in branches.iter().enumerate() {
                writeln!(f, "{prefix}{}<branch{}>", Indent(4), i)?;
                print_elem(f, branch, &Pair(prefix, Indent(8)))?;
                writeln!(f, "{prefix}")?;
            }

            // Do next
            print_elem(f, next, prefix)
        },
        Elem::Parallel(ElemParallel { branches, merge, next }) => {
            writeln!(f, "{prefix}parallel")?;
            writeln!(f, "{prefix}  - merge strategy : {merge:?}")?;

            // Write the branches
            for (i, branch) in branches.iter().enumerate() {
                writeln!(f, "{prefix}{}<branch{}>", Indent(4), i)?;
                print_elem(f, branch, &Pair(prefix, Indent(8)))?;
                writeln!(f, "{prefix}")?;
            }

            // Do next
            print_elem(f, next, prefix)
        },
        Elem::Loop(ElemLoop { body, next }) => {
            writeln!(f, "{prefix}loop")?;
            writeln!(f, "{}<repeated>", Pair(prefix, Indent(4)))?;
            print_elem(f, body, &Pair(prefix, Indent(8)))?;
            writeln!(f)?;

            // Do next
            print_elem(f, next, prefix)
        },
        Elem::Commit(ElemCommit { id, data_name, location, input, next }) => {
            writeln!(f, "{prefix}commit <{} as '{}'>", write_iter!(input.iter().map(|data| format!("'{}'", data.name)), " or "), data_name)?;
            writeln!(f, "{prefix}  - id   : {id}")?;
            for i in input {
                if let Some(from) = &i.from {
                    writeln!(f, "{prefix}  - from : '{}' <- '{}'", i.name, from)?;
                }
            }
            if let Some(location) = location {
                writeln!(f, "{prefix}  - to   : {location}")?;
            }

            // Do next
            print_elem(f, next, prefix)
        },

        Elem::Next => {
            writeln!(f, "{}next", prefix)
        },
        Elem::Stop(returns) => {
            writeln!(
                f,
                "{}stop{}",
                prefix,
                if !returns.is_empty() {
                    format!(" <returns {}>", write_iter!(returns.iter().map(|data| format!("'{}'", data.name)), " or "))
                } else {
                    String::new()
                }
            )
        },
    }
}

/***** HELPERS *****/
/// Writes two display things successively.
struct Pair<D1, D2>(D1, D2);
impl<D1: Display, D2: Display> Display for Pair<D1, D2> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "{}{}", self.0, self.1) }
}

/// Generates indentation of the asked size.
struct Indent(usize);
impl Display for Indent {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        for _ in 0..self.0 {
            write!(f, " ")?;
        }
        Ok(())
    }
}

/***** FORMATTERS *****/
/// Capable of printing the [`Workflow`] to some writer.
#[derive(Debug)]
pub struct WorkflowFormatter<'w> {
    /// The workflow to format.
    wf: &'w Workflow,
}
impl<'w> Display for WorkflowFormatter<'w> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        // Print some nice header thingy
        writeln!(f, "Workflow [")?;

        // Print global metadata
        if !self.wf.metadata.is_empty() {
            writeln!(
                f,
                "{}{}",
                Indent(4),
                write_iter!(
                    self.wf.metadata.iter().map(|metadata| format!(
                        "#{}.{}{}",
                        metadata.owner,
                        metadata.tag,
                        if let Some((assigner, signature)) = &metadata.signature { format!("#{assigner}:{signature}") } else { String::new() }
                    )),
                    ", "
                )
            )?;
            writeln!(f)?;
        }

        // Alright print the main elements
        print_elem(f, &self.wf.start, &Indent(4))?;

        // Finish with the end bracket
        write!(f, "]")
    }
}

impl Workflow {
    /// Returns a nice formatter that visualizes the workflow more easily understandable than its [`Debug`](std::fmt::Debug)-implementation.
    ///
    /// # Returns
    /// A new [`WorkflowFormatter`] that can visualize the workflow when its [`Display`]-implementation is called.
    #[inline]
    pub fn visualize(&self) -> WorkflowFormatter { WorkflowFormatter { wf: self } }
}