safe_nd/messaging/
transfer.rs1use super::{
11 AuthorisationKind, CmdError, MiscAuthKind, MoneyAuthKind, QueryResponse, TransferError,
12};
13#[cfg(feature = "simulated-payouts")]
14use crate::Transfer;
15use crate::{DebitAgreementProof, Error, PublicKey, SignedTransfer, XorName};
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19#[allow(clippy::large_enum_variant)]
21#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
22pub enum TransferCmd {
23 #[cfg(feature = "simulated-payouts")]
24 SimulatePayout(Transfer),
26 ValidateTransfer(SignedTransfer),
28 RegisterTransfer(DebitAgreementProof),
30}
31
32#[allow(clippy::large_enum_variant)]
34#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
35pub enum TransferQuery {
36 GetReplicaKeys(PublicKey),
38 GetBalance(PublicKey),
40 GetHistory {
42 at: PublicKey,
44 since_version: usize,
46 },
47}
48
49impl TransferCmd {
50 pub fn error(&self, error: Error) -> CmdError {
53 use CmdError::*;
54 use TransferCmd::*;
55 use TransferError::*;
56 match *self {
57 ValidateTransfer(_) => Transfer(TransferValidation(error)),
58 RegisterTransfer(_) => Transfer(TransferRegistration(error)),
59 #[cfg(feature = "simulated-payouts")]
60 SimulatePayout(_) => Transfer(TransferRegistration(error)),
61 }
62 }
63
64 pub fn authorisation_kind(&self) -> AuthorisationKind {
66 use TransferCmd::*;
67 match self {
68 RegisterTransfer(_) => AuthorisationKind::None, ValidateTransfer(_) => AuthorisationKind::Misc(MiscAuthKind::WriteAndTransfer),
70 #[cfg(feature = "simulated-payouts")]
71 SimulatePayout(_) => AuthorisationKind::None,
72 }
73 }
74
75 pub fn dst_address(&self) -> XorName {
77 use TransferCmd::*;
78 match self {
79 RegisterTransfer(ref proof) => XorName::from(proof.from()), ValidateTransfer(ref signed_transfer) => XorName::from(signed_transfer.from()), #[cfg(feature = "simulated-payouts")]
82 SimulatePayout(ref transfer) => XorName::from(transfer.from()), }
84 }
85}
86
87impl fmt::Debug for TransferCmd {
88 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
89 use TransferCmd::*;
90 write!(
91 formatter,
92 "TransferCmd::{}",
93 match *self {
94 RegisterTransfer { .. } => "RegisterTransfer",
95 ValidateTransfer { .. } => "ValidateTransfer",
96 #[cfg(feature = "simulated-payouts")]
97 SimulatePayout { .. } => "SimulatePayout",
98 }
99 )
100 }
101}
102
103impl TransferQuery {
104 pub fn error(&self, error: Error) -> QueryResponse {
118 use TransferQuery::*;
119 match *self {
120 GetReplicaKeys(_) => QueryResponse::GetReplicaKeys(Err(error)),
121 GetBalance(_) => QueryResponse::GetBalance(Err(error)),
122 GetHistory { .. } => QueryResponse::GetHistory(Err(error)),
123 }
124 }
125
126 pub fn authorisation_kind(&self) -> AuthorisationKind {
128 use TransferQuery::*;
129 match self {
130 GetBalance(_) => AuthorisationKind::Money(MoneyAuthKind::ReadBalance), GetReplicaKeys(_) => AuthorisationKind::None, GetHistory { .. } => AuthorisationKind::Money(MoneyAuthKind::ReadHistory), }
134 }
135
136 pub fn dst_address(&self) -> XorName {
138 use TransferQuery::*;
139 match self {
140 GetBalance(at) | GetReplicaKeys(at) | GetHistory { at, .. } => XorName::from(*at),
141 }
142 }
143}
144
145impl fmt::Debug for TransferQuery {
146 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
147 use TransferQuery::*;
148 write!(
149 formatter,
150 "TransferQuery::{}",
151 match *self {
152 GetBalance(_) => "GetBalance",
153 GetReplicaKeys(_) => "GetReplicaKeys",
154 GetHistory { .. } => "GetHistory",
155 }
156 )
157 }
158}