Skip to content

Commit

Permalink
Merge pull request #192 from theangryangel/feat-with-request-id-trait
Browse files Browse the repository at this point in the history
feat: with_request_id
fix: missing io-util feature in tokio dep
  • Loading branch information
theangryangel authored Sep 17, 2024
2 parents d9a600b + 6dc4301 commit c860dd7
Show file tree
Hide file tree
Showing 34 changed files with 310 additions and 12 deletions.
2 changes: 1 addition & 1 deletion insim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ insim_pth = { path = "../insim_pth", optional = true, version = "2.1.0" }
insim_smx = { path = "../insim_smx", optional = true, version = "2.1.0" }
serde = { workspace = true, features = ["derive"], optional = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt", "time", "net", "macros", "sync", "tracing"], optional = true }
tokio = { workspace = true, features = ["time", "net", "macros", "sync", "tracing", "io-util"], optional = true }
tokio-tungstenite = { workspace = true, optional = true }
tracing = { workspace = true }

Expand Down
6 changes: 6 additions & 0 deletions insim/src/identifiers/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ impl DerefMut for ClickId {
&mut self.0
}
}

impl From<u8> for ClickId {
fn from(value: u8) -> Self {
Self(value)
}
}
6 changes: 6 additions & 0 deletions insim/src/identifiers/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ impl DerefMut for ConnectionId {
&mut self.0
}
}

impl From<u8> for ConnectionId {
fn from(value: u8) -> Self {
Self(value)
}
}
6 changes: 6 additions & 0 deletions insim/src/identifiers/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ impl DerefMut for PlayerId {
&mut self.0
}
}

impl From<u8> for PlayerId {
fn from(value: u8) -> Self {
Self(value)
}
}
6 changes: 6 additions & 0 deletions insim/src/identifiers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ impl DerefMut for RequestId {
&mut self.0
}
}

impl From<u8> for RequestId {
fn from(value: u8) -> Self {
Self(value)
}
}
2 changes: 2 additions & 0 deletions insim/src/insim/axm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ pub struct Axm {
#[br(count = numo)]
pub info: Vec<ObjectInfo>,
}

impl_typical_with_request_id!(Axm);
4 changes: 4 additions & 0 deletions insim/src/insim/btn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct Bfn {
pub inst: BtnInst,
}

impl_typical_with_request_id!(Bfn);

#[binrw]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
Expand Down Expand Up @@ -170,6 +172,8 @@ pub struct Btn {
pub text: String,
}

impl_typical_with_request_id!(Btn);

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
9 changes: 5 additions & 4 deletions insim/src/insim/cim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ impl From<u8> for CimSubModeNormal {
2 => Self::WheelDamage,
3 => Self::LiveSettings,
4 => Self::PitInstructions,
_ => {
other => {
unreachable!(
"Unhandled CimSubModeNormal. Perhaps a programming error or protocol update?"
"Unhandled CimSubModeNormal. Perhaps a programming error or protocol update? Found {}, expected 0-4.",
other
)
},
}
Expand Down Expand Up @@ -133,9 +134,9 @@ impl From<u8> for CimSubModeGarage {
6 => Self::Tyres,
7 => Self::Aero,
8 => Self::Pass,
_ => {
other => {
unreachable!(
"Unhandled CimSubModeGarage. Perhaps a programming error or protocol update?"
"Unhandled CimSubModeGarage. Perhaps a programming error or protocol update? Found {}, expected 0-8", other
)
},
}
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ pub struct Cpp {
/// State flags to set
pub flags: StaFlags,
}

impl_typical_with_request_id!(Cpp);
2 changes: 2 additions & 0 deletions insim/src/insim/hcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ pub struct Hcp {
// TODO make this work with [Vehicle]
pub info: [HcpCarHandicap; 32],
}

impl_typical_with_request_id!(Hcp);
2 changes: 2 additions & 0 deletions insim/src/insim/ipb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ impl Ipb {
}
}

impl_typical_with_request_id!(Ipb);

#[cfg(test)]
mod tests {
use std::io::{Cursor, Seek};
Expand Down
26 changes: 25 additions & 1 deletion insim/src/insim/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use insim_core::{
string::{binrw_parse_codepage_string, binrw_write_codepage_string},
};

use crate::{identifiers::RequestId, VERSION};
use crate::{identifiers::RequestId, WithRequestId, VERSION};

bitflags! {
/// Flags for the [Init] packet flags field.
Expand Down Expand Up @@ -57,6 +57,15 @@ impl IsiFlags {
}
}

impl From<IsiFlags> for Isi {
fn from(value: IsiFlags) -> Self {
Self {
flags: value,
..Default::default()
}
}
}

#[binrw]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
Expand Down Expand Up @@ -122,6 +131,21 @@ impl Default for Isi {
}
}

impl_typical_with_request_id!(Isi);

impl WithRequestId for IsiFlags {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Isi {
reqi: reqi.into(),
flags: self,
..Default::default()
}
}
}

