Skip to content

Commit

Permalink
Improve Error Handling, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pflenker committed Apr 2, 2024
1 parent 4a7df66 commit ce8bed0
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use crossterm::event::{read, Event::Key, KeyCode::Char};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};


pub struct Editor {

}
pub struct Editor {}

impl Editor {
pub fn default() -> Self {
Editor{}
pub fn default() -> Self {
Editor {}
}
pub fn run(&self) {
if let Err(err) = self.repl() {

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

repl will soon hold our read-eval-print` loop. The function will return nothing if everything went well, and returns an error if something we couldn't recover from happened.

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

Note how we're using the shorthand if let again.

panic!("{err:#?}");

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

panic! is a macro which basically crashes our program cleanly. Since at this point we have no way of handling this error, let's crash and print a pretty debug version of the error we encountered.

}
print!("Goodbye.\r\n");
}
pub fn run(&self){
enable_raw_mode().unwrap();
fn repl(&self) -> Result<(), std::io::Error> {

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

This is our new function. Note the return type: Result<(), std::io::Error>
To stay with our metaphor with the boxes, this code says:
"This function will return either a pink Ok box with nothing in it, or a black Err box with a std::io::Error in it".

enable_raw_mode()?;

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

This refactoring enables us to use the ? operator.
What this does is: It unwraps the Result of enable_raw_mode for us. If it's an error, it returns the error immediately. If not, it continues.

loop {
match read() {
Ok(Key(event)) => {
println!("{event:?} \r");
if let Char(c) = event.code {
if c == 'q' {
break;
}
if let Key(event) = read()? {

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

Similarly, we can use the ? operator here. And since we no longer need to match the Err down here, we can simplify our match and replace it with an if let.

println!("{event:?} \r");
if let Char(c) = event.code {
if c == 'q' {
break;
}
},
Err(err) => println!("Error: {err}"),
_ => ()
}
}
}
disable_raw_mode().unwrap();
disable_raw_mode()?;
Ok(())

This comment has been minimized.

Copy link
@pflenker

pflenker Apr 2, 2024

Author Owner

Our function now needs to return something. We leverage the fact that Rust treats the last line as something we want to return (notice the missing ;).
If we made it down here, it means none of the functions with a ? have found an error that has been returned, so we can safely return a pink box labeled Ok with nothing in it. The code representation of this is: Ok(()).

}
}

0 comments on commit ce8bed0

Please sign in to comment.