1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

derive Display where possible

This commit is contained in:
Robin Appelman 2019-08-27 21:43:46 +02:00
commit 66668e3e61
7 changed files with 47 additions and 91 deletions

26
Cargo.lock generated
View file

@ -267,6 +267,29 @@ dependencies = [
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "parse-display"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parse-display-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parse-display-derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "pretty_assertions" name = "pretty_assertions"
version = "0.6.1" version = "0.6.1"
@ -476,6 +499,7 @@ dependencies = [
"enumflags2_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "enumflags2_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parse-display 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
@ -559,6 +583,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum output_vt100 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" "checksum output_vt100 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9"
"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" "checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
"checksum parse-display 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2599c9b246df6125636caa76dd32be9ee3ebb530342838c7bca346e8a36c6a71"
"checksum parse-display-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d35162893701f919df3c4cb873ed78dccbd80ffea968b120a975c64249a3ef2"
"checksum pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427" "checksum pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"

View file

@ -24,6 +24,7 @@ serde_json = "1.0"
serde_repr = "0.1" serde_repr = "0.1"
better-panic = "0.1" better-panic = "0.1"
err-derive = "0.1" err-derive = "0.1"
parse-display = "0.1"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6" pretty_assertions = "0.6"

View file

@ -4,6 +4,7 @@ use crate::{MalformedDemoError, ParseError, Result};
pub use super::gameevent_gen::{GameEvent, GameEventType}; pub use super::gameevent_gen::{GameEvent, GameEventType};
use crate::demo::message::gameevent::GameEventTypeId; use crate::demo::message::gameevent::GameEventTypeId;
use parse_display::Display;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
@ -41,7 +42,7 @@ pub struct GameEventEntry {
pub kind: GameEventValueType, pub kind: GameEventValueType,
} }
#[derive(BitRead, Debug, Clone, Copy, PartialEq)] #[derive(BitRead, Debug, Clone, Copy, PartialEq, Display)]
#[discriminant_bits = 3] #[discriminant_bits = 3]
pub enum GameEventValueType { pub enum GameEventValueType {
None = 0, None = 0,
@ -54,21 +55,6 @@ pub enum GameEventValueType {
Local = 7, Local = 7,
} }
impl fmt::Display for GameEventValueType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GameEventValueType::None => write!(f, "None"),
GameEventValueType::String => write!(f, "String"),
GameEventValueType::Float => write!(f, "Float"),
GameEventValueType::Long => write!(f, "Long"),
GameEventValueType::Short => write!(f, "Short"),
GameEventValueType::Byte => write!(f, "Byte"),
GameEventValueType::Boolean => write!(f, "Boolean"),
GameEventValueType::Local => write!(f, "Local"),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum GameEventValue { pub enum GameEventValue {
String(String), String(String),

View file

@ -6,6 +6,7 @@ use crate::demo::packet::datatable::{SendTable, SendTableName, ServerClass};
use crate::demo::parser::ParseBitSkip; use crate::demo::parser::ParseBitSkip;
use crate::demo::sendprop::{SendProp, SendPropDefinition, SendPropValue}; use crate::demo::sendprop::{SendProp, SendPropDefinition, SendPropValue};
use crate::{MalformedDemoError, Parse, ParseError, ParserState, ReadResult, Result, Stream}; use crate::{MalformedDemoError, Parse, ParseError, ParserState, ReadResult, Result, Stream};
use parse_display::Display;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::hint::unreachable_unchecked; use std::hint::unreachable_unchecked;
@ -13,15 +14,9 @@ use std::num::ParseIntError;
use std::rc::Rc; use std::rc::Rc;
use std::str::FromStr; use std::str::FromStr;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
pub struct EntityId(u32); pub struct EntityId(u32);
impl fmt::Display for EntityId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<u32> for EntityId { impl From<u32> for EntityId {
fn from(num: u32) -> Self { fn from(num: u32) -> Self {
EntityId(num) EntityId(num)

View file

@ -3,6 +3,7 @@ use bitstream_reader::{BitRead, LittleEndian};
use crate::demo::parser::MalformedSendPropDefinitionError; use crate::demo::parser::MalformedSendPropDefinitionError;
use crate::demo::sendprop::{SendPropDefinition, SendPropFlag, SendPropName, SendPropType}; use crate::demo::sendprop::{SendPropDefinition, SendPropFlag, SendPropName, SendPropType};
use crate::{MalformedDemoError, Parse, ParseError, ParserState, ReadResult, Result, Stream}; use crate::{MalformedDemoError, Parse, ParseError, ParserState, ReadResult, Result, Stream};
use parse_display::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
@ -36,21 +37,9 @@ pub struct ServerClass {
pub data_table: SendTableName, pub data_table: SendTableName,
} }
#[derive(BitRead, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] #[derive(BitRead, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, Display)]
pub struct SendTableName(Rc<String>); pub struct SendTableName(Rc<String>);
impl Clone for SendTableName {
fn clone(&self) -> Self {
SendTableName(Rc::clone(&self.0))
}
}
impl fmt::Display for SendTableName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for SendTableName { impl From<String> for SendTableName {
fn from(value: String) -> Self { fn from(value: String) -> Self {
Self(Rc::new(value)) Self(Rc::new(value))

View file

@ -9,11 +9,12 @@ use super::vector::{Vector, VectorXY};
use crate::demo::message::stringtable::log_base2; use crate::demo::message::stringtable::log_base2;
use crate::demo::packet::datatable::SendTableName; use crate::demo::packet::datatable::SendTableName;
use crate::demo::parser::MalformedSendPropDefinitionError; use crate::demo::parser::MalformedSendPropDefinitionError;
use parse_display::Display;
use std::convert::TryInto; use std::convert::TryInto;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
#[derive(BitRead, PartialEq, Eq, Hash, Debug)] #[derive(BitRead, PartialEq, Eq, Hash, Debug, Display, Clone)]
pub struct SendPropName(Rc<String>); pub struct SendPropName(Rc<String>);
impl PartialEq<&str> for SendPropName { impl PartialEq<&str> for SendPropName {
@ -22,18 +23,6 @@ impl PartialEq<&str> for SendPropName {
} }
} }
impl Clone for SendPropName {
fn clone(&self) -> Self {
SendPropName(Rc::clone(&self.0))
}
}
impl fmt::Display for SendPropName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for SendPropName { impl From<String> for SendPropName {
fn from(value: String) -> Self { fn from(value: String) -> Self {
Self(Rc::new(value)) Self(Rc::new(value))
@ -213,7 +202,7 @@ impl SendPropDefinition {
} }
} }
#[derive(BitRead, Copy, Clone, PartialEq, Debug)] #[derive(BitRead, Copy, Clone, PartialEq, Debug, Display)]
#[discriminant_bits = 5] #[discriminant_bits = 5]
pub enum SendPropType { pub enum SendPropType {
Int = 0, Int = 0,
@ -226,21 +215,6 @@ pub enum SendPropType {
NumSendPropTypes = 7, NumSendPropTypes = 7,
} }
impl fmt::Display for SendPropType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SendPropType::Int => write!(f, "Int"),
SendPropType::Float => write!(f, "Float"),
SendPropType::Vector => write!(f, "Vector"),
SendPropType::VectorXY => write!(f, "VectorXY"),
SendPropType::String => write!(f, "String"),
SendPropType::Array => write!(f, "Array"),
SendPropType::DataTable => write!(f, "DataTable"),
SendPropType::NumSendPropTypes => write!(f, "NumSendPropTypes"),
}
}
}
#[derive(EnumFlags, Copy, Clone, PartialEq, Debug)] #[derive(EnumFlags, Copy, Clone, PartialEq, Debug)]
#[repr(u16)] #[repr(u16)]
pub enum SendPropFlag { pub enum SendPropFlag {
@ -486,22 +460,13 @@ impl From<Vec<SendPropValue>> for SendPropValue {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Display)]
#[display("{definition.owner_table}::{definition.name} = {value}")]
pub struct SendProp { pub struct SendProp {
pub definition: Rc<SendPropDefinition>, pub definition: Rc<SendPropDefinition>,
pub value: SendPropValue, pub value: SendPropValue,
} }
impl fmt::Display for SendProp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}::{} = {}",
self.definition.owner_table, self.definition.name, self.value
)
}
}
pub fn read_var_int(stream: &mut Stream, signed: bool) -> ReadResult<i32> { pub fn read_var_int(stream: &mut Stream, signed: bool) -> ReadResult<i32> {
let mut result: i32 = 0; let mut result: i32 = 0;

View file

@ -1,28 +1,22 @@
use bitstream_reader::{BitRead, BitSize}; use bitstream_reader::{BitRead, BitSize};
use parse_display::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(BitRead, BitSize, Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)] #[derive(
BitRead, BitSize, Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Display,
)]
#[display("({x}, {y}, {z})")]
pub struct Vector { pub struct Vector {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
pub z: f32, pub z: f32,
} }
impl fmt::Display for Vector { #[derive(
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { BitRead, BitSize, Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Display,
write!(f, "({}, {}, {})", self.x, self.y, self.z) )]
} #[display("({x}, {y})")]
}
#[derive(BitRead, BitSize, Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
pub struct VectorXY { pub struct VectorXY {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
} }
impl fmt::Display for VectorXY {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}