@@ -35,65 +35,49 @@ pub fn start_repl(port: &mut Box<dyn SerialPort>) -> Result<()> {
3535 repl. run ( )
3636}
3737
38- /// Wrap peek command
39- fn peek ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
40- let address = _args. value_of ( "address" ) . unwrap ( ) . to_string ( ) ;
41- let length = _args. value_of ( "length" ) . unwrap_or ( "1" ) . to_string ( ) . parse :: < usize > ( ) ?;
42- match commands:: peek ( context. port , address, length, None , true ) {
38+ /// Helper function to convert error type
39+ fn handle_result ( result : core:: result:: Result < ( ) , anyhow:: Error > ) -> Result < Option < String > > {
40+ match result {
4341 Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
4442 err. to_string ( ) ,
4543 ) ) ,
4644 Ok ( ( ) ) => Ok ( None ) ,
4745 }
4846}
4947
48+ /// Wrap peek command
49+ fn peek ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
50+ let address = _args. value_of ( "address" ) . unwrap ( ) . to_string ( ) ;
51+ let length = _args
52+ . value_of ( "length" )
53+ . unwrap_or ( "1" )
54+ . to_string ( )
55+ . parse :: < usize > ( ) ?;
56+ let result = commands:: peek ( context. port , address, length, None , true ) ;
57+ handle_result ( result)
58+ }
5059
5160/// Wrap reset command
5261fn reset ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
53- match commands:: reset ( context. port , false ) {
54- Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
55- err. to_string ( ) ,
56- ) ) ,
57- Ok ( ( ) ) => Ok ( None ) ,
58- }
62+ handle_result ( commands:: reset ( context. port , false ) )
5963}
6064
6165/// Wrap go64 command
6266fn go64 ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
63- match serial:: go64 ( context. port ) {
64- Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
65- err. to_string ( ) ,
66- ) ) ,
67- Ok ( ( ) ) => Ok ( None ) ,
68- }
67+ handle_result ( serial:: go64 ( context. port ) )
6968}
7069
7170/// Wrap stop cpu command
7271fn stop ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
73- match serial:: stop_cpu ( context. port ) {
74- Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
75- err. to_string ( ) ,
76- ) ) ,
77- Ok ( ( ) ) => Ok ( None ) ,
78- }
72+ handle_result ( serial:: stop_cpu ( context. port ) )
7973}
8074
8175/// Wrap start cpu command
8276fn start ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
83- match serial:: start_cpu ( context. port ) {
84- Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
85- err. to_string ( ) ,
86- ) ) ,
87- Ok ( ( ) ) => Ok ( None ) ,
88- }
77+ handle_result ( serial:: start_cpu ( context. port ) )
8978}
9079
9180/// Wrap filehost command
9281fn filehost ( _args : ArgMatches , context : & mut Context ) -> Result < Option < String > > {
93- match commands:: filehost ( context. port ) {
94- Err ( err) => Err ( reedline_repl_rs:: Error :: IllegalDefaultError (
95- err. to_string ( ) ,
96- ) ) ,
97- Ok ( ( ) ) => Ok ( None ) ,
98- }
82+ handle_result ( commands:: filehost ( context. port ) )
9983}
0 commit comments