Преглед на файлове

Merge pull request #96 from Relayz-io/silvestr/issue-95-add-bandwidth-struct

Add a separate struct that holds a bandwidth information
develop
Predko Silvestr преди 2 години
родител
ревизия
506aa67d8f
No account linked to committer's email address
променени са 2 файла, в които са добавени 41 реда и са изтрити 2 реда
  1. 1
    0
      common-api/Cargo.toml
  2. 40
    2
      common-api/src/api.rs

+ 1
- 0
common-api/Cargo.toml Целия файл

29
 [dev-dependencies]
29
 [dev-dependencies]
30
 rand = "0.8.5"
30
 rand = "0.8.5"
31
 rand_chacha = "0.3"
31
 rand_chacha = "0.3"
32
+serde_json = "1"

+ 40
- 2
common-api/src/api.rs Целия файл

1
 use crate::crypto::prelude::*;
1
 use crate::crypto::prelude::*;
2
+use borsh::{BorshDeserialize, BorshSerialize};
2
 use near_account_id::AccountId;
3
 use near_account_id::AccountId;
3
 use serde::{Deserialize, Serialize};
4
 use serde::{Deserialize, Serialize};
4
 use std::{collections::HashSet, time::Duration};
5
 use std::{collections::HashSet, time::Duration};
27
     pub account_id: AccountId,
28
     pub account_id: AccountId,
28
 }
29
 }
29
 
30
 
30
-#[derive(Clone, Debug, Serialize, Deserialize)]
31
+#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
32
+pub struct Bandwidth {
33
+    pub speed: u32,
34
+    pub units: String,
35
+}
36
+
37
+#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
31
 pub struct NodeInfo {
38
 pub struct NodeInfo {
32
     pub region: String,
39
     pub region: String,
33
-    pub bandwidth: String,
40
+    pub bandwidth: Bandwidth,
34
     pub account_id: AccountId,
41
     pub account_id: AccountId,
35
 }
42
 }
43
+
44
+#[cfg(test)]
45
+mod tests {
46
+
47
+    use super::*;
48
+    use std::str::FromStr;
49
+
50
+    #[test]
51
+    fn node_info_json() {
52
+        let s1 = serde_json::to_value(&NodeInfo {
53
+            account_id: AccountId::from_str("account.testnet").unwrap(),
54
+            bandwidth: Bandwidth {
55
+                speed: 100,
56
+                units: "mbps".to_owned(),
57
+            },
58
+            region: "us".to_owned(),
59
+        })
60
+        .unwrap();
61
+
62
+        let s2 = serde_json::json!({
63
+            "account_id": "account.testnet",
64
+            "region": "us",
65
+            "bandwidth": {
66
+                "speed": 100,
67
+                "units": "mbps"
68
+            }
69
+        });
70
+
71
+        assert_eq!(s1, s2);
72
+    }
73
+}

Loading…
Отказ
Запис