tf_demo_parser/demo/parser/
error.rs

1use crate::demo::gamevent::GameEventValueType;
2use crate::demo::message::gameevent::GameEventTypeId;
3use crate::demo::message::packetentities::EntityId;
4use crate::demo::packet::datatable::{ClassId, SendTableName};
5use crate::demo::sendprop::{SendPropIdentifier, SendPropValue};
6use bitbuffer::BitError;
7use std::str::Utf8Error;
8use std::string::FromUtf8Error;
9use thiserror::Error;
10
11/// Errors that can occur during parsing
12#[non_exhaustive]
13#[derive(Debug, Error)]
14pub enum ParseError {
15    #[error("Error while reading bits from stream: {0}")]
16    ReadError(#[source] BitError),
17    #[error("Malformed utf8 while reading string")]
18    MalformedUTF8(#[from] Utf8Error),
19    #[error("Unexpected type of compressed data: {0}")]
20    UnexpectedCompressionType(String),
21    #[error("Error while decompressing SNAP compressed string table: {0}")]
22    SnapError(#[from] snap::Error),
23    #[error(
24        "Unexpected size after decompressing SNAP data, got {} bytes, expected {} bytes",
25        size,
26        expected
27    )]
28    UnexpectedDecompressedSize {
29        /// Expected decompressed size
30        expected: u32,
31        /// Actual decompressed size
32        size: u32,
33    },
34    #[error("Malformed demo file: {0}")]
35    InvalidDemo(&'static str),
36    #[error("Packet identifier is invalid: {0}")]
37    InvalidPacketType(u8),
38    #[error("Message identifier is invalid: {0}")]
39    InvalidMessageType(u8),
40    #[error("Invalid SendProp type: {0}")]
41    InvalidSendPropType(u8),
42    #[error("Invalid SendProp: {0}")]
43    InvalidSendProp(#[from] MalformedSendPropDefinitionError),
44    #[error("Unexpected amount of data left after parsing an object, {0} bits remaining")]
45    DataRemaining(usize),
46    #[error("String table with index {0} not found")]
47    StringTableNotFound(u8),
48    #[error("A malformed game event was read: {0}")]
49    MalformedGameEvent(#[from] GameEventError),
50    #[error(
51        "A read game event doesn't contain the expected values, expected type {expected_type} for {name} event, got type {found_type}"
52    )]
53    InvalidGameEvent {
54        expected_type: GameEventValueType,
55        name: &'static str,
56        found_type: GameEventValueType,
57    },
58    #[error("Game event of type {ty} does not contain a {field} value")]
59    MissingGameEventValue { ty: &'static str, field: String },
60    #[error("An entity with an unknown server class({0}) was read")]
61    UnknownServerClass(ClassId),
62    #[error("Unknown send table: {}", _0)]
63    UnknownSendTable(SendTableName),
64    #[error("Property index out of bounds, got {index} but only {prop_count} props exist")]
65    PropIndexOutOfBounds {
66        index: i32,
67        prop_count: usize,
68        table: String,
69    },
70    #[error("An attempt was made to update an unknown entity: {0}")]
71    UnknownEntity(EntityId),
72    #[error("No sendprop definition found for property")]
73    UnknownDefinition(SendPropIdentifier),
74}
75
76#[non_exhaustive]
77#[derive(Debug, Error)]
78pub enum MalformedSendPropDefinitionError {
79    #[error("Float property without defined size")]
80    UnsizedFloat,
81    #[error("Array property without defined size")]
82    UnsizedArray,
83    #[error("Array property without defined inner type")]
84    UntypedArray,
85    #[error("Property used that can't be read")]
86    InvalidPropType,
87    #[error("Array contents can't have the 'ChangesOften' flag")]
88    ArrayChangesOften,
89    #[error("SendProp value out of range")]
90    OutOfRange,
91    #[error("Wrong prop value type for definition")]
92    WrongPropType {
93        expected: &'static str,
94        value: SendPropValue,
95    },
96}
97
98#[non_exhaustive]
99#[derive(Debug, Error)]
100pub enum GameEventError {
101    #[error("Incorrect number of values")]
102    IncorrectValueCount,
103    #[error("Event with 'none' value")]
104    NoneValue,
105    #[error("Unknown type: {0}")]
106    UnknownType(GameEventTypeId),
107}
108
109impl From<BitError> for ParseError {
110    fn from(err: BitError) -> ParseError {
111        match err {
112            BitError::Utf8Error(utf8_error, _) => ParseError::MalformedUTF8(utf8_error),
113            _ => ParseError::ReadError(err),
114        }
115    }
116}
117
118impl From<FromUtf8Error> for ParseError {
119    fn from(err: FromUtf8Error) -> ParseError {
120        ParseError::MalformedUTF8(err.utf8_error())
121    }
122}
123
124pub type Result<T> = std::result::Result<T, ParseError>;