123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- use borsh::{BorshDeserialize, BorshSerialize};
- use near_client::prelude::*;
- use serde::{Deserialize, Serialize};
- use std::{collections::HashSet, time::Duration};
-
- #[derive(Clone, Debug, Serialize, Deserialize)]
- pub struct PublicKeys {
- pub timeout: Duration,
- pub participants: HashSet<AccountId>,
- }
-
- #[derive(Clone, Debug, Serialize, Deserialize)]
- pub struct ExchangeMessage {
- pub account_id: AccountId,
- pub message: Data,
- }
-
- #[derive(Clone, Debug, Serialize, Deserialize)]
- pub struct Data {
- pub moderator_pk: Ed25519PublicKey,
- pub data: Vec<u8>,
- }
-
- #[derive(Clone, Debug, Serialize, Deserialize)]
- pub struct ParticipantInfo {
- pub public_key: Ed25519PublicKey,
- pub account_id: AccountId,
- }
-
- #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
- pub struct Bandwidth {
- pub speed: u32,
- pub units: String,
- }
-
- #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
- pub struct NodeInfo {
- pub region: String,
- pub bandwidth: Bandwidth,
- pub account_id: AccountId,
- }
-
- #[derive(Debug, Serialize, Deserialize)]
- #[serde(tag = "response", content = "body")]
- #[serde(rename_all = "snake_case")]
- pub enum ApiResponse<T> {
- Success(T),
- Timeout,
- }
-
- #[cfg(test)]
- mod tests {
-
- use super::*;
- use std::str::FromStr;
-
- #[test]
- fn node_info_json() {
- let s1 = serde_json::to_value(&NodeInfo {
- account_id: AccountId::from_str("account.testnet").unwrap(),
- bandwidth: Bandwidth {
- speed: 100,
- units: "mbps".to_owned(),
- },
- region: "us".to_owned(),
- })
- .unwrap();
-
- let s2 = serde_json::json!({
- "account_id": "account.testnet",
- "region": "us",
- "bandwidth": {
- "speed": 100,
- "units": "mbps"
- }
- });
-
- assert_eq!(s1, s2);
- }
-
- #[test]
- fn api_response_parsing() {
- let test_data = serde_json::to_value(&ApiResponse::Success(NodeInfo {
- account_id: AccountId::from_str("account.testnet").unwrap(),
- bandwidth: Bandwidth {
- speed: 100,
- units: "mbps".to_owned(),
- },
- region: "us".to_owned(),
- }))
- .unwrap();
-
- let test_data_match = serde_json::json!({
- "response": "success",
- "body": {
- "account_id": "account.testnet",
- "region": "us",
- "bandwidth": {
- "speed": 100,
- "units": "mbps"
- }
- }
- });
- assert_eq!(test_data, test_data_match);
-
- let test_data = serde_json::to_value(&ApiResponse::<()>::Timeout).unwrap();
- let test_data_match = serde_json::json!({
- "response": "timeout",
- });
- assert_eq!(test_data, test_data_match);
- }
- }
|