1- use crate :: { IndexSession , btree:: Item } ;
1+ use crate :: {
2+ IndexSession ,
3+ btree:: { Item , utils:: Visualizer } ,
4+ } ;
5+ use std:: path:: PathBuf ;
26
37struct Command < KeyType , ValType > {
48 index_type : String ,
@@ -12,30 +16,57 @@ where
1216 KeyType : std:: str:: FromStr ,
1317 ValType : std:: str:: FromStr ,
1418{
15- fn new ( command : & str ) -> Self {
16- let tokens: Vec < & str > = command. split ( " " ) . collect ( ) ;
19+ fn new ( command : & str ) -> Option < Self > {
20+ let tokens: Vec < & str > = command. split_whitespace ( ) . collect ( ) ;
21+ if tokens. is_empty ( ) {
22+ return None ;
23+ }
24+
25+ let index_type = tokens[ 0 ] . to_string ( ) ;
26+ if tokens. len ( ) < 2 {
27+ return None ;
28+ }
29+
30+ let index_function = tokens[ 1 ] . to_string ( ) ;
1731 let key = if tokens. len ( ) > 2 {
18- Some ( tokens[ 2 ] . parse :: < KeyType > ( ) . ok ( ) . unwrap ( ) )
32+ tokens[ 2 ] . parse :: < KeyType > ( ) . ok ( )
1933 } else {
2034 None
2135 } ;
2236 let value = if tokens. len ( ) > 3 {
23- Some ( tokens[ 3 ] . parse :: < ValType > ( ) . ok ( ) . unwrap ( ) )
37+ tokens[ 3 ] . parse :: < ValType > ( ) . ok ( )
2438 } else {
2539 None
2640 } ;
27- Command {
28- index_type : tokens[ 0 ] . to_string ( ) ,
29- index_function : tokens[ 1 ] . to_string ( ) ,
41+
42+ Some ( Command {
43+ index_type,
44+ index_function,
3045 key,
3146 value,
32- }
47+ } )
3348 }
3449}
50+
3551pub fn parse_command ( index_session : & mut IndexSession , command : & str ) {
3652 let trimmed_command = command. trim ( ) ;
53+ if trimmed_command. is_empty ( ) {
54+ return ;
55+ }
3756 println ! ( "Command: {trimmed_command}" ) ;
38- let cmd: Command < i32 , String > = Command :: new ( command) ;
57+
58+ let mut viz_path = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
59+ viz_path. push ( "src" ) ;
60+ viz_path. push ( "btree" ) ;
61+ viz_path. push ( "tests" ) ;
62+ viz_path. push ( "visualizer.md" ) ;
63+ let visualizer = Visualizer :: new ( viz_path. to_str ( ) . unwrap ( ) ) ;
64+
65+ let cmd = match Command :: < i32 , String > :: new ( trimmed_command) {
66+ Some ( cmd) => cmd,
67+ None => return ,
68+ } ;
69+
3970 if cmd. index_type . as_str ( ) == "BTREE" || cmd. index_type . as_str ( ) == "btree" {
4071 match cmd. index_function . as_str ( ) {
4172 "INSERT" | "insert" => {
@@ -58,7 +89,10 @@ pub fn parse_command(index_session: &mut IndexSession, command: &str) {
5889 } ;
5990
6091 index_session. btree . insert ( Item { key, val } ) ;
61- println ! ( "{:?}" , index_session. btree) ;
92+
93+ if let Err ( e) = visualizer. update ( & index_session. btree ) {
94+ eprintln ! ( "Failed to update visualization: {e}" ) ;
95+ }
6296 }
6397 "SEARCH" | "search" => {
6498 let key = match cmd. key {
@@ -85,7 +119,10 @@ pub fn parse_command(index_session: &mut IndexSession, command: &str) {
85119 match index_session. btree . delete ( key) {
86120 Ok ( _) => {
87121 println ! ( "Successfully deleted key {key}" ) ;
88- println ! ( "Tree after deletion: {:?}" , index_session. btree) ;
122+
123+ if let Err ( e) = visualizer. update ( & index_session. btree ) {
124+ eprintln ! ( "Failed to update visualization: {e}" ) ;
125+ }
89126 }
90127 Err ( e) => println ! ( "Failed to delete key {key}: {e}" ) ,
91128 }
0 commit comments