1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
mod value; use crate::ProtocolVersion; use derive_more::{Constructor, From, IsVariant, TryInto}; use enum_kinds::EnumKind; use serde::Serialize; use std::{convert::TryFrom, time::SystemTime}; pub use value::*; mod sealed { pub trait Sealed {} } pub trait RequestType: Into<Request> + TryFrom<Request> + sealed::Sealed { fn protocol_version(&self) -> ProtocolVersion; } macro_rules! impl_request_type { ($t:ty => $e:expr) => { impl sealed::Sealed for $t {} impl RequestType for $t { #[inline] fn protocol_version(&self) -> ProtocolVersion { $e } } }; } #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct VersionReq; impl_request_type!(VersionReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct GetChannelInfoAllReq; impl_request_type!(GetChannelInfoAllReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct ItemValueSignInReq { pub items: Vec<u32>, } impl_request_type!(ItemValueSignInReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct ItemValueSignOutReq { pub items: Vec<u32>, } impl_request_type!(ItemValueSignOutReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct BlockListReq { #[serde(rename = "LIST-RANGE")] pub list_range: u32, } impl_request_type!(BlockListReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct ProjectListReq; impl_request_type!(ProjectListReq => ProtocolVersion::ZeroZeroThree); #[derive(Serialize, Debug, Constructor)] #[serde(rename_all = "UPPERCASE")] pub struct ItemValueSetReq { pub values: Vec<ItemSetValue>, } impl_request_type!(ItemValueSetReq => ProtocolVersion::ZeroZeroThree); #[derive(Debug, Serialize, From, IsVariant, TryInto, EnumKind)] #[enum_kind(RequestKind)] #[serde(tag = "CMD")] pub enum Request { #[serde(rename = "VERSION_REQ")] Version(VersionReq), #[serde(rename = "GET_CHANNEL_INFO_ALL_REQ")] GetChannelInfoAll(GetChannelInfoAllReq), #[serde(rename = "ITEM_VALUE_SIGN_IN_REQ")] ItemValueSignIn(ItemValueSignInReq), #[serde(rename = "ITEM_VALUE_SIGN_OUT_REQ")] ItemValueSignOut(ItemValueSignOutReq), #[serde(rename = "BLOCK_LIST_REQ")] BlockList(BlockListReq), #[serde(rename = "PROJECT_LIST_GET")] ProjectList(ProjectListReq), #[serde(rename = "ITEM_VALUE_SET")] ItemValueSet(ItemValueSetReq), } impl Request { #[inline] pub fn kind(&self) -> RequestKind { RequestKind::from(self) } } impl sealed::Sealed for Request {} impl RequestType for Request { fn protocol_version(&self) -> ProtocolVersion { match self { Request::Version(v) => v.protocol_version(), Request::GetChannelInfoAll(v) => v.protocol_version(), Request::ItemValueSignIn(v) => v.protocol_version(), Request::ItemValueSignOut(v) => v.protocol_version(), Request::BlockList(v) => v.protocol_version(), Request::ProjectList(v) => v.protocol_version(), Request::ItemValueSet(v) => v.protocol_version(), } } } #[derive(Debug, Serialize)] #[serde(rename_all = "UPPERCASE")] pub struct RequestEnvelope { #[serde(flatten)] pub body: Request, pub protocol: ProtocolVersion, #[serde(serialize_with = "serialize_enet_timestamp")] pub timestamp: SystemTime, } impl RequestEnvelope { pub fn new(request: impl RequestType) -> Self { let protocol = request.protocol_version(); Self::_new(request.into(), protocol) } #[inline(never)] fn _new(request: Request, protocol: ProtocolVersion) -> Self { Self { body: request, protocol, timestamp: SystemTime::now(), } } } fn serialize_enet_timestamp<S>(value: &SystemTime, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { let s = value .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs() .to_string(); s.serialize(serializer) }