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
#![recursion_limit = "512"]
extern crate proc_macro;
mod codegen;
#[cfg(feature = "graphviz")]
mod diagramgen;
mod parser;
use syn::parse_macro_input;
#[proc_macro]
pub fn statemachine(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as parser::StateMachine);
match parser::ParsedStateMachine::new(input) {
Ok(sm) => {
#[cfg(feature = "graphviz")]
{
use std::io::Write;
let diagram = diagramgen::generate_diagram(&sm);
let mut process = std::process::Command::new("dot")
.args(&["-Tsvg", "-o", "statemachine.svg"])
.stdin(std::process::Stdio::piped())
.spawn()
.expect("Failed to execute 'dot'. Are you sure graphviz is installed?");
process
.stdin
.as_mut()
.map(|s| s.write_all(diagram.as_bytes()));
match process.wait() {
Ok(status) => {
if !status.success() {
panic!("'dot' failed to run. Are you sure graphviz is installed?");
}
}
Err(_) => panic!("'dot' failed to run. Are you sure graphviz is installed?"),
}
}
codegen::generate_code(&sm).into()
}
Err(error) => error.to_compile_error().into(),
}
}