mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-05 10:29:32 +00:00
Compare commits
5 commits
d72c96ab9c
...
1b326067c3
Author | SHA1 | Date | |
---|---|---|---|
|
1b326067c3 | ||
|
f7be3e127a | ||
|
15a80be526 | ||
|
4830a26180 | ||
|
87e99da0eb |
9 changed files with 78 additions and 21 deletions
|
@ -75,7 +75,7 @@ rayon = { version = "1.9.0", default-features = false }
|
||||||
serde_bytes = { version = "0.11.12", default-features = false }
|
serde_bytes = { version = "0.11.12", default-features = false }
|
||||||
serde_json = { version = "1.0.108", default-features = false }
|
serde_json = { version = "1.0.108", default-features = false }
|
||||||
serde = { version = "1.0.190", 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 }
|
thiserror = { version = "1.0.50", default-features = false }
|
||||||
thread_local = { version = "1.1.7", default-features = false }
|
thread_local = { version = "1.1.7", default-features = false }
|
||||||
tokio-util = { version = "0.7.10", default-features = false }
|
tokio-util = { version = "0.7.10", default-features = false }
|
||||||
|
|
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||||
description = "Monero RPC types"
|
description = "Monero RPC types"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
authors = ["hinto-janai"]
|
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"]
|
keywords = ["cuprate", "rpc", "types", "monero"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
|
@ -46,15 +46,17 @@ TODO: fix doc links when types are ready.
|
||||||
# Mixed types
|
# Mixed types
|
||||||
Note that some types within [`other`] mix JSON & binary together, i.e.,
|
Note that some types within [`other`] mix JSON & binary together, i.e.,
|
||||||
the message overall is JSON, however some fields contain binary
|
the message overall is JSON, however some fields contain binary
|
||||||
values, for example:
|
values inside JSON strings, for example:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"string": "",
|
"string": "",
|
||||||
"float": 30.0,
|
"float": 30.0,
|
||||||
"integer": 30,
|
"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.
|
TODO: list the specific types.
|
29
rpc/types/src/data/binary_string.rs
Normal file
29
rpc/types/src/data/binary_string.rs
Normal 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::*;
|
||||||
|
}
|
9
rpc/types/src/data/mod.rs
Normal file
9
rpc/types/src/data/mod.rs
Normal 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;
|
|
@ -1,8 +1,16 @@
|
||||||
//! Data structures that appear in other types.
|
//! TODO
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Import
|
//---------------------------------------------------------------------------------------------------- Import
|
||||||
|
use crate::Status;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- TODO
|
//---------------------------------------------------------------------------------------------------- ResponseBase
|
||||||
|
/// TODO
|
||||||
|
pub struct ResponseBase {
|
||||||
|
/// TODO
|
||||||
|
status: Status,
|
||||||
|
/// TODO
|
||||||
|
untrusted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Tests
|
//---------------------------------------------------------------------------------------------------- Tests
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
|
@ -35,7 +35,7 @@ define_request_and_response! {
|
||||||
//
|
//
|
||||||
// If there are any additional attributes (`/// docs` or `#[derive]`s)
|
// If there are any additional attributes (`/// docs` or `#[derive]`s)
|
||||||
// for the struct, they go here, e.g.:
|
// for the struct, they go here, e.g.:
|
||||||
#[derive(Copy)]
|
// #[derive(Copy)]
|
||||||
Response {
|
Response {
|
||||||
// Within the `{}` is an infinite matching pattern of:
|
// Within the `{}` is an infinite matching pattern of:
|
||||||
// ```
|
// ```
|
||||||
|
|
|
@ -95,14 +95,12 @@
|
||||||
)]
|
)]
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Use
|
//---------------------------------------------------------------------------------------------------- Use
|
||||||
// Misc types.
|
mod macros;
|
||||||
mod status;
|
mod status;
|
||||||
|
|
||||||
pub use status::Status;
|
pub use status::Status;
|
||||||
|
|
||||||
// Internal modules.
|
|
||||||
mod macros;
|
|
||||||
|
|
||||||
// Request/response JSON/binary/other types.
|
|
||||||
pub mod bin;
|
pub mod bin;
|
||||||
|
pub mod data;
|
||||||
pub mod json;
|
pub mod json;
|
||||||
pub mod other;
|
pub mod other;
|
||||||
|
|
|
@ -4,10 +4,14 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use strum::{
|
use strum::{
|
||||||
AsRefStr, Display, EnumCount, EnumIs, EnumIter, EnumMessage, EnumProperty, EnumString,
|
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.
|
/// RPC response status.
|
||||||
///
|
///
|
||||||
/// This type represents `monerod`'s frequently appearing string field, `status`.
|
/// This type represents `monerod`'s frequently appearing string field, `status`.
|
||||||
|
@ -22,38 +26,44 @@ use strum::{
|
||||||
/// use serde_json::to_string;
|
/// use serde_json::to_string;
|
||||||
/// use strum::AsRefStr;
|
/// use strum::AsRefStr;
|
||||||
///
|
///
|
||||||
|
/// let other = Status::Other("hello".into());
|
||||||
|
///
|
||||||
/// assert_eq!(to_string(&Status::Ok).unwrap(), r#""OK""#);
|
/// assert_eq!(to_string(&Status::Ok).unwrap(), r#""OK""#);
|
||||||
/// assert_eq!(to_string(&Status::Retry).unwrap(), r#""Retry""#);
|
/// assert_eq!(to_string(&Status::Retry).unwrap(), r#""Retry""#);
|
||||||
/// assert_eq!(to_string(&Status::Failed).unwrap(), r#""Failed""#);
|
/// 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::BadRequest).unwrap(), r#""Invalid request type""#);
|
||||||
/// assert_eq!(to_string(&Status::BadJson).unwrap(), r#""Malformed json""#);
|
/// 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::Ok.as_ref(), "OK");
|
||||||
/// assert_eq!(Status::Retry.as_ref(), "Retry");
|
/// assert_eq!(Status::Retry.as_ref(), "Retry");
|
||||||
/// assert_eq!(Status::Failed.as_ref(), "Failed");
|
/// assert_eq!(Status::Failed.as_ref(), "Failed");
|
||||||
/// assert_eq!(Status::BadRequest.as_ref(), "Invalid request type");
|
/// assert_eq!(Status::BadRequest.as_ref(), "Invalid request type");
|
||||||
/// assert_eq!(Status::BadJson.as_ref(), "Malformed json");
|
/// assert_eq!(Status::BadJson.as_ref(), "Malformed json");
|
||||||
|
/// assert_eq!(other.as_ref(), "Other");
|
||||||
///
|
///
|
||||||
/// assert_eq!(format!("{}", Status::Ok), "OK");
|
/// assert_eq!(format!("{}", Status::Ok), "OK");
|
||||||
/// assert_eq!(format!("{}", Status::Retry), "Retry");
|
/// assert_eq!(format!("{}", Status::Retry), "Retry");
|
||||||
/// assert_eq!(format!("{}", Status::Failed), "Failed");
|
/// assert_eq!(format!("{}", Status::Failed), "Failed");
|
||||||
/// assert_eq!(format!("{}", Status::BadRequest), "Invalid request type");
|
/// assert_eq!(format!("{}", Status::BadRequest), "Invalid request type");
|
||||||
/// assert_eq!(format!("{}", Status::BadJson), "Malformed json");
|
/// assert_eq!(format!("{}", Status::BadJson), "Malformed json");
|
||||||
|
/// assert_eq!(format!("{}", other), "Other");
|
||||||
///
|
///
|
||||||
/// assert_eq!(format!("{:?}", Status::Ok), "Ok");
|
/// assert_eq!(format!("{:?}", Status::Ok), "Ok");
|
||||||
/// assert_eq!(format!("{:?}", Status::Retry), "Retry");
|
/// assert_eq!(format!("{:?}", Status::Retry), "Retry");
|
||||||
/// assert_eq!(format!("{:?}", Status::Failed), "Failed");
|
/// assert_eq!(format!("{:?}", Status::Failed), "Failed");
|
||||||
/// assert_eq!(format!("{:?}", Status::BadRequest), "BadRequest");
|
/// assert_eq!(format!("{:?}", Status::BadRequest), "BadRequest");
|
||||||
/// assert_eq!(format!("{:?}", Status::BadJson), "BadJson");
|
/// assert_eq!(format!("{:?}", Status::BadJson), "BadJson");
|
||||||
|
/// assert_eq!(format!("{:?}", other), "Other(\"hello\")");
|
||||||
///
|
///
|
||||||
/// assert_eq!(format!("{:#?}", Status::Ok), "Ok");
|
/// assert_eq!(format!("{:#?}", Status::Ok), "Ok");
|
||||||
/// assert_eq!(format!("{:#?}", Status::Retry), "Retry");
|
/// assert_eq!(format!("{:#?}", Status::Retry), "Retry");
|
||||||
/// assert_eq!(format!("{:#?}", Status::Failed), "Failed");
|
/// assert_eq!(format!("{:#?}", Status::Failed), "Failed");
|
||||||
/// assert_eq!(format!("{:#?}", Status::BadRequest), "BadRequest");
|
/// assert_eq!(format!("{:#?}", Status::BadRequest), "BadRequest");
|
||||||
/// assert_eq!(format!("{:#?}", Status::BadJson), "BadJson");
|
/// assert_eq!(format!("{:#?}", Status::BadJson), "BadJson");
|
||||||
|
/// assert_eq!(format!("{:#?}", other), "Other(\n \"hello\",\n)");
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(
|
#[derive(
|
||||||
Copy,
|
|
||||||
Clone,
|
Clone,
|
||||||
Debug,
|
Debug,
|
||||||
Default,
|
Default,
|
||||||
|
@ -70,10 +80,8 @@ use strum::{
|
||||||
EnumMessage,
|
EnumMessage,
|
||||||
EnumProperty,
|
EnumProperty,
|
||||||
EnumString,
|
EnumString,
|
||||||
EnumTryAs,
|
|
||||||
FromRepr,
|
FromRepr,
|
||||||
IntoStaticStr,
|
IntoStaticStr,
|
||||||
VariantArray,
|
|
||||||
VariantNames,
|
VariantNames,
|
||||||
Serialize,
|
Serialize,
|
||||||
Deserialize,
|
Deserialize,
|
||||||
|
@ -114,10 +122,13 @@ pub enum Status {
|
||||||
alias = "malformed JSON"
|
alias = "malformed JSON"
|
||||||
)]
|
)]
|
||||||
BadJson,
|
BadJson,
|
||||||
// TODO:
|
|
||||||
// This may not be all the string `monerod` uses.
|
#[serde(untagged)]
|
||||||
// We could use an `Other(String)` here just in case,
|
/// Some unknown other string.
|
||||||
// otherwise deserialization would fail.
|
///
|
||||||
|
/// This exists to act as a catch-all if `monerod` adds
|
||||||
|
/// a string and a Cuprate node hasn't updated yet.
|
||||||
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Tests
|
//---------------------------------------------------------------------------------------------------- Tests
|
||||||
|
|
Loading…
Reference in a new issue