safe_nd/messaging/
sequence.rs

1// Copyright 2019 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10use super::{AuthorisationKind, CmdError, DataAuthKind, QueryResponse};
11use crate::{
12    Error, PublicKey, Sequence, SequenceAddress as Address, SequenceEntry as Entry,
13    SequenceIndex as Index, SequencePrivatePermissions as PrivatePermissions,
14    SequencePublicPermissions as PublicPermissions, SequenceUser as User,
15    SequenceWriteOp as WriteOp, XorName,
16};
17use serde::{Deserialize, Serialize};
18use std::fmt;
19
20/// TODO: docs
21#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
22pub enum SequenceRead {
23    /// Get Sequence from the network.
24    Get(Address),
25    /// Get a range of entries from an Sequence object on the network.
26    GetRange {
27        /// Sequence address.
28        address: Address,
29        /// Range of entries to fetch.
30        ///
31        /// For example, get 10 last entries:
32        /// range: (Index::FromEnd(10), Index::FromEnd(0))
33        ///
34        /// Get all entries:
35        /// range: (Index::FromStart(0), Index::FromEnd(0))
36        ///
37        /// Get first 5 entries:
38        /// range: (Index::FromStart(0), Index::FromStart(5))
39        range: (Index, Index),
40    },
41    /// Get last entry from the Sequence.
42    GetLastEntry(Address),
43    /// List all current users permissions.
44    GetPermissions(Address),
45    /// Get current permissions for a specified user(s).
46    GetUserPermissions {
47        /// Sequence address.
48        address: Address,
49        /// User to get permissions for.
50        user: User,
51    },
52    /// Get current owner.
53    GetOwner(Address),
54}
55
56/// TODO: docs
57#[allow(clippy::large_enum_variant)]
58#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
59pub enum SequenceWrite {
60    /// Create a new Sequence on the network.
61    New(Sequence),
62    /// Edit the Sequence (insert/remove entry).
63    Edit(WriteOp<Entry>),
64    /// Delete a private Sequence.
65    ///
66    /// This operation MUST return an error if applied to public Sequence. Only the current
67    /// owner(s) can perform this action.
68    Delete(Address),
69    /// Set a new owner. Only the current owner(s) can perform this action.
70    SetOwner(WriteOp<PublicKey>),
71    /// Set new permissions for public Sequence.
72    SetPublicPermissions(WriteOp<PublicPermissions>),
73    /// Set new permissions for private Sequence.
74    SetPrivatePermissions(WriteOp<PrivatePermissions>),
75}
76
77impl SequenceRead {
78    /// Creates a Response containing an error, with the Response variant corresponding to the
79    /// Request variant.
80    pub fn error(&self, error: Error) -> QueryResponse {
81        use SequenceRead::*;
82        match *self {
83            Get(_) => QueryResponse::GetSequence(Err(error)),
84            GetRange { .. } => QueryResponse::GetSequenceRange(Err(error)),
85            GetLastEntry(_) => QueryResponse::GetSequenceLastEntry(Err(error)),
86            GetPermissions(_) => QueryResponse::GetSequencePermissions(Err(error)),
87            GetUserPermissions { .. } => QueryResponse::GetSequencePermissions(Err(error)),
88            GetOwner(_) => QueryResponse::GetSequenceOwner(Err(error)),
89        }
90    }
91
92    /// Returns the access categorisation of the request.
93    pub fn authorisation_kind(&self) -> AuthorisationKind {
94        use SequenceRead::*;
95        match *self {
96            Get(address)
97            | GetRange { address, .. }
98            | GetLastEntry(address)
99            | GetPermissions(address)
100            | GetUserPermissions { address, .. }
101            | GetOwner(address) => {
102                if address.is_pub() {
103                    AuthorisationKind::Data(DataAuthKind::PublicRead)
104                } else {
105                    AuthorisationKind::Data(DataAuthKind::PrivateRead)
106                }
107            }
108        }
109    }
110
111    /// Returns the address of the destination for request.
112    pub fn dst_address(&self) -> XorName {
113        use SequenceRead::*;
114        match self {
115            Get(ref address)
116            | GetRange { ref address, .. }
117            | GetLastEntry(ref address)
118            | GetPermissions(ref address)
119            | GetUserPermissions { ref address, .. }
120            | GetOwner(ref address) => *address.name(),
121        }
122    }
123}
124
125impl fmt::Debug for SequenceRead {
126    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
127        use SequenceRead::*;
128        write!(
129            formatter,
130            "Request::{}",
131            match *self {
132                Get(_) => "GetSequence",
133                GetRange { .. } => "GetSequenceRange",
134                GetLastEntry(_) => "GetSequenceLastEntry",
135                GetPermissions { .. } => "GetSequencePermissions",
136                GetUserPermissions { .. } => "GetUserPermissions",
137                GetOwner { .. } => "GetOwner",
138            }
139        )
140    }
141}
142
143impl SequenceWrite {
144    /// Creates a Response containing an error, with the Response variant corresponding to the
145    /// Request variant.
146    pub fn error(&self, error: Error) -> CmdError {
147        CmdError::Data(error)
148    }
149
150    /// Returns the access categorisation of the request.
151    pub fn authorisation_kind(&self) -> AuthorisationKind {
152        AuthorisationKind::Data(DataAuthKind::Write)
153    }
154
155    /// Returns the address of the destination for request.
156    pub fn dst_address(&self) -> XorName {
157        use SequenceWrite::*;
158        match self {
159            New(ref data) => *data.name(),
160            Delete(ref address) => *address.name(),
161            SetPublicPermissions(ref op) => *op.address.name(),
162            SetPrivatePermissions(ref op) => *op.address.name(),
163            SetOwner(ref op) => *op.address.name(),
164            Edit(ref op) => *op.address.name(),
165        }
166    }
167}
168
169impl fmt::Debug for SequenceWrite {
170    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
171        use SequenceWrite::*;
172        write!(
173            formatter,
174            "Request::{}",
175            match *self {
176                New(_) => "NewSequence",
177                Delete(_) => "DeleteSequence",
178                SetPublicPermissions(_) => "SetPublicPermissions",
179                SetPrivatePermissions(_) => "SetPrivatePermissions",
180                SetOwner(_) => "SetOwner",
181                Edit(_) => "EditSequence",
182            }
183        )
184    }
185}