json: add doc-test for all types

This commit is contained in:
hinto.janai 2024-07-19 17:50:42 -04:00
parent 302c268cd8
commit 5f83e3790a
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 1112 additions and 553 deletions

View file

@ -53,6 +53,12 @@ pub(crate) fn default_zero<T: From<u8>>() -> T {
T::from(0)
}
/// Default `1` value used in request/response types.
#[inline]
pub(crate) fn default_one<T: From<u8>>() -> T {
T::from(1)
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {

File diff suppressed because it is too large Load diff

View file

@ -3,6 +3,8 @@
//---------------------------------------------------------------------------------------------------- Use
use std::mem::size_of;
#[cfg(feature = "serde")]
use crate::defaults::default_true;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -65,6 +67,60 @@ fn decompress_integer_array(array: Vec<u64>) -> Vec<u64> {
/// Upon deserialization, the presence of a `compressed_data`
/// field signifies that the [`Self::CompressedBinary`] should
/// be selected.
///
/// # JSON Example
/// ```rust
/// use cuprate_rpc_types::misc::Distribution;
/// use serde_json::*;
///
/// // Uncompressed.
/// let json = json!({
/// "amount": 2628780000_u64,
/// "base": 0_u64,
/// "distribution": "asdf",
/// "start_height": 1462078_u64
/// });
/// let d = from_value::<Distribution>(json).unwrap();
/// match d {
/// Distribution::Uncompressed {
/// start_height,
/// base,
/// distribution,
/// amount,
/// binary,
/// } => {
/// assert_eq!(start_height, 1462078);
/// assert_eq!(base, 0);
/// assert_eq!(distribution, "asdf");
/// assert_eq!(amount, 2628780000);
/// assert_eq!(binary, true);
/// },
/// Distribution::CompressedBinary { .. } => unreachable!(),
/// }
///
/// // Compressed binary.
/// let json = json!({
/// "amount": 2628780000_u64,
/// "base": 0_u64,
/// "compressed_data": "asdf",
/// "start_height": 1462078_u64
/// });
/// let d = from_value::<Distribution>(json).unwrap();
/// match d {
/// Distribution::CompressedBinary {
/// start_height,
/// base,
/// compressed_data,
/// amount,
/// } => {
/// assert_eq!(start_height, 1462078);
/// assert_eq!(base, 0);
/// assert_eq!(compressed_data, "asdf");
/// assert_eq!(amount, 2628780000);
/// },
/// Distribution::Uncompressed { .. } => unreachable!(),
/// }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
@ -77,8 +133,9 @@ pub enum Distribution {
///
/// Considering both the `binary` field and `/get_output_distribution.bin`
/// endpoint are undocumented in the first place, Cuprate could just drop support for this.
distribution: Vec<u64>,
distribution: String,
amount: u64,
#[cfg_attr(feature = "serde", serde(default = "default_true"))]
binary: bool,
},
/// Distribution data will be (de)serialized as compressed binary.
@ -95,7 +152,7 @@ impl Default for Distribution {
Self::Uncompressed {
start_height: u64::default(),
base: u64::default(),
distribution: Vec::<u64>::default(),
distribution: String::default(),
amount: u64::default(),
binary: false,
}
@ -113,7 +170,7 @@ impl Default for Distribution {
pub struct __DistributionEpeeBuilder {
pub start_height: Option<u64>,
pub base: Option<u64>,
pub distribution: Option<Vec<u64>>,
pub distribution: Option<String>,
pub amount: Option<u64>,
pub compressed_data: Option<String>,
pub binary: Option<bool>,

View file

@ -22,7 +22,7 @@ use crate::{
CORE_RPC_STATUS_BUSY, CORE_RPC_STATUS_NOT_MINING, CORE_RPC_STATUS_OK,
CORE_RPC_STATUS_PAYMENT_REQUIRED,
},
defaults::default_zero,
defaults::{default_string, default_zero},
macros::monero_definition_link,
};
@ -51,9 +51,9 @@ macro_rules! define_struct_and_impl_epee {
)*
}
) => {
$( #[$struct_attr] )*
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
$( #[$struct_attr] )*
pub struct $struct_name {
$(
$( #[$field_attr] )*
@ -142,7 +142,9 @@ define_struct_and_impl_epee! {
rpc_port: u16,
send_count: u64,
send_idle_time: u64,
ssl: bool,
// Exists in the original definition, but isn't
// used or (de)serialized for RPC purposes.
// ssl: bool,
state: String,
support_flags: u32,
}
@ -156,7 +158,9 @@ define_struct_and_impl_epee! {
)]
/// Used in [`crate::json::SetBansRequest`].
SetBan {
#[cfg_attr(feature = "serde", serde(default = "default_string"))]
host: String,
#[cfg_attr(feature = "serde", serde(default = "default_zero"))]
ip: u32,
ban: bool,
seconds: u32,

View file

@ -771,7 +771,7 @@ r#"{
"id": "0",
"method": "get_output_histogram",
"params": {
"amounts": ["20000000000"]
"amounts": [20000000000]
}
}"#;
Response =
@ -1106,13 +1106,16 @@ r#"{
"id": "0",
"jsonrpc": "2.0",
"result": {
"credits": 0,
"distributions": [{
"amount": 2628780000,
"base": 0,
"distribution": "",
"start_height": 1462078
}],
"status": "OK"
"status": "OK",
"top_hash": "",
"untrusted": false
}
}"#;
}