Page MenuHomePhabricator

D12937.id42950.diff
No OneTemporary

D12937.id42950.diff

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 util_macros::TagAwareDeserialize;
/// Message sent by device to set device token.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
@@ -6,3 +8,110 @@
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 From<Platform> for String {
+ fn from(value: Platform) -> Self {
+ value.to_string().to_lowercase()
+ }
+}
+impl TryFrom<String> for Platform {
+ type Error = serde_json::Error;
+
+ fn try_from(value: String) -> Result<Self, Self::Error> {
+ let quoted = format!("\"{}\"", value);
+ serde_json::from_str(&quoted)
+ }
+}
+
+/// Message sent by device to set device with platform.
+#[derive(Serialize, Deserialize, PartialEq, Debug, TagAwareDeserialize)]
+#[serde(tag = "type", remote = "Self", 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::try_from("web".to_string()).unwrap(),
+ Platform::Web
+ );
+ assert_eq!(
+ Platform::try_from("ios".to_string()).unwrap(),
+ Platform::IOS
+ );
+ assert_eq!(
+ Platform::try_from("android".to_string()).unwrap(),
+ Platform::Android
+ );
+ assert_eq!(
+ Platform::try_from("windows".to_string()).unwrap(),
+ Platform::Windows
+ );
+ assert_eq!(
+ Platform::try_from("macos".to_string()).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");
+ }
+
+ #[test]
+ fn test_from_for_platform() {
+ let platform = Platform::MacOS;
+
+ let platform_str: String = platform.into();
+
+ assert_eq!(platform_str, "macos".to_string());
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Sep 20, 6:12 PM (20 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2153219
Default Alt Text
D12937.id42950.diff (4 KB)

Event Timeline