|
| 1 | +use code0_definition_reader::parser::Parser; |
| 2 | +use colored::Colorize; |
| 3 | + |
| 4 | +pub fn search_definition(name: String, path: Option<String>) { |
| 5 | + let dir_path = path.unwrap_or_else(|| "./definitions".to_string()); |
| 6 | + |
| 7 | + let parser = match Parser::from_path(dir_path.as_str()) { |
| 8 | + Some(reader) => reader, |
| 9 | + None => { |
| 10 | + panic!("Error reading definitions"); |
| 11 | + } |
| 12 | + }; |
| 13 | + |
| 14 | + search_and_display_definitions(&name, &parser); |
| 15 | +} |
| 16 | + |
| 17 | +fn search_and_display_definitions(search_name: &str, parser: &Parser) { |
| 18 | + let mut found_any = false; |
| 19 | + let mut total_matches = 0; |
| 20 | + println!( |
| 21 | + "{}", |
| 22 | + format!("Searching for definitions matching: '{search_name}'") |
| 23 | + .bright_yellow() |
| 24 | + .bold() |
| 25 | + ); |
| 26 | + println!("{}", "─".repeat(60).dimmed()); |
| 27 | + |
| 28 | + for feature in &parser.features { |
| 29 | + // Search FlowTypes |
| 30 | + for flow_type in &feature.flow_types { |
| 31 | + if flow_type.identifier == search_name { |
| 32 | + total_matches += 1; |
| 33 | + if !found_any { |
| 34 | + found_any = true; |
| 35 | + } |
| 36 | + |
| 37 | + println!("\n{}", "FlowType".bright_cyan().bold()); |
| 38 | + match serde_json::to_string_pretty(flow_type) { |
| 39 | + Ok(json) => { |
| 40 | + for line in json.lines() { |
| 41 | + println!("{}", line.bright_green()); |
| 42 | + } |
| 43 | + } |
| 44 | + Err(_) => println!("{}", "Error serializing FlowType".red()), |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Search DataTypes |
| 50 | + for data_type in &feature.data_types { |
| 51 | + if data_type.identifier == search_name { |
| 52 | + total_matches += 1; |
| 53 | + if !found_any { |
| 54 | + found_any = true; |
| 55 | + } |
| 56 | + |
| 57 | + println!("\n{}", "DataType".bright_cyan().bold()); |
| 58 | + match serde_json::to_string_pretty(data_type) { |
| 59 | + Ok(json) => { |
| 60 | + for line in json.lines() { |
| 61 | + println!("{}", line.bright_green()); |
| 62 | + } |
| 63 | + } |
| 64 | + Err(_) => println!("{}", "Error serializing DataType".red()), |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Search RuntimeFunctions |
| 70 | + for runtime_func in &feature.runtime_functions { |
| 71 | + if runtime_func.runtime_name == search_name { |
| 72 | + total_matches += 1; |
| 73 | + if !found_any { |
| 74 | + found_any = true; |
| 75 | + } |
| 76 | + |
| 77 | + println!("\n{}", "RuntimeFunction".bright_cyan().bold()); |
| 78 | + match serde_json::to_string_pretty(runtime_func) { |
| 79 | + Ok(json) => { |
| 80 | + let mut index = 0; |
| 81 | + for line in json.lines() { |
| 82 | + index += 1; |
| 83 | + println!( |
| 84 | + "{} {}", |
| 85 | + format!("{index}:").bright_blue(), |
| 86 | + line.bright_green() |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + Err(_) => println!("{}", "Error serializing RuntimeFunction".red()), |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if !found_any { |
| 97 | + println!( |
| 98 | + "\n{}", |
| 99 | + format!("No definitions found matching '{search_name}'") |
| 100 | + .red() |
| 101 | + .bold() |
| 102 | + ); |
| 103 | + } else { |
| 104 | + println!("\n{}", "─".repeat(60).dimmed()); |
| 105 | + println!( |
| 106 | + "{}", |
| 107 | + format!("Found {total_matches} matching definition(s)").bright_yellow() |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments