From 604ecc83933eac38285d667b8cdaee3de2da7051 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 23 Oct 2024 20:32:39 -0400 Subject: [PATCH] fix/doc type serde --- rpc/types/src/json.rs | 16 ++++++++-------- types/src/address_type.rs | 20 ++++++++++++++++++++ types/src/connection_state.rs | 25 +++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/rpc/types/src/json.rs b/rpc/types/src/json.rs index ff309f1..c6f77d9 100644 --- a/rpc/types/src/json.rs +++ b/rpc/types/src/json.rs @@ -658,7 +658,7 @@ define_request_and_response! { connections: vec![ ConnectionInfo { address: "3evk3kezfjg44ma6tvesy7rbxwwpgpympj45xar5fo4qajrsmkoaqdqd.onion:18083".into(), - address_type: cuprate_rpc_types::misc::AddressType::Tor, + address_type: cuprate_types::AddressType::Tor, avg_download: 0, avg_upload: 0, connection_id: "22ef856d0f1d44cc95e84fecfd065fe2".into(), @@ -680,12 +680,12 @@ define_request_and_response! { rpc_port: 0, send_count: 3406572, send_idle_time: 30, - state: "normal".into(), + state: cuprate_types::ConnectionState::Normal, support_flags: 0 }, ConnectionInfo { address: "4iykytmumafy5kjahdqc7uzgcs34s2vwsadfjpk4znvsa5vmcxeup2qd.onion:18083".into(), - address_type: cuprate_rpc_types::misc::AddressType::Tor, + address_type: cuprate_types::AddressType::Tor, avg_download: 0, avg_upload: 0, connection_id: "c7734e15936f485a86d2b0534f87e499".into(), @@ -707,7 +707,7 @@ define_request_and_response! { rpc_port: 0, send_count: 3370566, send_idle_time: 120, - state: "normal".into(), + state: cuprate_types::ConnectionState::Normal, support_flags: 0 } ], @@ -1251,7 +1251,7 @@ define_request_and_response! { SyncInfoPeer { info: ConnectionInfo { address: "142.93.128.65:44986".into(), - address_type: AddressType::Ipv4, + address_type: cuprate_types::AddressType::Ipv4, avg_download: 1, avg_upload: 1, connection_id: "a5803c4c2dac49e7b201dccdef54c862".into(), @@ -1273,14 +1273,14 @@ define_request_and_response! { rpc_port: 18089, send_count: 32235, send_idle_time: 6, - state: "normal".into(), + state: cuprate_types::ConnectionState::Normal, support_flags: 1 } }, SyncInfoPeer { info: ConnectionInfo { address: "4iykytmumafy5kjahdqc7uzgcs34s2vwsadfjpk4znvsa5vmcxeup2qd.onion:18083".into(), - address_type: AddressType::Tor, + address_type: cuprate_types::AddressType::Tor, avg_download: 0, avg_upload: 0, connection_id: "277f7c821bc546878c8bd29977e780f5".into(), @@ -1302,7 +1302,7 @@ define_request_and_response! { rpc_port: 0, send_count: 99120, send_idle_time: 15, - state: "normal".into(), + state: cuprate_types::ConnectionState::Normal, support_flags: 0 } } diff --git a/types/src/address_type.rs b/types/src/address_type.rs index 3118293..743902d 100644 --- a/types/src/address_type.rs +++ b/types/src/address_type.rs @@ -20,6 +20,26 @@ use strum::{ /// /// Original definition: /// - +/// +/// # Serde +/// This type's `serde` implementation (de)serializes from a [`u8`]. +/// +/// ```rust +/// use cuprate_types::AddressType as A; +/// use serde_json::{to_string, from_str}; +/// +/// assert_eq!(from_str::(&"0").unwrap(), A::Invalid); +/// assert_eq!(from_str::(&"1").unwrap(), A::Ipv4); +/// assert_eq!(from_str::(&"2").unwrap(), A::Ipv6); +/// assert_eq!(from_str::(&"3").unwrap(), A::I2p); +/// assert_eq!(from_str::(&"4").unwrap(), A::Tor); +/// +/// assert_eq!(to_string(&A::Invalid).unwrap(), "0"); +/// assert_eq!(to_string(&A::Ipv4).unwrap(), "1"); +/// assert_eq!(to_string(&A::Ipv6).unwrap(), "2"); +/// assert_eq!(to_string(&A::I2p).unwrap(), "3"); +/// assert_eq!(to_string(&A::Tor).unwrap(), "4"); +/// ``` #[derive( Copy, Clone, diff --git a/types/src/connection_state.rs b/types/src/connection_state.rs index 4dbd061..69b8ed6 100644 --- a/types/src/connection_state.rs +++ b/types/src/connection_state.rs @@ -20,6 +20,26 @@ use strum::{ /// /// Original definition: /// - +/// +/// # Serde +/// This type's `serde` implementation depends on `snake_case`. +/// +/// ```rust +/// use cuprate_types::ConnectionState as C; +/// use serde_json::to_string; +/// +/// assert_eq!(to_string(&C::BeforeHandshake).unwrap(), r#""before_handshake""#); +/// assert_eq!(to_string(&C::Synchronizing).unwrap(), r#""synchronizing""#); +/// assert_eq!(to_string(&C::Standby).unwrap(), r#""standby""#); +/// assert_eq!(to_string(&C::Idle).unwrap(), r#""idle""#); +/// assert_eq!(to_string(&C::Normal).unwrap(), r#""normal""#); +/// +/// assert_eq!(C::BeforeHandshake.to_string(), "before_handshake"); +/// assert_eq!(C::Synchronizing.to_string(), "synchronizing"); +/// assert_eq!(C::Standby.to_string(), "standby"); +/// assert_eq!(C::Idle.to_string(), "idle"); +/// assert_eq!(C::Normal.to_string(), "normal"); +/// ``` #[derive( Copy, Clone, @@ -40,7 +60,8 @@ use strum::{ VariantArray, )] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))] +#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] // cuprate-rpc-types depends on snake_case +#[strum(serialize_all = "snake_case")] #[repr(u8)] pub enum ConnectionState { BeforeHandshake, @@ -75,7 +96,7 @@ impl ConnectionState { /// ```rust /// use cuprate_types::ConnectionState as C; /// - /// assert_eq!(C::from_u8(0), Some(C::BeforeHandShake)); + /// assert_eq!(C::from_u8(0), Some(C::BeforeHandshake)); /// assert_eq!(C::from_u8(1), Some(C::Synchronizing)); /// assert_eq!(C::from_u8(2), Some(C::Standby)); /// assert_eq!(C::from_u8(3), Some(C::Idle));