add response bases

This commit is contained in:
hinto.janai 2024-06-23 18:55:33 -04:00
parent 9188b13330
commit be7ca959b8
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 69 additions and 10 deletions

View file

@ -0,0 +1,24 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use serde::{Deserialize, Serialize};
use crate::Status;
//---------------------------------------------------------------------------------------------------- ResponseBase
/// TODO
///
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server_commands_defs.h#L114-L122>.
#[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct AccessRequestBase {
/// TODO
client: String,
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -0,0 +1,27 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use serde::{Deserialize, Serialize};
use crate::data::ResponseBase;
//---------------------------------------------------------------------------------------------------- ResponseBase
/// TODO
///
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server_commands_defs.h#L124-L136>.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct AccessResponseBase {
/// TODO
#[serde(flatten)]
response_base: ResponseBase,
/// TODO
credits: u64,
/// TODO
top_hash: String,
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -2,8 +2,10 @@
//! //!
//! TODO //! TODO
mod access_response_base;
mod binary_string; mod binary_string;
mod response_base; mod response_base;
pub use access_response_base::AccessResponseBase;
pub use binary_string::BinaryString; pub use binary_string::BinaryString;
pub use response_base::ResponseBase; pub use response_base::ResponseBase;

View file

@ -1,10 +1,17 @@
//! TODO //! TODO
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use serde::{Deserialize, Serialize};
use crate::Status; use crate::Status;
//---------------------------------------------------------------------------------------------------- ResponseBase //---------------------------------------------------------------------------------------------------- ResponseBase
/// TODO /// TODO
///
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server_commands_defs.h#L101-L112>.
#[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct ResponseBase { pub struct ResponseBase {
/// TODO /// TODO
status: Status, status: Status,

View file

@ -60,10 +60,12 @@ use crate::constants::{
/// assert_eq!(format!("{:?}", Status::PaymentRequired), "PaymentRequired"); /// assert_eq!(format!("{:?}", Status::PaymentRequired), "PaymentRequired");
/// assert_eq!(format!("{:?}", other), "Other(\"hello\")"); /// assert_eq!(format!("{:?}", other), "Other(\"hello\")");
/// ``` /// ```
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub enum Status { pub enum Status {
// FIXME: // FIXME:
// `#[serde(rename = "")]` onlys takes raw string literals? // `#[serde(rename = "")]` only takes raw string literals?
// We have to re-type the constants here... // We have to re-type the constants here...
/// Successful RPC response, everything is OK; [`CORE_RPC_STATUS_OK`]. /// Successful RPC response, everything is OK; [`CORE_RPC_STATUS_OK`].
#[serde(rename = "OK")] #[serde(rename = "OK")]
@ -82,12 +84,12 @@ pub enum Status {
#[serde(rename = "PAYMENT REQUIRED")] #[serde(rename = "PAYMENT REQUIRED")]
PaymentRequired, PaymentRequired,
#[serde(untagged)] #[serde(other)]
/// Some unknown other string. /// Some unknown other string.
/// ///
/// This exists to act as a catch-all if `monerod` adds /// This exists to act as a catch-all if `monerod` adds
/// a string and a Cuprate node hasn't updated yet. /// a string and a Cuprate node hasn't updated yet.
Other(String), Unknown,
} }
impl From<String> for Status { impl From<String> for Status {
@ -97,7 +99,7 @@ impl From<String> for Status {
CORE_RPC_STATUS_BUSY => Self::Busy, CORE_RPC_STATUS_BUSY => Self::Busy,
CORE_RPC_STATUS_NOT_MINING => Self::NotMining, CORE_RPC_STATUS_NOT_MINING => Self::NotMining,
CORE_RPC_STATUS_PAYMENT_REQUIRED => Self::PaymentRequired, CORE_RPC_STATUS_PAYMENT_REQUIRED => Self::PaymentRequired,
_ => Self::Other(s), _ => Self::Unknown,
} }
} }
} }
@ -109,7 +111,7 @@ impl AsRef<str> for Status {
Self::Busy => CORE_RPC_STATUS_BUSY, Self::Busy => CORE_RPC_STATUS_BUSY,
Self::NotMining => CORE_RPC_STATUS_NOT_MINING, Self::NotMining => CORE_RPC_STATUS_NOT_MINING,
Self::PaymentRequired => CORE_RPC_STATUS_PAYMENT_REQUIRED, Self::PaymentRequired => CORE_RPC_STATUS_PAYMENT_REQUIRED,
Self::Other(s) => s, Self::Unknown => "UNKNOWN",
} }
} }
} }
@ -135,10 +137,7 @@ impl EpeeValue for Status {
} }
fn should_write(&self) -> bool { fn should_write(&self) -> bool {
match self { true
Self::Ok | Self::Busy | Self::NotMining | Self::PaymentRequired => true,
Self::Other(s) => !s.is_empty(),
}
} }
fn epee_default_value() -> Option<Self> { fn epee_default_value() -> Option<Self> {