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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
mod de;
mod proj;
mod update;

use crate::ProtocolVersion;
use derive_more::{From, IsVariant};
use enum_kinds::EnumKind;
use serde::{Deserialize, Deserializer};
use serde_json::Value;
use std::convert::TryFrom;
use tracing::{event, Level};

pub use proj::*;
pub use update::ItemUpdateValue;

const FIELD_NAME_PROTOCOL: &str = "PROTOCOL";
const FIELD_NAME_KIND: &str = "CMD";

mod sealed {
  pub trait Sealed {}
}

pub trait ResponseType: Into<Response> + TryFrom<Response> + sealed::Sealed {
  fn protocol_version(&self) -> ProtocolVersion;
}

macro_rules! impl_response_type {
  ($t:ty => $e:expr) => {
    impl sealed::Sealed for $t {}
    impl ResponseType for $t {
      #[inline]
      fn protocol_version(&self) -> ProtocolVersion {
        $e
      }
    }

    impl $t {
      fn deserialize_response<'de, D>(deserializer: D) -> Result<Response, D::Error>
      where
        D: Deserializer<'de>
      {
        <Self as Deserialize<'de>>::deserialize(deserializer).map(Into::into)
      }
    }
  };
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct VersionRes {
  pub firmware: String,
  pub hardware: String,
  pub enet: String,
}
impl_response_type!(VersionRes => ProtocolVersion::ZeroZeroThree);

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct GetChannelInfoAllRes {
  pub devices: Vec<u32>,
}
impl_response_type!(GetChannelInfoAllRes => ProtocolVersion::ZeroZeroThree);

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct ItemValueRes {}
impl_response_type!(ItemValueRes => ProtocolVersion::ZeroZeroThree);

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct ItemValueSignInRes {}
impl_response_type!(ItemValueSignInRes => ProtocolVersion::ZeroZeroThree);

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct ProjectListRes {
  pub project_id: String,
  pub items: Vec<proj::ProjectItem>,
  pub lists: Vec<proj::ProjectList>,
}
impl_response_type!(ProjectListRes => ProtocolVersion::ZeroZeroThree);

#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub struct ItemUpdateInd {
  pub values: Vec<ItemUpdateValue>,
}
impl_response_type!(ItemUpdateInd => ProtocolVersion::ZeroZeroThree);

#[derive(Debug)]
pub struct UnknownRes {
  pub kind: String,
  pub protocol: String,
  pub values: Value,
}

#[derive(Debug, From, IsVariant, EnumKind)]
#[enum_kind(ResponseKind)]
pub enum Response {
  Version(VersionRes),

  GetChannelInfoAll(GetChannelInfoAllRes),

  ItemValueSignIn(ItemValueSignInRes),

  ItemValue(ItemValueRes),

  ProjectList(ProjectListRes),

  ItemUpdate(ItemUpdateInd),

  Unknown(UnknownRes),
}

macro_rules! try_into {
  ($variant:ident => $ty:ty) => {
    impl TryFrom<Response> for $ty {
      type Error = Response;

      #[inline]
      fn try_from(value: Response) -> Result<Self, Self::Error> {
        match value {
          Response::$variant(v) => Ok(v),
          _ => Err(value),
        }
      }
    }
  };
}

try_into!(Version => VersionRes);
try_into!(GetChannelInfoAll => GetChannelInfoAllRes);
try_into!(ProjectList => ProjectListRes);
try_into!(ItemUpdate => ItemUpdateInd);
try_into!(ItemValue => ItemValueRes);
try_into!(ItemValueSignIn => ItemValueSignInRes);

impl Response {
  #[inline]
  pub fn kind(&self) -> ResponseKind {
    ResponseKind::from(self)
  }
}

macro_rules! match_response {
  ($kind:ident, $protocol:ident, $deserializer:ident => {
    $(($k:ident, $v:ident) => $t:ty),*$(,)?
  }) => {
    match (&$kind, &$protocol) {
      $(
        (de::ResponseKind::$k, ProtocolVersion::$v) => <$t>::deserialize_response($deserializer),
      )+
      (de::ResponseKind::Unknown(_), _) | (_, ProtocolVersion::Unknown(_)) => {
        reconstruct($kind, $protocol, $deserializer)
      }
    }
  };
}

impl Response {
  fn deserialize<'de, D>(
    kind: de::ResponseKind,
    protocol: ProtocolVersion,
    deserializer: D,
  ) -> Result<Self, D::Error>
  where
    D: Deserializer<'de>,
  {
    match_response!(kind, protocol, deserializer => {
      (Version, ZeroZeroThree) => VersionRes,
      (GetChannelInfoAll, ZeroZeroThree) => GetChannelInfoAllRes,
      (ItemValue, ZeroZeroThree) => ItemValueRes,
      (ItemValueSignIn, ZeroZeroThree) => ItemValueSignInRes,
      (ProjectList, ZeroZeroThree) => ProjectListRes,
      (ItemUpdate, ZeroZeroThree) => ItemUpdateInd,
    })
  }
}

fn reconstruct<'de, D>(
  kind: de::ResponseKind,
  protocol: ProtocolVersion,
  deserializer: D,
) -> Result<Response, D::Error>
where
  D: Deserializer<'de>,
{
  let json = serde_json::Value::deserialize(deserializer)?;

  event!(target: "enet-proto::res", Level::WARN, %kind, %protocol, %json, "received unknown response type");
  Ok(Response::Unknown(UnknownRes {
    kind: kind.to_string(),
    protocol: protocol.to_string(),
    values: json,
  }))
}