11use clap:: { Parser as ClapParser , Subcommand } ;
22use reader:: parser:: Parser ;
3- use reader:: parser:: DefinitionError ;
4- use reader:: parser:: Feature ;
5- use tucana:: shared:: { RuntimeFunctionDefinition , DefinitionDataType , FlowType } ;
63use notify:: { Watcher , RecursiveMode , Event , EventKind , recommended_watcher} ;
74use std:: sync:: mpsc:: channel;
8- use std:: time:: Duration ;
95use colored:: * ;
106use crate :: table:: * ;
7+ use serde_json;
118
129mod table;
1310
@@ -33,7 +30,7 @@ enum Commands {
3330 Feature {
3431 /// Optional name of the definition set.
3532 #[ arg( short, long) ]
36- featurename : Option < String > ,
33+ name : Option < String > ,
3734 /// Optional path to root directory of all definitions.
3835 #[ arg( short, long) ]
3936 path : Option < String > ,
@@ -60,10 +57,7 @@ fn main() {
6057
6158 match cli. command {
6259 Commands :: Report { path } => {
63- let dir_path = match path {
64- Some ( p) => p,
65- None => "../definitions" . to_string ( ) ,
66- } ;
60+ let dir_path = path. unwrap_or_else ( || "./definitions" . to_string ( ) ) ;
6761
6862 let parser = match Parser :: from_path ( dir_path. as_str ( ) ) {
6963 Some ( reader) => reader,
@@ -74,11 +68,8 @@ fn main() {
7468 error_table ( & parser. features ) ;
7569 summary_table ( & parser. features ) ;
7670 }
77- Commands :: Feature { featurename, path } => {
78- let dir_path = match path {
79- Some ( p) => p,
80- None => "../definitions" . to_string ( ) ,
81- } ;
71+ Commands :: Feature { name, path } => {
72+ let dir_path = path. unwrap_or_else ( || "./definitions" . to_string ( ) ) ;
8273
8374 let parser = match Parser :: from_path ( dir_path. as_str ( ) ) {
8475 Some ( reader) => reader,
@@ -87,10 +78,10 @@ fn main() {
8778 }
8879 } ;
8980
90- if let Some ( featurename ) = featurename {
81+ if let Some ( feature_name ) = name {
9182 let mut features_to_report = Vec :: new ( ) ;
9283 for feature in & parser. features {
93- if feature. name == featurename {
84+ if feature. name == feature_name {
9485 feature_table ( & feature) ;
9586 features_to_report. push ( feature. clone ( ) ) ;
9687 }
@@ -104,19 +95,19 @@ fn main() {
10495 }
10596 }
10697 Commands :: Definition { name, path } => {
107- println ! ( "Handling definition with name: {}" , name) ;
108- let dir_path = match path {
109- Some ( p) => p,
110- None => "../definitions" . to_string ( ) ,
98+ let dir_path = path. unwrap_or_else ( || "./definitions" . to_string ( ) ) ;
99+
100+ let parser = match Parser :: from_path ( dir_path. as_str ( ) ) {
101+ Some ( reader) => reader,
102+ None => {
103+ panic ! ( "Error reading definitions" ) ;
104+ }
111105 } ;
112106
113- todo ! ( "Implement definition query and display command!" ) ;
107+ search_and_display_definitions ( & name , & parser ) ;
114108 }
115109 Commands :: Watch { path } => {
116- let dir_path = match path {
117- Some ( p) => p,
118- None => "../definitions" . to_string ( ) ,
119- } ;
110+ let dir_path = path. unwrap_or_else ( || "./definitions" . to_string ( ) ) ;
120111
121112 println ! ( "{}" , format!( "Watching directory: {}" , dir_path) . bright_yellow( ) . bold( ) ) ;
122113 println ! ( "{}" , "Press Ctrl+C to stop watching..." . dimmed( ) ) ;
@@ -164,3 +155,81 @@ fn main() {
164155 }
165156 }
166157}
158+
159+ fn search_and_display_definitions ( search_name : & str , parser : & Parser ) {
160+ let mut found_any = false ;
161+ let mut total_matches = 0 ;
162+ println ! ( "{}" , format!( "Searching for definitions matching: '{}'" , search_name) . bright_yellow( ) . bold( ) ) ;
163+ println ! ( "{}" , "─" . repeat( 60 ) . dimmed( ) ) ;
164+
165+ for feature in & parser. features {
166+ // Search FlowTypes
167+ for flow_type in & feature. flow_types {
168+ if flow_type. identifier == search_name {
169+ total_matches += 1 ;
170+ if !found_any {
171+ found_any = true ;
172+ }
173+
174+ println ! ( "\n {}" , "FlowType" . bright_cyan( ) . bold( ) ) ;
175+ match serde_json:: to_string_pretty ( flow_type) {
176+ Ok ( json) => {
177+ for line in json. lines ( ) {
178+ println ! ( "{}" , line. bright_green( ) ) ;
179+ }
180+ }
181+ Err ( _) => println ! ( "{}" , "Error serializing FlowType" . red( ) ) ,
182+ }
183+ }
184+ }
185+
186+ // Search DataTypes
187+ for data_type in & feature. data_types {
188+ if data_type. identifier == search_name {
189+ total_matches += 1 ;
190+ if !found_any {
191+ found_any = true ;
192+ }
193+
194+ println ! ( "\n {}" , "DataType" . bright_cyan( ) . bold( ) ) ;
195+ match serde_json:: to_string_pretty ( data_type) {
196+ Ok ( json) => {
197+ for line in json. lines ( ) {
198+ println ! ( "{}" , line. bright_green( ) ) ;
199+ }
200+ }
201+ Err ( _) => println ! ( "{}" , "Error serializing DataType" . red( ) ) ,
202+ }
203+ }
204+ }
205+
206+ // Search RuntimeFunctions
207+ for runtime_func in & feature. runtime_functions {
208+ if runtime_func. runtime_name == search_name {
209+ total_matches += 1 ;
210+ if !found_any {
211+ found_any = true ;
212+ }
213+
214+ println ! ( "\n {}" , "RuntimeFunction" . bright_cyan( ) . bold( ) ) ;
215+ match serde_json:: to_string_pretty ( runtime_func) {
216+ Ok ( json) => {
217+ let mut index = 0 ;
218+ for line in json. lines ( ) {
219+ index += 1 ;
220+ println ! ( "{} {}" , format!( "{}:" , index) . bright_blue( ) , line. bright_green( ) ) ;
221+ }
222+ }
223+ Err ( _) => println ! ( "{}" , "Error serializing RuntimeFunction" . red( ) ) ,
224+ }
225+ }
226+ }
227+ }
228+
229+ if !found_any {
230+ println ! ( "\n {}" , format!( "No definitions found matching '{}'" , search_name) . red( ) . bold( ) ) ;
231+ } else {
232+ println ! ( "\n {}" , "─" . repeat( 60 ) . dimmed( ) ) ;
233+ println ! ( "{}" , format!( "Found {} matching definition(s)" , total_matches) . bright_yellow( ) ) ;
234+ }
235+ }
0 commit comments