feat: change enums to SCREAMING_SNAKE_CASE

This commit is contained in:
Tien Do Nam
2025-02-24 22:49:31 +01:00
parent 2e15f0ad33
commit b2bd3b060c
5 changed files with 75 additions and 19 deletions
+1 -1
View File
@@ -238,7 +238,7 @@ mod tests {
fn test_fingerprint() {
let key = generate_key();
let fingerprint = generate_token_timestamp(&key).unwrap();
let verified = verify_token_timestamp(&key.to_verifying_key(), &fingerprint);
let verified = verify_token_timestamp(&*key.to_verifying_key(), &fingerprint);
assert!(verified);
}
}
+2
View File
@@ -29,6 +29,8 @@ pub struct RegisterResponseDto {
pub download: bool,
}
// TODO: Change enums to SCREAMING_SNAKE_CASE for v3
#[derive(Clone, Debug, Deserialize, Eq, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum DeviceType {
+44 -11
View File
@@ -17,7 +17,7 @@ use uuid::Uuid;
/// A message sent by the server to the client.
#[derive(Clone, Deserialize, Eq, Serialize, Debug, PartialEq)]
#[serde(tag = "type", rename_all = "camelCase")]
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum WsServerMessage {
/// The initial message sent to the client that has just connected.
Hello {
@@ -115,7 +115,7 @@ impl ClientInfo {
/// The data that is encoded as JSON which is again encoded as base64.
/// Sent as query during websocket connection.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientInfoWithoutId {
/// The name of the peer.
@@ -151,15 +151,15 @@ impl From<ClientInfo> for ClientInfoWithoutId {
}
/// The HTTP request sent by the client to the server.
#[derive(Clone, Deserialize, Serialize, Debug)]
#[serde(tag = "type", rename_all = "camelCase")]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum WsClientMessage {
Update(ClientInfoWithoutId),
Update { info: ClientInfoWithoutId },
Offer(WsClientSdpMessage),
Answer(WsClientSdpMessage),
}
#[derive(Clone, Deserialize, Serialize, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WsClientSdpMessage {
/// The session id to correctly associate answers with offers.
@@ -371,7 +371,7 @@ impl ManagedSignalingConnection {
}
async fn send_update(tx: &mpsc::Sender<WsClientMessage>, info: ClientInfoWithoutId) -> Result<()> {
tx.send(WsClientMessage::Update(info)).await?;
tx.send(WsClientMessage::Update { info }).await?;
tracing::debug!("Sent update to the server");
@@ -437,14 +437,14 @@ mod tests {
assert_eq!(
encoded,
r#"{
"type": "hello",
"type": "HELLO",
"client": {
"id": "00000000-0000-0000-0000-000000000000",
"alias": "Cute Apple",
"version": "2.3",
"deviceModel": "Dell",
"deviceType": "desktop",
"fingerprint": "123"
"token": "123"
},
"peers": []
}"#
@@ -475,13 +475,13 @@ mod tests {
assert_eq!(
encoded,
r#"{
"type": "offer",
"type": "OFFER",
"peer": {
"id": "00000000-0000-0000-0000-000000000000",
"alias": "Cute Apple",
"version": "2.3",
"deviceType": "desktop",
"fingerprint": "123"
"token": "123"
},
"sessionId": "456",
"sdp": "my-sdp"
@@ -492,4 +492,37 @@ mod tests {
assert_eq!(message, decoded);
}
#[test]
fn ws_client_update_message_encoding() {
let message = WsClientMessage::Update {
info: ClientInfoWithoutId {
alias: "Cute Apple".to_string(),
version: "2.3".to_string(),
device_model: Some("Dell".to_string()),
device_type: Some(DeviceType::Desktop),
token: "123".to_string(),
},
};
let encoded = serde_json::to_string_pretty(&message).unwrap();
assert_eq!(
encoded,
r#"{
"type": "UPDATE",
"info": {
"alias": "Cute Apple",
"version": "2.3",
"deviceModel": "Dell",
"deviceType": "desktop",
"token": "123"
}
}"#
);
let decoded: WsClientMessage = serde_json::from_str(&encoded).unwrap();
assert_eq!(message, decoded);
}
}
+27 -6
View File
@@ -47,7 +47,7 @@ struct RTCTokenRequest {
/// also the status for the next step.
/// After this step, the sending peer may close the connection due to `InvalidSignature`.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "camelCase")]
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
enum RTCTokenResponse {
Ok { token: String },
PinRequired { token: String },
@@ -67,7 +67,7 @@ struct RTCPinMessage {
/// Response to the PIN message by the receiving peer.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "camelCase")]
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
enum RTCPinReceivingResponse {
Ok,
PinRequired,
@@ -76,7 +76,7 @@ enum RTCPinReceivingResponse {
/// Response to the PIN message by the sending peer.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "camelCase")]
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
enum RTCPinSendingResponse {
Ok { files: Vec<FileDto> },
PinRequired,
@@ -84,8 +84,8 @@ enum RTCPinSendingResponse {
}
/// Sent by receiving peer after receiving `RTCPinSendingResponse::Ok`.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "camelCase")]
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
enum RTCFileListResponse {
Ok {
files: HashMap<String, String>,
@@ -100,7 +100,7 @@ enum RTCFileListResponse {
/// Pair response by the sending peer.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "status", rename_all = "camelCase")]
#[serde(tag = "status", rename_all = "SCREAMING_SNAKE_CASE")]
enum RTCPairResponse {
Ok {
#[serde(rename = "publicKey")]
@@ -1376,4 +1376,25 @@ mod tests {
assert_eq!(chunks[1].iter().all(|x| *x == 1), true);
assert_eq!(chunks[2].iter().all(|x| *x == 2), true);
}
#[test]
fn rtc_file_list_response_encoding() {
let response = RTCFileListResponse::Pair {
public_key: "123".to_string(),
};
let encoded = serde_json::to_string_pretty(&response).unwrap();
assert_eq!(
encoded,
r#"{
"status": "PAIR",
"publicKey": "123"
}"#
);
let decoded: RTCFileListResponse = serde_json::from_str(&encoded).unwrap();
assert_eq!(response, decoded);
}
}
+1 -1
View File
@@ -195,7 +195,7 @@ async fn handle_socket(
}
match msg {
WsClientMessage::Update(info) => {
WsClientMessage::Update { info } => {
send_update_to_other_peers_with_lock(
&tx_map_clone,
&ip_group_clone,