add strum, add misc module

This commit is contained in:
hinto.janai 2024-06-03 17:42:24 -04:00
parent b41b7dd557
commit 972bdfbec6
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 135 additions and 1 deletions

31
Cargo.lock generated
View file

@ -970,6 +970,12 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "heed"
version = "0.20.0"
@ -1402,6 +1408,7 @@ dependencies = [
"paste",
"serde",
"serde_json",
"strum",
]
[[package]]
@ -2049,7 +2056,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4a8caec23b7800fb97971a1c6ae365b6239aaeddfb934d6265f8505e795699d"
dependencies = [
"heck",
"heck 0.4.1",
"proc-macro2",
"quote",
"syn 2.0.60",
@ -2204,6 +2211,28 @@ dependencies = [
"spin",
]
[[package]]
name = "strum"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7993a8e3a9e88a00351486baae9522c91b123a088f76469e5bd5cc17198ea87"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.60",
]
[[package]]
name = "subtle"
version = "2.5.0"

View file

@ -71,6 +71,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 }
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

@ -15,6 +15,7 @@ default = ["serde"]
monero-serai = { workspace = true }
paste = { workspace = true }
serde = { workspace = true, optional = true }
strum = { workspace = true, features = ["derive"] }
[dev-dependencies]
serde_json = { workspace = true }

View file

@ -0,0 +1,103 @@
//! TODO.
//---------------------------------------------------------------------------------------------------- Import
use strum::{
AsRefStr, Display, EnumCount, EnumIs, EnumIter, EnumMessage, EnumProperty, EnumString,
EnumTryAs, FromRepr, IntoStaticStr, VariantArray, VariantNames,
};
//---------------------------------------------------------------------------------------------------- TODO
/// TODO
///
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/message.cpp#L40-L44>.
///
/// ## String formatting
/// ```rust
/// # use monero_rpc_types::misc::*;
/// use serde_json::to_string;
/// use strum::AsRefStr;
///
/// 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!(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!(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!("{:?}", 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!("{:#?}", 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");
/// ```
#[derive(
Copy,
Clone,
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
AsRefStr,
Display,
EnumCount,
EnumIs,
EnumIter,
EnumMessage,
EnumProperty,
EnumString,
EnumTryAs,
FromRepr,
IntoStaticStr,
VariantArray,
VariantNames,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Status {
/// TODO
#[strum(serialize = "OK")]
#[cfg_attr(feature = "serde", serde(rename = "OK"))]
#[default]
Ok,
/// TODO
Retry,
/// TODO
Failed,
/// TODO
#[strum(serialize = "Invalid request type")]
#[cfg_attr(feature = "serde", serde(rename = "Invalid request type"))]
BadRequest,
/// TODO
#[strum(serialize = "Malformed json")]
#[cfg_attr(feature = "serde", serde(rename = "Malformed json"))]
BadJson,
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}