diff --git a/lib/types/tunnelbroker/message-to-tunnelbroker-types.js b/lib/types/tunnelbroker/message-to-tunnelbroker-types.js --- a/lib/types/tunnelbroker/message-to-tunnelbroker-types.js +++ b/lib/types/tunnelbroker/message-to-tunnelbroker-types.js @@ -1,10 +1,19 @@ // @flow +import type { Platform } from '../device-types.js'; + export const messageToTunnelbrokerTypes = Object.freeze({ SET_DEVICE_TOKEN: 'SetDeviceToken', + SET_DEVICE_TOKEN_WITH_PLATFORM: 'SetDeviceTokenWithPlatform', }); export type SetDeviceToken = { +type: 'SetDeviceToken', +deviceToken: string, }; + +export type SetDeviceTokenWithPlatform = { + +type: 'SetDeviceTokenWithPlatform', + +deviceToken: string, + +platform: Platform, +}; diff --git a/shared/tunnelbroker_messages/src/messages/message_to_tunnelbroker.rs b/shared/tunnelbroker_messages/src/messages/message_to_tunnelbroker.rs --- a/shared/tunnelbroker_messages/src/messages/message_to_tunnelbroker.rs +++ b/shared/tunnelbroker_messages/src/messages/message_to_tunnelbroker.rs @@ -1,4 +1,6 @@ use serde::{Deserialize, Serialize}; +use std::fmt::{Display, Formatter, Result as FmtResult}; +use std::str::FromStr; /// Message sent by device to set device token. #[derive(Serialize, Deserialize, PartialEq, Debug)] @@ -6,3 +8,80 @@ pub struct SetDeviceToken { pub device_token: String, } + +#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] +#[serde(rename_all = "lowercase")] +pub enum Platform { + Android, + IOS, + Web, + Windows, + MacOS, +} + +impl Display for Platform { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match self { + Platform::Web => write!(f, "web"), + Platform::IOS => write!(f, "ios"), + Platform::Android => write!(f, "android"), + Platform::Windows => write!(f, "windows"), + Platform::MacOS => write!(f, "macos"), + } + } +} + +impl FromStr for Platform { + type Err = serde_json::Error; + fn from_str(value: &str) -> Result { + let quoted = format!("\"{}\"", value); + serde_json::from_str("ed) + } +} + +/// Message sent by device to set device with platform. +#[derive(Serialize, Deserialize, PartialEq, Debug)] +#[serde(tag = "type", rename_all = "camelCase")] +pub struct SetDeviceTokenWithPlatform { + pub device_token: String, + pub platform: Platform, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_deserialization() { + let device_token = "some_device_token"; + let example_payload = r#"{ + "type": "SetDeviceTokenWithPlatform", + "deviceToken": "some_device_token", + "platform": "macos" + }"#; + + let deserialized: SetDeviceTokenWithPlatform = + serde_json::from_str(example_payload).unwrap(); + + assert_eq!(deserialized.device_token, device_token); + assert_eq!(deserialized.platform, Platform::MacOS); + } + + #[test] + fn test_try_from_for_platform() { + assert_eq!(Platform::from_str("web").unwrap(), Platform::Web); + assert_eq!(Platform::from_str("ios").unwrap(), Platform::IOS); + assert_eq!(Platform::from_str("android").unwrap(), Platform::Android); + assert_eq!(Platform::from_str("windows").unwrap(), Platform::Windows); + assert_eq!(Platform::from_str("macos").unwrap(), Platform::MacOS); + } + + #[test] + fn test_display_for_platform() { + assert_eq!(format!("{}", Platform::Web), "web"); + assert_eq!(format!("{}", Platform::IOS), "ios"); + assert_eq!(format!("{}", Platform::Android), "android"); + assert_eq!(format!("{}", Platform::Windows), "windows"); + assert_eq!(format!("{}", Platform::MacOS), "macos"); + } +}