use serde::{Deserialize, Serialize};
use super::{
Address, AddressBalance, FeeEstimates, HistoryCursor, HistoryPage, NetworkId, Transaction,
TransactionStatus, Txid,
};
use crate::{
domain::{Source, Timestamp},
error::{Error, ValidationError},
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Operation {
AddressBalance {
address: Address,
},
AddressHistory {
address: Address,
cursor: HistoryCursor,
},
FeeEstimates,
TransactionStatus {
txid: Txid,
},
Transaction {
txid: Txid,
},
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ContextFields")]
pub struct Context {
schema_version: u16,
network: NetworkId,
operation: Operation,
source: Source,
retrieved_at: Timestamp,
}
impl Context {
pub fn new(
network: NetworkId,
operation: Operation,
source: Source,
retrieved_at: Timestamp,
) -> Result<Self, Error> {
let address = match &operation {
Operation::AddressBalance { address } | Operation::AddressHistory { address, .. } => {
Some(address)
}
_ => None,
};
if address.is_some_and(|address| address.network() != network.network()) {
return Err(ValidationError::NetworkMismatch.into());
}
Ok(Self {
schema_version: 1,
network,
operation,
source,
retrieved_at,
})
}
#[must_use]
pub const fn schema_version(&self) -> u16 {
self.schema_version
}
#[must_use]
pub const fn network(&self) -> &NetworkId {
&self.network
}
#[must_use]
pub const fn operation(&self) -> &Operation {
&self.operation
}
#[must_use]
pub const fn source(&self) -> &Source {
&self.source
}
#[must_use]
pub const fn retrieved_at(&self) -> Timestamp {
self.retrieved_at
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ContextFields {
schema_version: u16,
network: NetworkId,
operation: Operation,
source: Source,
retrieved_at: Timestamp,
}
impl TryFrom<ContextFields> for Context {
type Error = Error;
fn try_from(value: ContextFields) -> Result<Self, Error> {
if value.schema_version != 1 {
return Err(ValidationError::UnsupportedBitcoinSchema.into());
}
Self::new(
value.network,
value.operation,
value.source,
value.retrieved_at,
)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Observation<T> {
context: Context,
value: T,
}
impl<T> Observation<T> {
#[must_use]
pub const fn context(&self) -> &Context {
&self.context
}
#[must_use]
pub const fn value(&self) -> &T {
&self.value
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields, bound(deserialize = "T: Deserialize<'de>"))]
struct ObservationFields<T> {
context: Context,
value: T,
}
macro_rules! observation_type {
($ty:ty, $method:ident, $validator:expr, $doc:literal) => {
impl Observation<$ty> {
#[doc = $doc]
pub fn $method(value: $ty, context: Context) -> Result<Self, Error> {
let valid: fn(&$ty, &Operation) -> bool = $validator;
if !valid(&value, &context.operation) {
return Err(ValidationError::ObservationOperationMismatch.into());
}
Ok(Self { context, value })
}
}
impl<'de> Deserialize<'de> for Observation<$ty> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let fields = ObservationFields::<$ty>::deserialize(deserializer)?;
Self::$method(fields.value, fields.context).map_err(serde::de::Error::custom)
}
}
};
}
observation_type!(
AddressBalance,
address_balance,
|value, operation| matches!(operation, Operation::AddressBalance { address } if address == value.address()),
"Records a balance with its matching requested address."
);
observation_type!(
HistoryPage,
address_history,
|value, operation| matches!(operation, Operation::AddressHistory { address, cursor } if address == value.address() && *cursor == value.cursor()),
"Records a page with its matching address and exact cursor."
);
observation_type!(
FeeEstimates,
fee_estimates,
|_value, operation| matches!(operation, Operation::FeeEstimates),
"Records fee estimates with their matching operation."
);
observation_type!(
TransactionStatus,
transaction_status,
|_value, operation| matches!(operation, Operation::TransactionStatus { .. }),
"Records transaction status while retaining its exact requested identifier."
);
impl Observation<Transaction> {
pub fn transaction(value: Transaction, context: Context) -> Result<Self, Error> {
if value.network() != context.network.network() {
return Err(ValidationError::NetworkMismatch.into());
}
if !matches!(context.operation, Operation::Transaction { txid } if txid == value.body().txid())
{
return Err(ValidationError::ObservationOperationMismatch.into());
}
Ok(Self { context, value })
}
}
impl<'de> Deserialize<'de> for Observation<Transaction> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let fields = ObservationFields::<Transaction>::deserialize(deserializer)?;
Self::transaction(fields.value, fields.context).map_err(serde::de::Error::custom)
}
}