easyfix_messages_gen/
lib.rs1mod r#gen;
2
3use std::{
4 collections::HashMap,
5 error::Error,
6 fs,
7 io::prelude::*,
8 path::Path,
9 process::{Command, Stdio},
10 time::Instant,
11};
12
13pub use easyfix_dictionary as dictionary;
14use easyfix_dictionary::{Dictionary, ParseRejectReason};
15use proc_macro2::TokenStream;
16
17use crate::r#gen::Generator;
18
19fn create_source_file(
20 tokens_stream: TokenStream,
21 source_file: impl AsRef<Path>,
22) -> Result<(), Box<dyn Error + 'static>> {
23 let code = tokens_stream.to_string();
24
25 let output = if true {
26 let start = Instant::now();
27 #[expect(clippy::zombie_processes)]
28 let mut rustfmt = Command::new("rustfmt")
29 .stdin(Stdio::piped())
30 .stdout(Stdio::piped())
31 .stderr(Stdio::piped())
32 .spawn()
33 .expect("failed to run rustfmt");
34 rustfmt.stdin.take().unwrap().write_all(code.as_bytes())?;
35 let output = rustfmt.wait_with_output()?;
36 eprintln!("rustfmt output status: {:?}", output.status);
37 let now = Instant::now() - start;
38 eprintln!(
39 "rustfmt done after {}.{}",
40 now.as_secs(),
41 now.subsec_millis()
42 );
43 if output.status.success() {
44 output.stdout
45 } else {
46 std::io::stdout().write_all(&output.stdout)?;
47 std::io::stderr().write_all(&output.stderr)?;
48 std::process::exit(output.status.code().unwrap_or(1));
49 }
50 } else {
51 code.into_bytes()
52 };
53
54 let mut file = fs::File::create(source_file.as_ref())?;
55 file.write_all(&output)?;
56 eprintln!(
57 "{}: {} bytes written",
58 source_file.as_ref().display(),
59 output.len()
60 );
61
62 Ok(())
63}
64
65fn log_duration<T>(msg: &str, action: impl FnOnce() -> T) -> T {
66 let start = Instant::now();
67 let result = action();
68 let now = Instant::now() - start;
69 eprintln!("{msg} after {}.{}", now.as_secs(), now.subsec_millis());
70 result
71}
72
73pub fn generate_fix_messages(
74 fixt_xml_path: Option<impl AsRef<Path>>,
75 fix_xml_path: impl AsRef<Path>,
76 fields_file: impl AsRef<Path>,
77 groups_file: impl AsRef<Path>,
78 messages_file: impl AsRef<Path>,
79 reject_reason_overrides: Option<HashMap<ParseRejectReason, String>>,
80) -> std::result::Result<(), Box<dyn std::error::Error + 'static>> {
81 eprintln!("fields file path: {}", fields_file.as_ref().display());
82 eprintln!("groups file path: {}", groups_file.as_ref().display());
83 eprintln!("messages file path: {}", messages_file.as_ref().display());
84 let fix_xml = fs::read_to_string(fix_xml_path)?;
85 let mut dictionary = Dictionary::new(reject_reason_overrides);
86
87 if let Some(some_fixt_xml_path) = fixt_xml_path {
88 log_duration("FIXT XML processed", || {
89 let fixt_xml = fs::read_to_string(some_fixt_xml_path)?;
90 dictionary.process_fixt_xml(&fixt_xml)
91 })?;
92
93 log_duration("FIX XML processed", || dictionary.process_fix_xml(&fix_xml))?;
94 } else {
95 log_duration("FIX legacy XML processed", || {
96 dictionary.process_legacy_fix_xml(&fix_xml)
97 })?;
98 }
99
100 let generator = log_duration("Generator ready", || Generator::new(&dictionary));
101
102 create_source_file(
103 log_duration("Fields token stream", || generator.generate_fields()),
104 fields_file,
105 )?;
106 create_source_file(
107 log_duration("Groups token stream", || generator.generate_groups()),
108 groups_file,
109 )?;
110 create_source_file(
111 log_duration("Messages token stream", || generator.generate_messages()),
112 messages_file,
113 )?;
114
115 Ok(())
116}