From 1baebacc7f8541ed7077b12b3f8989255ec290fe Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 27 Apr 2020 15:30:07 +0200 Subject: 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. --- src/lib.rs | 1 - src/raw/parser.rs | 29 ++++++++++++----------------- 2 files changed, 12 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index c2272cf..891e387 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 for ParseError { - fn from(_: std::option::NoneError) -> Self { - ParseError::InvalidData - } -} - /// A type indicating the parse result. pub type ParseResult = Result; @@ -254,13 +248,18 @@ pub fn parse_skill(input: &mut T) -> ParseResult { /// /// * `input` - Input stream. /// * `parser` - The parse function to use. -pub fn parse_events(input: &mut T, parser: fn(&mut T) -> ParseResult) -> ParseResult> { +pub fn parse_events( + input: &mut T, + parser: fn(&mut T) -> ParseResult, +) -> ParseResult> { 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(input: &mut T) -> ParseResult { 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(input: &mut T) -> ParseResult { 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(input: &mut T) -> ParseResult { }) } - - /// Parse a partial EVTC file. /// /// * `input` - Input stream. @@ -409,8 +408,6 @@ pub fn parse_partial_file(input: &mut T) -> ParseResult { }) } - - /// Finish a partial EVTC by reading the events. /// /// * `partial` - The partial EVTC. @@ -431,8 +428,6 @@ pub fn finish_parsing(partial: PartialEvtc, input: &mut T) -> ParseResu }) } - - /// Parse a complete EVTC file. /// /// * `input` - Input stream. -- cgit v1.2.3