mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-06-03 18:24:09 +02:00
no more nom
This commit is contained in:
parent
19f9c9f973
commit
c79166cd03
6 changed files with 16 additions and 61 deletions
|
|
@ -9,7 +9,6 @@ repository = "https://github.com/icewind1991/tf-log-parser"
|
|||
|
||||
[dependencies]
|
||||
steamid-ng = "1"
|
||||
nom = "7"
|
||||
enum-iterator = "1.4"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
thiserror = "1"
|
||||
|
|
|
|||
|
|
@ -227,7 +227,7 @@ impl EventParam {
|
|||
fn skip_after(&self) -> TokenStream {
|
||||
let skip_after = self.skip_after as usize;
|
||||
if skip_after > 0 {
|
||||
quote_spanned!(self.span() => let input = &input.get(#skip_after..).ok_or(nom::Err::Incomplete(nom::Needed::Unknown))?;)
|
||||
quote_spanned!(self.span() => let input = &input.get(#skip_after..).ok_or(Error::Incomplete)?;)
|
||||
} else {
|
||||
quote!()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,25 +5,18 @@ mod player;
|
|||
use crate::common::{skip, skip_matches, split_once, take_until};
|
||||
use crate::event::game::{RoundLengthEvent, RoundWinEvent};
|
||||
use crate::raw_event::{against_subject_parser, RawSubject};
|
||||
use crate::{err_incomplete, IResult, RawEvent, RawEventType, SubjectId};
|
||||
use crate::{Error, IResult, RawEvent, RawEventType, SubjectId};
|
||||
pub use game::*;
|
||||
pub use medic::*;
|
||||
use nom::error::{ErrorKind, ParseError};
|
||||
use nom::number::complete::float;
|
||||
use nom::Err;
|
||||
pub use player::*;
|
||||
use std::net::SocketAddr;
|
||||
use std::num::NonZeroU32;
|
||||
use std::str::FromStr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum GameEventError {
|
||||
#[error("malformed game event({ty:?}): {err}")]
|
||||
Error {
|
||||
err: nom::error::Error<String>,
|
||||
ty: RawEventType,
|
||||
},
|
||||
Error { err: Box<Error>, ty: RawEventType },
|
||||
#[error("incomplete event body({0:?})")]
|
||||
Incomplete(RawEventType),
|
||||
}
|
||||
|
|
@ -34,16 +27,9 @@ trait GameEventErrTrait<T> {
|
|||
|
||||
impl<'a, T> GameEventErrTrait<T> for IResult<'a, T> {
|
||||
fn with_type(self, ty: RawEventType) -> Result<T, GameEventError> {
|
||||
self.map_err(|err| match err {
|
||||
Err::Error(e) | Err::Failure(e) => GameEventError::Error {
|
||||
err: nom::error::Error {
|
||||
input: e.input.to_string(),
|
||||
code: e.code,
|
||||
},
|
||||
ty,
|
||||
},
|
||||
|
||||
Err::Incomplete(_) => GameEventError::Incomplete(ty),
|
||||
self.map_err(|err| GameEventError::Error {
|
||||
err: Box::new(err),
|
||||
ty,
|
||||
})
|
||||
.map(|(_rest, t)| t)
|
||||
}
|
||||
|
|
@ -290,7 +276,7 @@ pub fn param_parse_with<'a, T, P: Fn(&'a str) -> IResult<'a, T>>(
|
|||
fn parse_from_str<'a, T: FromStr + 'a>(input: &'a str) -> IResult<T> {
|
||||
T::from_str(input)
|
||||
.map(|res| ("", res))
|
||||
.map_err(|_| Err::Error(nom::error::Error::from_error_kind(input, ErrorKind::IsNot)))
|
||||
.map_err(|_| Error::Malformed)
|
||||
}
|
||||
|
||||
fn int(input: &str) -> IResult<i32> {
|
||||
|
|
@ -302,7 +288,7 @@ fn int(input: &str) -> IResult<i32> {
|
|||
|
||||
fn u_int(input: &str) -> IResult<u32> {
|
||||
let (input, raw) = take_until(input, b' ');
|
||||
let val = raw.parse().map_err(|_| err_incomplete())?;
|
||||
let val = raw.parse().map_err(|_| Error::Incomplete)?;
|
||||
|
||||
Ok((input, val))
|
||||
}
|
||||
|
|
@ -337,7 +323,8 @@ impl<'a> EventField<'a> for u32 {
|
|||
|
||||
impl<'a> EventField<'a> for f32 {
|
||||
fn parse_field(input: &'a str) -> IResult<Self> {
|
||||
float(input)
|
||||
let (input, raw) = take_until(input, b' ');
|
||||
Ok((input, raw.parse().map_err(|_| Error::Malformed)?))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::common::{Class, Team};
|
||||
use crate::event::{param_parse_with, parse_field, ParamIter};
|
||||
use crate::raw_event::RawSubject;
|
||||
use crate::{Event, IResult};
|
||||
use crate::{Error, Event, IResult};
|
||||
use std::net::SocketAddr;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
|
|
|
|||
32
src/lib.rs
32
src/lib.rs
|
|
@ -8,8 +8,6 @@ pub use crate::subjectmap::SubjectMap;
|
|||
use chrono::NaiveDateTime;
|
||||
pub use event::{Event, EventMeta, GameEvent};
|
||||
use memchr::memmem::{find_iter, FindIter};
|
||||
use nom::error::ErrorKind;
|
||||
use nom::Err;
|
||||
pub use raw_event::{RawEvent, RawEventType};
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryInto;
|
||||
|
|
@ -25,10 +23,6 @@ pub mod module;
|
|||
pub mod raw_event;
|
||||
mod subjectmap;
|
||||
|
||||
pub(crate) fn err_incomplete() -> Err<nom::error::Error<&'static str>> {
|
||||
Err::Error(nom::error::Error::new("", ErrorKind::Digit))
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Malformed logfile")]
|
||||
|
|
@ -40,30 +34,6 @@ pub enum Error {
|
|||
#[error("{0}")]
|
||||
MalformedEvent(#[from] GameEventError),
|
||||
}
|
||||
impl From<nom::Err<nom::error::Error<&'_ str>>> for Error {
|
||||
fn from(e: nom::Err<nom::error::Error<&'_ str>>) -> Self {
|
||||
match e {
|
||||
Err::Incomplete(_) => Error::Incomplete,
|
||||
Err::Error(_) => Error::Malformed,
|
||||
Err::Failure(_) => Error::Malformed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for nom::Err<nom::error::Error<&'static str>> {
|
||||
fn from(e: Error) -> Self {
|
||||
match e {
|
||||
Error::Incomplete => err_incomplete(),
|
||||
_ => Err::Error(nom::error::Error::new("", ErrorKind::Verify)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<nom::error::Error<&'_ str>> for Error {
|
||||
fn from(_: nom::error::Error<&str>) -> Self {
|
||||
Error::Malformed
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for Error {
|
||||
fn from(_: ParseIntError) -> Self {
|
||||
|
|
@ -74,7 +44,7 @@ impl From<ParseIntError> for Error {
|
|||
type Result<O, E = Error> = std::result::Result<O, E>;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub type IResult<'a, O> = nom::IResult<&'a str, O>;
|
||||
pub type IResult<'a, O, E = Error> = std::result::Result<(&'a str, O), E>;
|
||||
|
||||
pub fn parse(
|
||||
log: &str,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use crate::common::{split_once, Team};
|
||||
use crate::{Error, Result};
|
||||
use crate::{Error, IResult, Result};
|
||||
use crate::{SubjectError, SubjectId};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use logos::{Lexer, Logos};
|
||||
use nom::{IResult, Needed};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
|
|
@ -110,8 +109,8 @@ fn test_split_player_subject() {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn against_subject_parser(input: &str) -> IResult<&str, RawSubject> {
|
||||
subject_parser(input).map_err(|_| nom::Err::Incomplete(Needed::Unknown))
|
||||
pub fn against_subject_parser(input: &str) -> IResult<RawSubject> {
|
||||
subject_parser(input).map_err(|_| Error::Incomplete)
|
||||
}
|
||||
|
||||
pub fn subject_parser(input: &str) -> Result<(&str, RawSubject)> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue