ts_bookkeeping/
messages.rs

1use std::net::IpAddr;
2
3use slog::Logger;
4use thiserror::Error;
5use time::{Duration, OffsetDateTime};
6use tsproto_packets::commands::CommandParser;
7use tsproto_packets::packets::{Direction, InHeader, OutCommand, PacketType};
8use tsproto_types::errors::Error;
9
10use crate::*;
11
12type Result<T> = std::result::Result<T, ParseError>;
13
14#[derive(Error, Debug)]
15#[non_exhaustive]
16pub enum ParseError {
17	#[error(transparent)]
18	Base64(#[from] base64::DecodeError),
19
20	#[error("Parameter {arg} not found in {name}")]
21	ParameterNotFound { arg: &'static str, name: &'static str },
22	#[error("Parameter {arg} not found in {name}")]
23	ParameterNotFound2 { arg: String, name: String },
24	#[error("Command {0} is unknown")]
25	UnknownCommand(String),
26	#[error(transparent)]
27	StringParse(#[from] std::str::Utf8Error),
28	#[error(transparent)]
29	TsProto(#[from] tsproto_packets::Error),
30	/// Gets thrown when parsing a specific command with the wrong input.
31	#[error("Command {0} is wrong")]
32	WrongCommand(String),
33	#[error("Wrong newprotocol flag ({0})")]
34	WrongNewprotocol(bool),
35	#[error("Wrong packet type {0:?}")]
36	WrongPacketType(PacketType),
37	#[error("Wrong direction {0:?}")]
38	WrongDirection(Direction),
39	#[error("Cannot parse \"{value}\" as int for parameter {arg} ({source})")]
40	ParseInt { arg: &'static str, value: String, source: std::num::ParseIntError },
41	#[error("Cannot parse \"{value}\" as SocketAddr for parameter {arg} ({source})")]
42	ParseAddr { arg: &'static str, value: String, source: std::net::AddrParseError },
43	#[error("Cannot parse \"{value}\" as float for parameter {arg} ({source})")]
44	ParseFloat { arg: &'static str, value: String, source: std::num::ParseFloatError },
45	#[error("Cannot parse \"{value}\" as bool for parameter {arg}")]
46	ParseBool { arg: &'static str, value: String },
47	#[error("Cannot parse \"{value}\" as SocketAddr for parameter {arg} ({source})")]
48	ParseUid { arg: &'static str, value: String, source: base64::DecodeError },
49	#[error("Invalid value \"{value}\" for parameter {arg}")]
50	InvalidValue { arg: &'static str, value: String },
51}
52
53pub trait InMessageTrait {
54	fn new(logger: &Logger, header: &InHeader, args: CommandParser) -> Result<Self>
55	where Self: Sized;
56}
57
58pub trait OutMessageTrait {
59	fn to_packet(self) -> OutCommand;
60}
61
62pub trait OutMessageWithReturnTrait {
63	fn to_packet(self, return_code: Option<&str>) -> OutCommand;
64}
65
66impl OutMessageTrait for OutCommand {
67	fn to_packet(self) -> OutCommand { self }
68}
69
70pub mod s2c {
71	use super::*;
72	include!(concat!(env!("OUT_DIR"), "/s2c_messages.rs"));
73}
74pub mod c2s {
75	use super::*;
76	include!(concat!(env!("OUT_DIR"), "/c2s_messages.rs"));
77}