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
mod gen;
use crate::gen::Generator;
use easyfix_dictionary::Dictionary;
use proc_macro2::TokenStream;
use std::{
error::Error,
fs,
io::prelude::*,
path::Path,
process::{Command, Stdio},
time::Instant,
};
fn create_source_file(
tokens_stream: TokenStream,
source_file: impl AsRef<Path>,
) -> Result<(), Box<dyn Error + 'static>> {
let code = tokens_stream.to_string();
let output = if true {
let start = Instant::now();
let mut rustfmt = Command::new("rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to run rustfmt");
rustfmt.stdin.take().unwrap().write_all(code.as_bytes())?;
let output = rustfmt.wait_with_output()?;
eprintln!("rustfmt output status: {:?}", output.status);
let now = Instant::now() - start;
eprintln!(
"rustfmt done after {}.{}",
now.as_secs(),
now.subsec_millis()
);
if output.status.success() {
output.stdout
} else {
std::io::stdout().write_all(&output.stdout)?;
std::io::stderr().write_all(&output.stderr)?;
std::process::exit(output.status.code().unwrap_or(1));
}
} else {
code.into_bytes()
};
let mut file = fs::File::create(source_file.as_ref())?;
file.write_all(&output)?;
eprintln!(
"{}: {} bytes written",
source_file.as_ref().display(),
output.len()
);
Ok(())
}
fn log_duration<T>(msg: &str, action: impl FnOnce() -> T) -> T {
let start = Instant::now();
let result = action();
let now = Instant::now() - start;
eprintln!("{msg} after {}.{}", now.as_secs(), now.subsec_millis());
result
}
pub fn generate_fix_messages(
fixt_xml_path: impl AsRef<Path>,
fix_xml_path: impl AsRef<Path>,
fields_file: impl AsRef<Path>,
groups_file: impl AsRef<Path>,
messages_file: impl AsRef<Path>,
) -> std::result::Result<(), Box<dyn std::error::Error + 'static>> {
eprintln!("fields file path: {}", fields_file.as_ref().display());
eprintln!("groups file path: {}", groups_file.as_ref().display());
eprintln!("messages file path: {}", messages_file.as_ref().display());
let fixt_xml = fs::read_to_string(fixt_xml_path)?;
let fix_xml = fs::read_to_string(fix_xml_path)?;
let mut dictionary = Dictionary::new();
log_duration("FIXT XML processed", || {
dictionary.process_fixt_xml(&fixt_xml)
})?;
log_duration("FIXT XML processed", || {
dictionary.process_fix_xml(&fix_xml)
})?;
let generator = log_duration("Generator ready", || Generator::new(&dictionary));
create_source_file(
log_duration("Fields token stream", || generator.generate_fields()),
fields_file,
)?;
create_source_file(
log_duration("Groups token stream", || generator.generate_groups()),
groups_file,
)?;
create_source_file(
log_duration("Messages token stream", || generator.generate_messages()),
messages_file,
)?;
Ok(())
}