diff --git a/src/core.rs b/src/core.rs index 27bf0d3c..ed217166 100644 --- a/src/core.rs +++ b/src/core.rs @@ -476,6 +476,30 @@ impl> From for EitherSide { } } +#[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize, derive_more::Display)] +#[display(fmt = "{}-{}", "&*_0[0]", "&*_0[1]")] +pub struct ClientVersion(pub [String; 2]); + +impl ClientVersion { + pub fn new(name: String, version: String) -> Self { + let array = [name, version]; + ClientVersion { 0: array } + } +} + +impl std::ops::Deref for ClientVersion { + type Target = [String; 2]; + fn deref(&self) -> &[String; 2] { + &self.0 + } +} + +impl std::ops::DerefMut for ClientVersion { + fn deref_mut(&mut self) -> &mut [String; 2] { + &mut self.0 + } +} + #[derive(PartialEq, Eq, Clone, Debug, Hash, Deserialize, Serialize, derive_more::Display)] #[serde(transparent)] pub struct Phase(pub Cow<'static, str>); diff --git a/src/core/server_messages.rs b/src/core/server_messages.rs index d0fa5264..7a85f2a6 100644 --- a/src/core/server_messages.rs +++ b/src/core/server_messages.rs @@ -1,4 +1,4 @@ -use super::{AppID, Mailbox, Mood, MySide, Nameplate, Phase, TheirSide}; +use super::{AppID, ClientVersion, Mailbox, Mood, MySide, Nameplate, Phase, TheirSide}; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; @@ -135,10 +135,16 @@ impl EncryptedMessage { pub enum OutboundMessage { #[display(fmt = "SubmitPermission({})", _0)] SubmitPermission(SubmitPermission), - #[display(fmt = "Bind {{ appid: {}, side: {} }}", appid, side)] + #[display( + fmt = "Bind {{ appid: {}, side: {}, client_version: {} }}", + appid, + side, + client_version + )] Bind { appid: AppID, side: MySide, + client_version: ClientVersion, }, List, Allocate, @@ -177,7 +183,14 @@ pub enum OutboundMessage { impl OutboundMessage { pub fn bind(appid: AppID, side: MySide) -> Self { - OutboundMessage::Bind { appid, side } + let client_name: String = String::from("rust"); + let client_version_string: String = String::from(env!("CARGO_PKG_VERSION")); + let client_version = ClientVersion::new(client_name, client_version_string); + OutboundMessage::Bind { + appid, + side, + client_version, + } } pub fn claim(nameplate: impl Into) -> Self { @@ -254,9 +267,11 @@ pub enum InboundMessage { mod test { use super::*; use serde_json::{from_str, json, Value}; + use std::ops::Deref; #[test] fn test_bind() { + let client_version_string: String = String::from(env!("CARGO_PKG_VERSION")); let m1 = OutboundMessage::bind( AppID::new("appid"), MySide::unchecked_from_string(String::from("side1")), @@ -266,10 +281,27 @@ mod test { assert_eq!( m2, json!({"type": "bind", "appid": "appid", - "side": "side1"}) + "side": "side1", "client_version": ["rust", client_version_string]}) ); } + #[test] + fn test_client_version_string_rep() { + let client_version = ClientVersion::new(String::from("foo"), String::from("1.0.2")); + + assert_eq!(client_version.to_string(), "foo-1.0.2") + } + + #[test] + fn test_client_version_deref() { + let client_version = ClientVersion::new(String::from("bar"), String::from("0.8.9")); + + assert_eq!( + client_version.deref(), + &[String::from("bar"), String::from("0.8.9")] + ) + } + #[test] fn test_list() { let m1 = OutboundMessage::List;