use super::{register::RegisterQuery, spentbook::SpentbookQuery, Error, QueryResponse};
use crate::types::{ChunkAddress, DataAddress, SpentbookAddress};
use serde::{Deserialize, Serialize};
use xor_name::XorName;
#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub struct DataQuery {
pub variant: DataQueryVariant,
pub node_index: usize,
}
#[allow(clippy::large_enum_variant)]
#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub enum DataQueryVariant {
GetChunk(ChunkAddress),
Register(RegisterQuery),
Spentbook(SpentbookQuery),
}
impl DataQueryVariant {
pub fn to_error_response(&self, error: Error) -> QueryResponse {
use DataQueryVariant::*;
match self {
GetChunk(_) => QueryResponse::GetChunk(Err(error)),
Register(q) => q.to_error_response(error),
Spentbook(q) => q.to_error_response(error),
}
}
pub fn dst_name(&self) -> XorName {
use DataQueryVariant::*;
match self {
GetChunk(address) => *address.name(),
Register(q) => q.dst_name(),
Spentbook(q) => q.dst_name(),
}
}
pub fn address(&self) -> DataAddress {
match self {
Self::GetChunk(address) => DataAddress::Bytes(*address),
Self::Register(read) => DataAddress::Register(read.dst_address()),
Self::Spentbook(read) => {
DataAddress::Spentbook(SpentbookAddress::new(*read.dst_address().name()))
}
}
}
}