1use super::{AuthorisationKind, CmdError, DataAuthKind, QueryResponse};
11use crate::{
12 Error, Map, MapAddress as Address, MapEntryActions as Changes,
13 MapPermissionSet as PermissionSet, PublicKey, XorName,
14};
15use serde::{Deserialize, Serialize};
16use std::fmt;
17
18#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
20pub enum MapRead {
21 Get(Address),
23 GetValue {
25 address: Address,
27 key: Vec<u8>,
29 },
30 GetShell(Address),
32 GetVersion(Address),
34 ListEntries(Address),
36 ListKeys(Address),
38 ListValues(Address),
40 ListPermissions(Address),
42 ListUserPermissions {
44 address: Address,
46 user: PublicKey,
48 },
49}
50
51#[allow(clippy::large_enum_variant)]
53#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
54pub enum MapWrite {
55 New(Map),
57 Delete(Address),
59 Edit {
61 address: Address,
63 changes: Changes,
65 },
66 DelUserPermissions {
68 address: Address,
70 user: PublicKey,
72 version: u64,
74 },
75 SetUserPermissions {
77 address: Address,
79 user: PublicKey,
81 permissions: PermissionSet,
83 version: u64,
85 },
86}
87
88impl MapRead {
89 pub fn error(&self, error: Error) -> QueryResponse {
92 use MapRead::*;
93 match *self {
94 Get(_) => QueryResponse::GetMap(Err(error)),
95 GetValue { .. } => QueryResponse::GetMapValue(Err(error)),
96 GetShell(_) => QueryResponse::GetMapShell(Err(error)),
97 GetVersion(_) => QueryResponse::GetMapVersion(Err(error)),
98 ListEntries(_) => QueryResponse::ListMapEntries(Err(error)),
99 ListKeys(_) => QueryResponse::ListMapKeys(Err(error)),
100 ListValues(_) => QueryResponse::ListMapValues(Err(error)),
101 ListPermissions(_) => QueryResponse::ListMapPermissions(Err(error)),
102 ListUserPermissions { .. } => QueryResponse::ListMapUserPermissions(Err(error)),
103 }
104 }
105
106 pub fn authorisation_kind(&self) -> AuthorisationKind {
108 use MapRead::*;
109 match *self {
110 Get(_)
111 | GetValue { .. }
112 | GetShell(_)
113 | GetVersion(_)
114 | ListEntries(_)
115 | ListKeys(_)
116 | ListValues(_)
117 | ListPermissions(_)
118 | ListUserPermissions { .. } => AuthorisationKind::Data(DataAuthKind::PrivateRead),
119 }
120 }
121
122 pub fn dst_address(&self) -> XorName {
124 use MapRead::*;
125 match self {
126 Get(ref address)
127 | GetValue { ref address, .. }
128 | GetShell(ref address)
129 | GetVersion(ref address)
130 | ListEntries(ref address)
131 | ListKeys(ref address)
132 | ListValues(ref address)
133 | ListPermissions(ref address)
134 | ListUserPermissions { ref address, .. } => *address.name(),
135 }
136 }
137}
138
139impl fmt::Debug for MapRead {
140 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
141 use MapRead::*;
142 write!(
143 formatter,
144 "Request::{}",
145 match *self {
146 Get(_) => "GetMap",
147 GetValue { .. } => "GetMapValue",
148 GetShell(_) => "GetMapShell",
149 GetVersion(_) => "GetMapVersion",
150 ListEntries(_) => "ListMapEntries",
151 ListKeys(_) => "ListMapKeys",
152 ListValues(_) => "ListMapValues",
153 ListPermissions(_) => "ListMapPermissions",
154 ListUserPermissions { .. } => "ListMapUserPermissions",
155 }
156 )
157 }
158}
159
160impl MapWrite {
161 pub fn error(&self, error: Error) -> CmdError {
164 CmdError::Data(error)
165 }
166
167 pub fn authorisation_kind(&self) -> AuthorisationKind {
169 AuthorisationKind::Data(DataAuthKind::Write)
170 }
171
172 pub fn dst_address(&self) -> XorName {
174 use MapWrite::*;
175 match self {
176 New(ref data) => *data.name(),
177 Delete(ref address)
178 | SetUserPermissions { ref address, .. }
179 | DelUserPermissions { ref address, .. }
180 | Edit { ref address, .. } => *address.name(),
181 }
182 }
183}
184
185impl fmt::Debug for MapWrite {
186 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
187 use MapWrite::*;
188 write!(
189 formatter,
190 "Request::{}",
191 match *self {
192 New(_) => "NewMap",
193 Delete(_) => "DeleteMap",
194 SetUserPermissions { .. } => "SetMapUserPermissions",
195 DelUserPermissions { .. } => "DelMapUserPermissions",
196 Edit { .. } => "EditMap",
197 }
198 )
199 }
200}