use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StorageQuery<Key> {
pub key: Key,
#[serde(rename = "type")]
pub query_type: StorageQueryType,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaginatedStorageQuery<Key> {
pub key: Key,
#[serde(rename = "type")]
pub query_type: StorageQueryType,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub pagination_start_key: Option<Key>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum StorageQueryType {
Value,
Hash,
ClosestDescendantMerkleValue,
DescendantsValues,
DescendantsHashes,
}
impl StorageQueryType {
pub fn is_descendant_query(&self) -> bool {
matches!(self, Self::DescendantsValues | Self::DescendantsHashes)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StorageResult {
pub key: String,
#[serde(flatten)]
pub result: StorageResultType,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum StorageResultType {
Value(String),
Hash(String),
ClosestDescendantMerkleValue(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StorageResultErr {
pub key: String,
#[serde(flatten)]
pub error: StorageResultType,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ArchiveStorageResult {
Ok(ArchiveStorageMethodOk),
Err(ArchiveStorageMethodErr),
}
impl ArchiveStorageResult {
pub fn ok(result: Vec<StorageResult>, discarded_items: usize) -> Self {
Self::Ok(ArchiveStorageMethodOk { result, discarded_items })
}
pub fn err(error: String) -> Self {
Self::Err(ArchiveStorageMethodErr { error })
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArchiveStorageMethodOk {
pub result: Vec<StorageResult>,
pub discarded_items: usize,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArchiveStorageMethodErr {
pub error: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn storage_result() {
let item =
StorageResult { key: "0x1".into(), result: StorageResultType::Value("res".into()) };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","value":"res"}"#;
assert_eq!(ser, exp);
let dec: StorageResult = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item =
StorageResult { key: "0x1".into(), result: StorageResultType::Hash("res".into()) };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","hash":"res"}"#;
assert_eq!(ser, exp);
let dec: StorageResult = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item = StorageResult {
key: "0x1".into(),
result: StorageResultType::ClosestDescendantMerkleValue("res".into()),
};
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","closestDescendantMerkleValue":"res"}"#;
assert_eq!(ser, exp);
let dec: StorageResult = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
}
#[test]
fn storage_query() {
let item = StorageQuery { key: "0x1", query_type: StorageQueryType::Value };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"value"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item = StorageQuery { key: "0x1", query_type: StorageQueryType::Hash };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"hash"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item = StorageQuery { key: "0x1", query_type: StorageQueryType::DescendantsValues };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"descendantsValues"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item = StorageQuery { key: "0x1", query_type: StorageQueryType::DescendantsHashes };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"descendantsHashes"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item =
StorageQuery { key: "0x1", query_type: StorageQueryType::ClosestDescendantMerkleValue };
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"closestDescendantMerkleValue"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
}
#[test]
fn storage_query_paginated() {
let item = PaginatedStorageQuery {
key: "0x1",
query_type: StorageQueryType::Value,
pagination_start_key: None,
};
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"value"}"#;
assert_eq!(ser, exp);
let dec: StorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec.key, item.key);
assert_eq!(dec.query_type, item.query_type);
let dec: PaginatedStorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
let item = PaginatedStorageQuery {
key: "0x1",
query_type: StorageQueryType::Value,
pagination_start_key: Some("0x2"),
};
let ser = serde_json::to_string(&item).unwrap();
let exp = r#"{"key":"0x1","type":"value","paginationStartKey":"0x2"}"#;
assert_eq!(ser, exp);
let dec: PaginatedStorageQuery<&str> = serde_json::from_str(exp).unwrap();
assert_eq!(dec, item);
}
}