fixed unwraps on nones occuring on fast subsequent reads in async (+rustfmt)

This commit is contained in:
emilis 2023-01-24 12:51:31 +00:00
parent f2b8517c31
commit ce611b8283
1 changed files with 184 additions and 178 deletions

View File

@ -107,7 +107,8 @@ pub enum Key {
/// Parse an Event from `item` and possibly subsequent bytes through `iter`. /// Parse an Event from `item` and possibly subsequent bytes through `iter`.
pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error> pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
where I: Iterator<Item = Result<u8, Error>> where
I: Iterator<Item = Result<u8, Error>>,
{ {
let error = Error::new(ErrorKind::Other, "Could not parse an event"); let error = Error::new(ErrorKind::Other, "Could not parse an event");
match item { match item {
@ -138,12 +139,10 @@ pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
c @ b'\x01'..=b'\x1A' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1 + b'a') as char))), c @ b'\x01'..=b'\x1A' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1 + b'a') as char))),
c @ b'\x1C'..=b'\x1F' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1C + b'4') as char))), c @ b'\x1C'..=b'\x1F' => Ok(Event::Key(Key::Ctrl((c as u8 - 0x1C + b'4') as char))),
b'\0' => Ok(Event::Key(Key::Null)), b'\0' => Ok(Event::Key(Key::Null)),
c => { c => Ok({
Ok({
let ch = parse_utf8_char(c, iter)?; let ch = parse_utf8_char(c, iter)?;
Event::Key(Key::Char(ch)) Event::Key(Key::Char(ch))
}) }),
}
} }
} }
@ -151,7 +150,8 @@ pub fn parse_event<I>(item: u8, iter: &mut I) -> Result<Event, Error>
/// ///
/// Returns None if an unrecognized sequence is found. /// Returns None if an unrecognized sequence is found.
fn parse_csi<I>(iter: &mut I) -> Option<Event> fn parse_csi<I>(iter: &mut I) -> Option<Event>
where I: Iterator<Item = Result<u8, Error>> where
I: Iterator<Item = Result<u8, Error>>,
{ {
Some(match iter.next() { Some(match iter.next() {
Some(Ok(b'[')) => match iter.next() { Some(Ok(b'[')) => match iter.next() {
@ -197,29 +197,26 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
// xterm mouse encoding: // xterm mouse encoding:
// ESC [ < Cb ; Cx ; Cy (;) (M or m) // ESC [ < Cb ; Cx ; Cy (;) (M or m)
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut c = iter.next().unwrap().unwrap(); let mut c = match iter.next() {
Some(next) => next.unwrap(),
None => return None,
};
while match c { while match c {
b'm' | b'M' => false, b'm' | b'M' => false,
_ => true, _ => true,
} { } {
buf.push(c); buf.push(c);
c = iter.next().unwrap().unwrap(); c = match iter.next() {
Some(next) => next.unwrap(),
None => return None,
};
} }
let str_buf = String::from_utf8(buf).unwrap(); let str_buf = String::from_utf8(buf).unwrap();
let nums = &mut str_buf.split(';'); let nums = &mut str_buf.split(';');
let cb = nums.next() let cb = nums.next().unwrap().parse::<u16>().unwrap();
.unwrap() let cx = nums.next().unwrap().parse::<u16>().unwrap();
.parse::<u16>() let cy = nums.next().unwrap().parse::<u16>().unwrap();
.unwrap();
let cx = nums.next()
.unwrap()
.parse::<u16>()
.unwrap();
let cy = nums.next()
.unwrap()
.parse::<u16>()
.unwrap();
let event = match cb { let event = match cb {
0..=2 | 64..=65 => { 0..=2 | 64..=65 => {
@ -248,12 +245,18 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
// Numbered escape code. // Numbered escape code.
let mut buf = Vec::new(); let mut buf = Vec::new();
buf.push(c); buf.push(c);
let mut c = iter.next().unwrap().unwrap(); let mut c = match iter.next() {
Some(next) => next.unwrap(),
None => return None,
};
// The final byte of a CSI sequence can be in the range 64-126, so // The final byte of a CSI sequence can be in the range 64-126, so
// let's keep reading anything else. // let's keep reading anything else.
while c < 64 || c > 126 { while c < 64 || c > 126 {
buf.push(c); buf.push(c);
c = iter.next().unwrap().unwrap(); c = match iter.next() {
Some(next) => next.unwrap(),
None => return None,
};
} }
match c { match c {
@ -316,14 +319,17 @@ fn parse_csi<I>(iter: &mut I) -> Option<Event>
} }
_ => return None, _ => return None,
}) })
} }
/// Parse `c` as either a single byte ASCII char or a variable size UTF-8 char. /// Parse `c` as either a single byte ASCII char or a variable size UTF-8 char.
fn parse_utf8_char<I>(c: u8, iter: &mut I) -> Result<char, Error> fn parse_utf8_char<I>(c: u8, iter: &mut I) -> Result<char, Error>
where I: Iterator<Item = Result<u8, Error>> where
I: Iterator<Item = Result<u8, Error>>,
{ {
let error = Err(Error::new(ErrorKind::Other, "Input character is not valid UTF-8")); let error = Err(Error::new(
ErrorKind::Other,
"Input character is not valid UTF-8",
));
if c.is_ascii() { if c.is_ascii() {
Ok(c as char) Ok(c as char)
} else { } else {