#[cfg(test)]
mod tests {
use std::{io::Cursor, time::Duration};
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/jrr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ pub struct Jrr {
/// 0: use default start point / Flags = 0x80: set start point
pub startpos: ObjectInfo,
}

impl_typical_with_request_id!(Jrr);
2 changes: 2 additions & 0 deletions insim/src/insim/mal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ impl Mal {
}
}

impl_typical_with_request_id!(Mal);

#[cfg(test)]
mod tests {
use std::io::{Cursor, Seek};
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ pub struct Mod {
/// Screen height. Zero to switch to windowed mode.
pub height: i32,
}

impl_typical_with_request_id!(Mod);
2 changes: 2 additions & 0 deletions insim/src/insim/mst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Mst {
pub msg: String,
}

impl_typical_with_request_id!(Mst);

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/mtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct Mtc {
pub text: String,
}

impl_typical_with_request_id!(Mtc);

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/oco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ pub struct Oco {
/// Bulbs/lights to manipulate
pub data: OcoLights,
}

impl_typical_with_request_id!(Oco);
2 changes: 2 additions & 0 deletions insim/src/insim/plc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ pub struct Plc {
pub cars: PlcAllowedCarsSet,
}

impl_typical_with_request_id!(Plc);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/plh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct Plh {
pub hcaps: Vec<PlayerHandicap>,
}

impl_typical_with_request_id!(Plh);

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
2 changes: 2 additions & 0 deletions insim/src/insim/reo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ impl Default for Reo {
}
}
}

impl_typical_with_request_id!(Reo);
2 changes: 2 additions & 0 deletions insim/src/insim/rip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ pub struct Rip {
#[br(parse_with = binrw_parse_codepage_string::<64, _>)]
pub rname: String,
}

impl_typical_with_request_id!(Rip);
2 changes: 2 additions & 0 deletions insim/src/insim/scc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ pub struct Scc {
#[brw(pad_after = 2)]
pub ingamecam: CameraView,
}

impl_typical_with_request_id!(Scc);
2 changes: 2 additions & 0 deletions insim/src/insim/sch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ pub struct Sch {
#[brw(pad_after = 2)]
pub flags: SchFlags,
}

impl_typical_with_request_id!(Sch);
2 changes: 2 additions & 0 deletions insim/src/insim/sfp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pub struct Sfp {
#[bw(map = |&x| x as u8)]
pub onoff: bool,
}

impl_typical_with_request_id!(Sfp);
52 changes: 51 additions & 1 deletion insim/src/insim/small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitflags::bitflags;
use insim_core::binrw::{self, binrw, BinRead, BinWrite};

use super::{PlcAllowedCarsSet, VtnAction};
use crate::{identifiers::RequestId, Packet};
use crate::{identifiers::RequestId, Packet, WithRequestId};

bitflags! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy, Default)]
Expand Down Expand Up @@ -186,6 +186,18 @@ impl From<LclFlags> for Packet {
}
}

impl WithRequestId for LclFlags {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Small {
reqi: reqi.into(),
subt: SmallType::Lcl(self),
}
}
}

impl From<LcsFlags> for Packet {
fn from(value: LcsFlags) -> Self {
Self::Small(Small {
Expand All @@ -195,6 +207,18 @@ impl From<LcsFlags> for Packet {
}
}

impl WithRequestId for LcsFlags {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Small {
reqi: reqi.into(),
subt: SmallType::Lcs(self),
}
}
}

impl From<PlcAllowedCarsSet> for Packet {
fn from(value: PlcAllowedCarsSet) -> Self {
Self::Small(Small {
Expand All @@ -204,6 +228,30 @@ impl From<PlcAllowedCarsSet> for Packet {
}
}

impl WithRequestId for PlcAllowedCarsSet {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Small {
reqi: reqi.into(),
subt: SmallType::Alc(self),
}
}
}

impl WithRequestId for SmallType {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Small {
reqi: reqi.into(),
subt: self,
}
}
}

impl BinRead for SmallType {
type Args<'a> = ();

Expand Down Expand Up @@ -280,6 +328,8 @@ pub struct Small {
pub subt: SmallType,
}

impl_typical_with_request_id!(Small);

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
26 changes: 25 additions & 1 deletion insim/src/insim/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use insim_core::{
string::{binrw_parse_codepage_string, binrw_write_codepage_string},
};

use crate::identifiers::RequestId;
use crate::{identifiers::RequestId, WithRequestId};

#[binrw]
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -44,3 +44,27 @@ pub struct Ssh {
#[bw(write_with = binrw_write_codepage_string::<32, _>)]
pub name: String,
}

impl_typical_with_request_id!(Ssh);

impl From<SshError> for Ssh {
fn from(error: SshError) -> Self {
Self {
error,
..Default::default()
}
}
}

impl WithRequestId for SshError {
fn with_request_id<R: Into<crate::identifiers::RequestId>>(
self,
reqi: R,
) -> impl Into<crate::Packet> + std::fmt::Debug {
Ssh {
reqi: reqi.into(),
error: self,
..Default::default()
}
}
}
Loading

0 comments on commit c860dd7

Please sign in to comment.