diff options
author | Daniel Schadt <kingdread@gmx.de> | 2020-04-27 15:30:07 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2020-04-27 15:30:07 +0200 |
commit | 1baebacc7f8541ed7077b12b3f8989255ec290fe (patch) | |
tree | 2a7ea2ef46c2bd6433c045756217b2bb8de83c23 | |
parent | 84d74c3a47cc0cfad42f2b000a872d9fcbec120e (diff) | |
download | evtclib-1baebacc7f8541ed7077b12b3f8989255ec290fe.tar.gz evtclib-1baebacc7f8541ed7077b12b3f8989255ec290fe.tar.bz2 evtclib-1baebacc7f8541ed7077b12b3f8989255ec290fe.zip |
remove try_trait
The .ok_or() method on Option is enough for those two occasions that we
don't need to activate the complete try_trait feature just for a very
small and questionable ergonomics gain. This also allows us to more
easily discern which data exactly was invalid, as the .ok_or() in
different places could take different error values.
This also allows evtclib to be used on stable now.
Some noise is introduced in the diff due to automatically re-formatting
the source.
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/raw/parser.rs | 29 |
2 files changed, 12 insertions, 18 deletions
@@ -16,7 +16,6 @@ //! //! (Look at the note on "Buffering" in the [parser //! module](raw/parser/index.html#buffering)) -#![feature(try_trait)] use thiserror::Error; use getset::Getters; diff --git a/src/raw/parser.rs b/src/raw/parser.rs index 0e91f78..5544fd2 100644 --- a/src/raw/parser.rs +++ b/src/raw/parser.rs @@ -65,8 +65,8 @@ use byteorder::{LittleEndian, ReadBytesExt, LE}; use num_traits::FromPrimitive; -use thiserror::Error; use std::io::{self, ErrorKind, Read}; +use thiserror::Error; use super::*; @@ -124,12 +124,6 @@ pub enum ParseError { InvalidZip(#[from] zip::result::ZipError), } -impl From<std::option::NoneError> for ParseError { - fn from(_: std::option::NoneError) -> Self { - ParseError::InvalidData - } -} - /// A type indicating the parse result. pub type ParseResult<T> = Result<T, ParseError>; @@ -254,13 +248,18 @@ pub fn parse_skill<T: Read>(input: &mut T) -> ParseResult<Skill> { /// /// * `input` - Input stream. /// * `parser` - The parse function to use. -pub fn parse_events<T: Read>(input: &mut T, parser: fn(&mut T) -> ParseResult<CbtEvent>) -> ParseResult<Vec<CbtEvent>> { +pub fn parse_events<T: Read>( + input: &mut T, + parser: fn(&mut T) -> ParseResult<CbtEvent>, +) -> ParseResult<Vec<CbtEvent>> { let mut result = Vec::new(); loop { let event = parser(input); match event { Ok(x) => result.push(x), - Err(ParseError::Io(ref e)) if e.kind() == ErrorKind::UnexpectedEof => return Ok(result), + Err(ParseError::Io(ref e)) if e.kind() == ErrorKind::UnexpectedEof => { + return Ok(result) + } Err(e) => return Err(e), } } @@ -295,7 +294,8 @@ pub fn parse_event_rev0<T: Read>(input: &mut T) -> ParseResult<CbtEvent> { let is_ninety = input.read_u8()? != 0; let is_fifty = input.read_u8()? != 0; let is_moving = input.read_u8()? != 0; - let is_statechange = CbtStateChange::from_u8(input.read_u8()?)?; + let is_statechange = + CbtStateChange::from_u8(input.read_u8()?).ok_or(ParseError::InvalidData)?; let is_flanking = input.read_u8()? != 0; let is_shields = input.read_u8()? != 0; @@ -355,7 +355,8 @@ pub fn parse_event_rev1<T: Read>(input: &mut T) -> ParseResult<CbtEvent> { let is_ninety = input.read_u8()? != 0; let is_fifty = input.read_u8()? != 0; let is_moving = input.read_u8()? != 0; - let is_statechange = CbtStateChange::from_u8(input.read_u8()?)?; + let is_statechange = + CbtStateChange::from_u8(input.read_u8()?).ok_or(ParseError::InvalidData)?; let is_flanking = input.read_u8()? != 0; let is_shields = input.read_u8()? != 0; let is_offcycle = input.read_u8()? != 0; @@ -390,8 +391,6 @@ pub fn parse_event_rev1<T: Read>(input: &mut T) -> ParseResult<CbtEvent> { }) } - - /// Parse a partial EVTC file. /// /// * `input` - Input stream. @@ -409,8 +408,6 @@ pub fn parse_partial_file<T: Read>(input: &mut T) -> ParseResult<PartialEvtc> { }) } - - /// Finish a partial EVTC by reading the events. /// /// * `partial` - The partial EVTC. @@ -431,8 +428,6 @@ pub fn finish_parsing<T: Read>(partial: PartialEvtc, input: &mut T) -> ParseResu }) } - - /// Parse a complete EVTC file. /// /// * `input` - Input stream. |