-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
268 lines (243 loc) · 8.66 KB
/
Copy pathmain.rs
File metadata and controls
268 lines (243 loc) · 8.66 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use crate::table::*;
use clap::{Parser as ClapParser, Subcommand};
use colored::*;
use notify::{Event, EventKind, RecursiveMode, Watcher, recommended_watcher};
use reader::parser::Parser;
use std::sync::mpsc::channel;
mod table;
/// Top-level CLI for 'definition'
#[derive(ClapParser)]
#[command(name = "definition")]
#[command(version = "1.0")]
#[command(about = "Manage definitions, reports, and features")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Generate a general report.
Report {
/// Optional path to root directory of all definitions.
#[arg(short, long)]
path: Option<String>,
},
/// Generate a report for a or all feature(s).
Feature {
/// Optional name of the definition set.
#[arg(short, long)]
name: Option<String>,
/// Optional path to root directory of all definitions.
#[arg(short, long)]
path: Option<String>,
},
/// Look up a specific definition.
Definition {
/// Required name of the definition.
#[arg(short, long)]
name: String,
/// Optional path to root directory of all definitions.
#[arg(short, long)]
path: Option<String>,
},
/// Watch for changes to and regenerate error reports.
Watch {
/// Optional path to root directory of all definitions.
#[arg(short, long)]
path: Option<String>,
},
}
fn main() {
let cli = Cli::parse();
match cli.command {
Commands::Report { path } => {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
let parser = match Parser::from_path(dir_path.as_str()) {
Some(reader) => reader,
None => {
panic!("Error reading definitions");
}
};
error_table(&parser.features);
summary_table(&parser.features);
}
Commands::Feature { name, path } => {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
let parser = match Parser::from_path(dir_path.as_str()) {
Some(reader) => reader,
None => {
panic!("Error reading definitions");
}
};
if let Some(feature_name) = name {
let mut features_to_report = Vec::new();
for feature in &parser.features {
if feature.name == feature_name {
feature_table(feature);
features_to_report.push(feature.clone());
}
}
summary_table(&features_to_report);
} else {
for feature in &parser.features {
feature_table(feature);
}
summary_table(&parser.features);
}
}
Commands::Definition { name, path } => {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
let parser = match Parser::from_path(dir_path.as_str()) {
Some(reader) => reader,
None => {
panic!("Error reading definitions");
}
};
search_and_display_definitions(&name, &parser);
}
Commands::Watch { path } => {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
println!(
"{}",
format!("Watching directory: {dir_path}")
.bright_yellow()
.bold()
);
println!("{}", "Press Ctrl+C to stop watching...".dimmed());
{
let parser = match Parser::from_path(dir_path.as_str()) {
Some(reader) => reader,
None => {
panic!("Error reading definitions");
}
};
error_table(&parser.features);
}
// Set up file watcher
let (tx, rx) = channel();
let mut watcher = recommended_watcher(tx).unwrap();
watcher
.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive)
.unwrap();
loop {
match rx.recv() {
Ok(event) => match event {
Ok(Event {
kind: EventKind::Create(_),
..
})
| Ok(Event {
kind: EventKind::Modify(_),
..
})
| Ok(Event {
kind: EventKind::Remove(_),
..
}) => {
println!(
"\n{}",
"Change detected! Regenerating report...".bright_yellow()
);
let parser = match Parser::from_path(dir_path.as_str()) {
Some(reader) => reader,
None => {
panic!("Error reading definitions");
}
};
error_table(&parser.features);
}
_ => {}
},
Err(e) => println!("Watch error: {e:?}"),
}
}
}
}
}
fn search_and_display_definitions(search_name: &str, parser: &Parser) {
let mut found_any = false;
let mut total_matches = 0;
println!(
"{}",
format!("Searching for definitions matching: '{search_name}'")
.bright_yellow()
.bold()
);
println!("{}", "─".repeat(60).dimmed());
for feature in &parser.features {
// Search FlowTypes
for flow_type in &feature.flow_types {
if flow_type.identifier == search_name {
total_matches += 1;
if !found_any {
found_any = true;
}
println!("\n{}", "FlowType".bright_cyan().bold());
match serde_json::to_string_pretty(flow_type) {
Ok(json) => {
for line in json.lines() {
println!("{}", line.bright_green());
}
}
Err(_) => println!("{}", "Error serializing FlowType".red()),
}
}
}
// Search DataTypes
for data_type in &feature.data_types {
if data_type.identifier == search_name {
total_matches += 1;
if !found_any {
found_any = true;
}
println!("\n{}", "DataType".bright_cyan().bold());
match serde_json::to_string_pretty(data_type) {
Ok(json) => {
for line in json.lines() {
println!("{}", line.bright_green());
}
}
Err(_) => println!("{}", "Error serializing DataType".red()),
}
}
}
// Search RuntimeFunctions
for runtime_func in &feature.runtime_functions {
if runtime_func.runtime_name == search_name {
total_matches += 1;
if !found_any {
found_any = true;
}
println!("\n{}", "RuntimeFunction".bright_cyan().bold());
match serde_json::to_string_pretty(runtime_func) {
Ok(json) => {
let mut index = 0;
for line in json.lines() {
index += 1;
println!(
"{} {}",
format!("{index}:").bright_blue(),
line.bright_green()
);
}
}
Err(_) => println!("{}", "Error serializing RuntimeFunction".red()),
}
}
}
}
if !found_any {
println!(
"\n{}",
format!("No definitions found matching '{search_name}'")
.red()
.bold()
);
} else {
println!("\n{}", "─".repeat(60).dimmed());
println!(
"{}",
format!("Found {total_matches} matching definition(s)").bright_yellow()
);
}
}