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
use crate::{error, Result}; use indexmap::IndexMap; use snafu::{OptionExt, ResultExt}; use std::io; use xio_instructionset::{Instruction, InstructionMap}; use xio_jobset::Job; pub trait WriteJob { fn write_job( &mut self, job: &Job, instructions: &InstructionMap, ) -> Result<()>; } impl<W: io::Write> WriteJob for W { fn write_job( &mut self, job: &Job, instructions: &InstructionMap, ) -> Result<()> { writeln!(self, "blockdiag {{").context(error::Io {})?; writeln!(self, " node_width=300").context(error::Io {})?; writeln!(self, " node_height=60").context(error::Io {})?; writeln!(self, " start[label=\"Start\",shape=\"beginpoint\"];") .context(error::Io {})?; let mut source_nodes = vec!["start".to_string()]; let mut source_label = ""; for (i, command) in job.commands.iter().enumerate() { let command_name = format!("command_{}_{}", i, command.command_type); for node in source_nodes.drain(..) { writeln!( self, " {} -> {} [folded, label=\"{}\"];", node, command_name, source_label ) .context(error::Io {})?; } let instruction = instructions .get(&command.command_type) .context(error::XioInstructionNotFound { name: command.command_type.to_string(), })?; let instruction_string = format_instruction(instruction, &command.parameters)?; let node_label = if instruction_string.is_empty() { format!( "{}\n[{}]", command.command_type, command.description.replace('"', "\\\"") ) } else { format!( "{}\\n[{}]\\n{}", command.command_type, command.description.replace('"', "\\\""), instruction_string ) }; let node_color = if command.conditions.is_empty() { "#ccccff" } else { "#ffff77" }; writeln!( self, " {}[label=\"{}\", color=\"{}\"];", command_name, node_label, node_color ) .context(error::Io {})?; let mut condition_source_node = command_name.to_string(); let mut group_nodes = vec![]; source_label = ""; let mut abort_node_name = "".to_string(); for (j, condition) in command.conditions.iter().enumerate() { let condition_name = format!( "{}_condition_{}_{}", command_name, j, condition.command_type ); let instruction = instructions .get(&condition.command_type) .context(error::XioInstructionNotFound { name: condition.command_type.to_string(), })?; let instruction_string = format_instruction( instruction, &condition.parameters, )?; let node_label = if instruction_string.is_empty() { format!( "{}\n[{}]", condition.command_type, condition.description.replace('"', "\\\"") ) } else { format!( "{}\\n[{}]\\n{}", condition.command_type, condition.description.replace('"', "\\\""), instruction_string ) }; writeln!( self, " {} -> {};", condition_source_node, condition_name ) .context(error::Io {})?; writeln!( self, " {}[label=\"{}\"];", condition_name, node_label ) .context(error::Io {})?; if condition.exit_job { abort_node_name = format!("{}_abort", command_name); writeln!( self, " {} -> {}[folded, label=\"yes\"]", condition_name, abort_node_name ) .context(error::Io {})?; } else { source_nodes.push(condition_name.to_string()); } group_nodes.push(condition_name.to_string()); condition_source_node = condition_name; source_label = "yes"; } if !abort_node_name.is_empty() { writeln!( self, " {}[label=\"abort\", color=\"#ff7777\"];", abort_node_name ) .context(error::Io {})?; group_nodes.push(abort_node_name); } if condition_source_node != command_name { writeln!( self, " {} -> {} [label=\"no\"];", condition_source_node, command_name ) .context(error::Io {})?; } if !group_nodes.is_empty() { writeln!( self, " group {{ {} }}", group_nodes.join("; ") ) .context(error::Io {})?; } if source_nodes.is_empty() { source_nodes.push(command_name); } } for node in source_nodes.drain(..) { writeln!( self, " {} -> end [folded, label=\"{}\"];", node, source_label ) .context(error::Io {})?; } writeln!(self, " end[label=\"End\",shape=\"endpoint\"];") .context(error::Io {})?; writeln!(self, "}}").context(error::Io {})?; Ok(()) } } fn format_instruction( instruction: &Instruction, parameters: &IndexMap<String, String>, ) -> Result<String> { let parameter_names = instruction .parameters .iter() .map(|param| param.id.to_string()) .collect::<Vec<_>>(); ensure!( parameter_names.len() == parameters.len(), error::XioParameterCountMismatch { names: parameter_names.len(), parameters: parameters.len(), } ); let parameters = parameter_names .into_iter() .map(|p| { parameters.get(&p).context(error::XioParameterNotFound { name: p.to_string(), }) }) .collect::<Result<Vec<_>>>()?; let mut label = instruction.formatstring.to_string(); for (i, parameter) in parameters.into_iter().enumerate() { label = label.replace(&format!("%{}", i), parameter); } Ok(label) }