-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefinition.rs
More file actions
99 lines (88 loc) · 3.32 KB
/
Copy pathdefinition.rs
File metadata and controls
99 lines (88 loc) · 3.32 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
use crate::formatter::{info, success};
use crate::parser::Parser;
use colored::Colorize;
pub fn search_definition(name: String, path: Option<String>) {
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);
}
fn search_and_display_definitions(search_name: &str, parser: &Parser) {
let mut found_any = false;
let mut total_matches = 0;
info(format!("Searching for '{}'", search_name));
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;
}
info(String::from("Found flow_type:\n"));
match serde_json::to_string_pretty(flow_type) {
Ok(json) => {
let mut index = 0;
for line in json.lines() {
index += 1;
println!("{} {}", format!("{index}:"), line.bright_cyan());
}
}
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;
}
info(String::from("Found data_type:\n"));
match serde_json::to_string_pretty(data_type) {
Ok(json) => {
let mut index = 0;
for line in json.lines() {
index += 1;
println!("{} {}", format!("{index}:"), line.bright_cyan());
}
}
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;
}
info(String::from("Found runtime_function_definition:\n"));
match serde_json::to_string_pretty(runtime_func) {
Ok(json) => {
let mut index = 0;
for line in json.lines() {
index += 1;
println!("{} {}", format!("{index}:"), line.bright_cyan());
}
}
Err(_) => println!("{}", "Error serializing RuntimeFunction".red()),
}
}
}
}
if !found_any {
println!(
"{}",
format!("\n{}: {}", "error".red(), "Found no matching definition(s)")
);
} else {
success(format!("Found {total_matches} matching definition(s)"))
}
}