safe_nd/messaging/
blob.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::{Blob, BlobAddress, Error, XorName};
12use serde::{Deserialize, Serialize};
13use std::fmt;
14
15/// TODO: docs
16#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
17pub enum BlobRead {
18    /// TODO: docs
19    Get(BlobAddress),
20}
21
22/// TODO: docs
23#[allow(clippy::large_enum_variant)]
24#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
25pub enum BlobWrite {
26    /// TODO: docs
27    New(Blob),
28    /// TODO: docs
29    DeletePrivate(BlobAddress),
30}
31
32impl BlobRead {
33    // /// Get the `Type` of this `Request`.
34    // pub fn get_type(&self) -> Type {
35    //     use BlobRead::*;
36    //     match self {
37    //         Get(BlobAddress::Public(_)) => Type::PublicRead,
38    //         Get(BlobAddress::Private(_)) => Type::PrivateRead,
39    //     }
40    // }
41
42    /// Creates a Response containing an error, with the Response variant corresponding to the
43    /// Request variant.
44    pub fn error(&self, error: Error) -> QueryResponse {
45        QueryResponse::GetBlob(Err(error))
46    }
47
48    /// Returns the type of authorisation needed for the request.
49    pub fn authorisation_kind(&self) -> AuthorisationKind {
50        use BlobRead::*;
51        match self {
52            Get(BlobAddress::Public(_)) => AuthorisationKind::Data(DataAuthKind::PublicRead),
53            Get(BlobAddress::Private(_)) => AuthorisationKind::Data(DataAuthKind::PrivateRead),
54        }
55    }
56
57    /// Returns the address of the destination for `request`.
58    pub fn dst_address(&self) -> XorName {
59        use BlobRead::*;
60        match self {
61            Get(ref address) => *address.name(),
62        }
63    }
64}
65
66impl BlobWrite {
67    /// Creates a Response containing an error, with the Response variant corresponding to the
68    /// Request variant.
69    pub fn error(&self, error: Error) -> CmdError {
70        CmdError::Data(error)
71    }
72
73    /// Returns the type of authorisation needed for the request.
74    pub fn authorisation_kind(&self) -> AuthorisationKind {
75        AuthorisationKind::Data(DataAuthKind::Write)
76    }
77
78    /// Returns the address of the destination for `request`.
79    pub fn dst_address(&self) -> XorName {
80        use BlobWrite::*;
81        match self {
82            New(ref data) => *data.name(),
83            DeletePrivate(ref address) => *address.name(),
84        }
85    }
86}
87
88impl fmt::Debug for BlobRead {
89    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90        use BlobRead::*;
91        match self {
92            Get(req) => write!(formatter, "{:?}", req),
93        }
94    }
95}
96
97impl fmt::Debug for BlobWrite {
98    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
99        use BlobWrite::*;
100        match self {
101            New(req) => write!(formatter, "{:?}", req),
102            DeletePrivate(req) => write!(formatter, "{:?}", req),
103        }
104    }
105}