mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-06-03 18:24:09 +02:00
more fine grained line skip
This commit is contained in:
parent
ec62be0b38
commit
8e61628c22
3 changed files with 63 additions and 44 deletions
54
src/error.rs
Normal file
54
src/error.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use crate::event::GameEventError;
|
||||
use crate::SubjectError;
|
||||
use std::num::ParseIntError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Malformed logfile")]
|
||||
Malformed,
|
||||
#[error("Incomplete logfile")]
|
||||
Incomplete,
|
||||
#[error("Incomplete logfile")]
|
||||
Skip,
|
||||
#[error("Malformed subject: {0}")]
|
||||
Subject(Box<SubjectError>),
|
||||
#[error("{0}")]
|
||||
MalformedEvent(Box<GameEventError>),
|
||||
}
|
||||
|
||||
impl From<SubjectError> for Error {
|
||||
fn from(value: SubjectError) -> Self {
|
||||
Error::Subject(Box::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GameEventError> for Error {
|
||||
fn from(value: GameEventError) -> Self {
|
||||
Error::MalformedEvent(Box::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for Error {
|
||||
fn from(_: ParseIntError) -> Self {
|
||||
Error::Malformed
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<O, E = Error> = std::result::Result<O, E>;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub type IResult<'a, O, E = Error> = std::result::Result<(&'a str, O), E>;
|
||||
|
||||
pub trait ResultExt: Sized {
|
||||
fn skip_incomplete(self) -> Self;
|
||||
}
|
||||
|
||||
impl<T> ResultExt for Result<T> {
|
||||
fn skip_incomplete(self) -> Self {
|
||||
self.map_err(|e| match e {
|
||||
Error::Incomplete => Error::Skip,
|
||||
e => e,
|
||||
})
|
||||
}
|
||||
}
|
||||
45
src/lib.rs
45
src/lib.rs
|
|
@ -1,64 +1,28 @@
|
|||
pub use crate::common::{SteamId3, SubjectData, SubjectError, SubjectId};
|
||||
use crate::event::GameEventError;
|
||||
pub use crate::module::EventHandler;
|
||||
use crate::module::{
|
||||
ChatMessages, ClassStatsHandler, HealSpread, MedicStatsBuilder, PlayerHandler,
|
||||
};
|
||||
pub use crate::subjectmap::SubjectMap;
|
||||
use chrono::{Duration, NaiveDate, NaiveDateTime};
|
||||
pub(crate) use error::ResultExt;
|
||||
pub use error::{Error, IResult, Result};
|
||||
pub use event::{Event, EventMeta, GameEvent};
|
||||
use memchr::memmem::{find_iter, FindIter};
|
||||
pub use raw_event::{RawEvent, RawEventType};
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryInto;
|
||||
use std::fmt::Debug;
|
||||
use std::num::ParseIntError;
|
||||
pub(crate) use tf_log_parser_derive::{Event, Events};
|
||||
use thiserror::Error;
|
||||
|
||||
mod common;
|
||||
pub mod event;
|
||||
#[macro_use]
|
||||
pub mod module;
|
||||
mod error;
|
||||
pub(crate) mod parsing;
|
||||
pub mod raw_event;
|
||||
mod subjectmap;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Malformed logfile")]
|
||||
Malformed,
|
||||
#[error("Incomplete logfile")]
|
||||
Incomplete,
|
||||
#[error("Malformed subject: {0}")]
|
||||
Subject(Box<SubjectError>),
|
||||
#[error("{0}")]
|
||||
MalformedEvent(Box<GameEventError>),
|
||||
}
|
||||
|
||||
impl From<SubjectError> for Error {
|
||||
fn from(value: SubjectError) -> Self {
|
||||
Error::Subject(Box::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GameEventError> for Error {
|
||||
fn from(value: GameEventError) -> Self {
|
||||
Error::MalformedEvent(Box::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for Error {
|
||||
fn from(_: ParseIntError) -> Self {
|
||||
Error::Malformed
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<O, E = Error> = std::result::Result<O, E>;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub type IResult<'a, O, E = Error> = std::result::Result<(&'a str, O), E>;
|
||||
|
||||
pub fn parse(
|
||||
log: &str,
|
||||
) -> Result<
|
||||
|
|
@ -94,7 +58,8 @@ pub fn parse_with_handler<Handler: EventHandler>(
|
|||
while let Some(event_res) = events.next() {
|
||||
let raw_event = match event_res {
|
||||
Ok(raw_event) => raw_event,
|
||||
Err(Error::Incomplete) => continue,
|
||||
Err(Error::Incomplete) if events.next().is_none() => break,
|
||||
Err(Error::Skip) => continue,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let should_handle = Handler::does_handle(raw_event.ty);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::common::Team;
|
||||
use crate::parsing::{skip, skip_matches, split_once, split_subject_end};
|
||||
use crate::{Error, Result};
|
||||
use crate::{Error, Result, ResultExt};
|
||||
use crate::{SubjectError, SubjectId};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use logos::{Lexer, Logos};
|
||||
|
|
@ -26,13 +26,13 @@ impl<'a> RawEvent<'a> {
|
|||
fn event_parser(input: &str) -> Result<RawEvent> {
|
||||
// println!("{}", input);
|
||||
if input.len() < 24 {
|
||||
return Err(Error::Incomplete);
|
||||
return Err(Error::Skip);
|
||||
}
|
||||
let date = RawDate(&input[0..21]);
|
||||
|
||||
let (input, subject) = subject_parser(&input[23..])?;
|
||||
let (input, subject) = subject_parser(&input[23..]).skip_incomplete()?;
|
||||
|
||||
let (input, ty) = event_type_parser(input)?;
|
||||
let (input, ty) = event_type_parser(input).skip_incomplete()?;
|
||||
|
||||
let params = skip_matches(input, b' ').0;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue