Compare commits

...

5 commits

Author SHA1 Message Date
hinto.janai
1b326067c3
add ResponseBase
Some checks are pending
Audit / audit (push) Waiting to run
Deny / audit (push) Waiting to run
2024-06-19 19:02:44 -04:00
hinto.janai
f7be3e127a
add BinaryString 2024-06-19 18:29:34 -04:00
hinto-janai
15a80be526
Update rpc/types/Cargo.toml
Co-authored-by: Boog900 <boog900@tutanota.com>
2024-06-19 17:59:08 -04:00
hinto.janai
4830a26180
add TODO for strum 2024-06-19 17:57:11 -04:00
hinto.janai
87e99da0eb
add Status::Other(String) 2024-06-17 14:50:52 -04:00
9 changed files with 78 additions and 21 deletions

View file

@ -75,7 +75,7 @@ rayon = { version = "1.9.0", default-features = false }
serde_bytes = { version = "0.11.12", default-features = false }
serde_json = { version = "1.0.108", default-features = false }
serde = { version = "1.0.190", default-features = false }
strum = { version = "0.26.2", default-features = false }
strum = { version = "0.26.2", default-features = false } # TODO(hinto): do we need this?
thiserror = { version = "1.0.50", default-features = false }
thread_local = { version = "1.1.7", default-features = false }
tokio-util = { version = "0.7.10", default-features = false }

View file

@ -5,7 +5,7 @@ edition = "2021"
description = "Monero RPC types"
license = "MIT"
authors = ["hinto-janai"]
repository = "https://github.com/Cuprate/cuprate/tree/main/rpc/cuprate-rpc-types"
repository = "https://github.com/Cuprate/cuprate/tree/main/rpc/types"
keywords = ["cuprate", "rpc", "types", "monero"]
[features]

View file

@ -46,15 +46,17 @@ TODO: fix doc links when types are ready.
# Mixed types
Note that some types within [`other`] mix JSON & binary together, i.e.,
the message overall is JSON, however some fields contain binary
values, for example:
values inside JSON strings, for example:
```json
{
"string": "",
"float": 30.0,
"integer": 30,
"binary": /* serialized binary */
"binary": "<serialized binary>"
}
```
`binary` here is (de)serialized as a normal [`String`]. In order to be clear on which fields contain binary data, the struct fields that have them will use [`crate::data::BinaryString`] instead of [`String`].
TODO: list the specific types.

View file

@ -0,0 +1,29 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
//---------------------------------------------------------------------------------------------------- BinaryString
/// TODO
///
/// ```rust
/// use serde::Deserialize;
/// use serde_json::from_str;
/// use cuprate_rpc_types::data::BinaryString;
///
/// #[derive(Deserialize)]
/// struct Key {
/// key: BinaryString,
/// }
///
/// let binary = r"<22>\b<><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
/// let json = format!("{{\"key\":\"{binary}\"}}");
/// let key = from_str::<Key>(&json).unwrap();
/// let binary: BinaryString = key.key;
/// ```
pub type BinaryString = String;
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -0,0 +1,9 @@
//! Data structures that appear in other types.
//!
//! TODO
mod binary_string;
mod response_base;
pub use binary_string::BinaryString;
pub use response_base::ResponseBase;

View file

@ -1,8 +1,16 @@
//! Data structures that appear in other types.
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use crate::Status;
//---------------------------------------------------------------------------------------------------- TODO
//---------------------------------------------------------------------------------------------------- ResponseBase
/// TODO
pub struct ResponseBase {
/// TODO
status: Status,
/// TODO
untrusted: bool,
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]

View file

@ -35,7 +35,7 @@ define_request_and_response! {
//
// If there are any additional attributes (`/// docs` or `#[derive]`s)
// for the struct, they go here, e.g.:
#[derive(Copy)]
// #[derive(Copy)]
Response {
// Within the `{}` is an infinite matching pattern of:
// ```

View file

@ -95,14 +95,12 @@
)]
//---------------------------------------------------------------------------------------------------- Use
// Misc types.
mod macros;
mod status;
pub use status::Status;
// Internal modules.
mod macros;
// Request/response JSON/binary/other types.
pub mod bin;
pub mod data;
pub mod json;
pub mod other;

View file

@ -4,10 +4,14 @@
use serde::{Deserialize, Serialize};
use strum::{
AsRefStr, Display, EnumCount, EnumIs, EnumIter, EnumMessage, EnumProperty, EnumString,
EnumTryAs, FromRepr, IntoStaticStr, VariantArray, VariantNames,
FromRepr, IntoStaticStr, VariantNames,
};
//---------------------------------------------------------------------------------------------------- TODO
// TODO(hinto):
// Do we need `strum`? Are there other types
// (maybe outside of this crate) that will use it?
//---------------------------------------------------------------------------------------------------- Status
/// RPC response status.
///
/// This type represents `monerod`'s frequently appearing string field, `status`.
@ -22,38 +26,44 @@ use strum::{
/// use serde_json::to_string;
/// use strum::AsRefStr;
///
/// let other = Status::Other("hello".into());
///
/// assert_eq!(to_string(&Status::Ok).unwrap(), r#""OK""#);
/// assert_eq!(to_string(&Status::Retry).unwrap(), r#""Retry""#);
/// assert_eq!(to_string(&Status::Failed).unwrap(), r#""Failed""#);
/// assert_eq!(to_string(&Status::BadRequest).unwrap(), r#""Invalid request type""#);
/// assert_eq!(to_string(&Status::BadJson).unwrap(), r#""Malformed json""#);
/// assert_eq!(to_string(&other).unwrap(), r#""hello""#);
///
/// assert_eq!(Status::Ok.as_ref(), "OK");
/// assert_eq!(Status::Retry.as_ref(), "Retry");
/// assert_eq!(Status::Failed.as_ref(), "Failed");
/// assert_eq!(Status::BadRequest.as_ref(), "Invalid request type");
/// assert_eq!(Status::BadJson.as_ref(), "Malformed json");
/// assert_eq!(other.as_ref(), "Other");
///
/// assert_eq!(format!("{}", Status::Ok), "OK");
/// assert_eq!(format!("{}", Status::Retry), "Retry");
/// assert_eq!(format!("{}", Status::Failed), "Failed");
/// assert_eq!(format!("{}", Status::BadRequest), "Invalid request type");
/// assert_eq!(format!("{}", Status::BadJson), "Malformed json");
/// assert_eq!(format!("{}", other), "Other");
///
/// assert_eq!(format!("{:?}", Status::Ok), "Ok");
/// assert_eq!(format!("{:?}", Status::Retry), "Retry");
/// assert_eq!(format!("{:?}", Status::Failed), "Failed");
/// assert_eq!(format!("{:?}", Status::BadRequest), "BadRequest");
/// assert_eq!(format!("{:?}", Status::BadJson), "BadJson");
/// assert_eq!(format!("{:?}", other), "Other(\"hello\")");
///
/// assert_eq!(format!("{:#?}", Status::Ok), "Ok");
/// assert_eq!(format!("{:#?}", Status::Retry), "Retry");
/// assert_eq!(format!("{:#?}", Status::Failed), "Failed");
/// assert_eq!(format!("{:#?}", Status::BadRequest), "BadRequest");
/// assert_eq!(format!("{:#?}", Status::BadJson), "BadJson");
/// assert_eq!(format!("{:#?}", other), "Other(\n \"hello\",\n)");
/// ```
#[derive(
Copy,
Clone,
Debug,
Default,
@ -70,10 +80,8 @@ use strum::{
EnumMessage,
EnumProperty,
EnumString,
EnumTryAs,
FromRepr,
IntoStaticStr,
VariantArray,
VariantNames,
Serialize,
Deserialize,
@ -114,10 +122,13 @@ pub enum Status {
alias = "malformed JSON"
)]
BadJson,
// TODO:
// This may not be all the string `monerod` uses.
// We could use an `Other(String)` here just in case,
// otherwise deserialization would fail.
#[serde(untagged)]
/// Some unknown other string.
///
/// This exists to act as a catch-all if `monerod` adds
/// a string and a Cuprate node hasn't updated yet.
Other(String),
}
//---------------------------------------------------------------------------------------------------- Tests