1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! # Misc
//!
//! `misc` contains different implementations for octopipes types

//
//   RustyPipes
//   Developed by Christian Visintin
//
// MIT License
// Copyright (c) 2019-2020 Christian Visintin
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

use super::OctopipesCapError;
use super::OctopipesCapMessage;
use super::OctopipesError;
use super::OctopipesProtocolVersion;
use super::OctopipesOptions;
use super::OctopipesServerError;

use std::fmt;

//Types utils
impl OctopipesProtocolVersion {
    pub(crate) fn from_u8(value: u8) -> Option<OctopipesProtocolVersion> {
        match value {
            1 => Some(OctopipesProtocolVersion::Version1),
            _ => None,
        }
    }
}

impl OctopipesCapMessage {
    pub(crate) fn from_u8(value: u8) -> Option<OctopipesCapMessage> {
        match value {
            0x01 => Some(OctopipesCapMessage::Subscription),
            0x02 => Some(OctopipesCapMessage::Unsubscription),
            0xff => Some(OctopipesCapMessage::Assignment),
            _ => None,
        }
    }
    pub(crate) fn to_string(&self) -> &str {
        match self {
            OctopipesCapMessage::Assignment => "ASSIGNMENT",
            OctopipesCapMessage::Subscription => "SUBSCRIPTION",
            OctopipesCapMessage::Unsubscription => "UNSUBSCRIPTION"
        }
    }
}

impl OctopipesCapError {
    pub(crate) fn from_u8(value: u8) -> Option<OctopipesCapError> {
        match value {
            0x00 => Some(OctopipesCapError::NoError),
            0x01 => Some(OctopipesCapError::NameAlreadyTaken),
            0x02 => Some(OctopipesCapError::FileSystemError),
            _ => None
        }
    }
    pub(crate) fn to_string(&self) -> &str {
        match self {
            OctopipesCapError::FileSystemError => "FileSystemError",
            OctopipesCapError::NameAlreadyTaken => "NameAlreadyTaken",
            OctopipesCapError::NoError => "NoError"
        }
    }
}

impl OctopipesOptions {
    pub(crate) fn from_u8(value: u8) -> OctopipesOptions {
        let mut option: OctopipesOptions = OctopipesOptions::empty();
        if value & OctopipesOptions::RCK.bits() != 0 {
            option.set(OctopipesOptions::RCK, true);
        }
        if value & OctopipesOptions::ACK.bits() != 0 {
            option.set(OctopipesOptions::ACK, true);
        }
        if value & OctopipesOptions::ICK.bits() != 0 {
            option.set(OctopipesOptions::ICK, true);
        }
        option
    }
}

impl OctopipesError {
    pub fn to_string(&self) -> &str {
        match self {
            OctopipesError::Uninitialized => "OctopipesClient is not initialized yet",
            OctopipesError::BadChecksum => "Packet has bad checksum",
            OctopipesError::BadPacket => "It was not possible to decode packet, since it contains bad data",
            OctopipesError::CapTimeout => "CAP timeout",
            OctopipesError::NoDataAvailable => "There is not data available on pipe",
            OctopipesError::NotSubscribed => "The client must be subscribed to the server before receiving and sending messages",
            OctopipesError::NotUnsubscribed => "The client must be unsubscribed to perform this action",
            OctopipesError::OpenFailed => "Could not open the requested pipe",
            OctopipesError::ReadFailed => "Could not read from pipe",
            OctopipesError::ThreadAlreadyRunning => "Client loop Thread is already running",
            OctopipesError::ThreadError => "Thread error",
            OctopipesError::UnsupportedVersion => "Unsupported protocol version",
            OctopipesError::WriteFailed => "Could not write to pipe",
            _ => "Unknown error"
        }
    }
    pub fn to_server_error(&self) -> OctopipesServerError {
        match self {
            OctopipesError::BadChecksum => OctopipesServerError::BadChecksum,
            OctopipesError::BadPacket => OctopipesServerError::BadPacket,
            OctopipesError::CapTimeout => OctopipesServerError::CapTimeout,
            OctopipesError::OpenFailed => OctopipesServerError::OpenFailed,
            OctopipesError::ReadFailed => OctopipesServerError::ReadFailed,
            OctopipesError::WriteFailed => OctopipesServerError::WriteFailed,
            OctopipesError::UnsupportedVersion => OctopipesServerError::UnsupportedVersion,
            _ => OctopipesServerError::Unknown
        }
    }
}

impl OctopipesServerError {
    pub fn to_string(&self) -> &str {
        match self {
            OctopipesServerError::Uninitialized => "OctopipesServer is not initialized yet",
            OctopipesServerError::BadChecksum => "Packet has bad checksum",
            OctopipesServerError::BadClientDir => "Could not create client directory",
            OctopipesServerError::BadPacket => "It was not possible to decode packet, since it contains bad data",
            OctopipesServerError::CapTimeout => "CAP timeout",
            OctopipesServerError::NoRecipient => "The provided message has no recipient",
            OctopipesServerError::OpenFailed => "Could not open the requested pipe",
            OctopipesServerError::ReadFailed => "Could not read from pipe",
            OctopipesServerError::ThreadAlreadyRunning => "Client loop Thread is already running",
            OctopipesServerError::ThreadError => "Thread error",
            OctopipesServerError::WorkerAlreadyRunning => "The requested worker is already running",
            OctopipesServerError::WorkerNotFound => "The requested Worker couldn't be found",
            OctopipesServerError::WorkerExists => "The requested Worker already exists",
            OctopipesServerError::WorkerNotRunning => "This worker is not running",
            OctopipesServerError::WriteFailed => "Could not write to pipe",
            OctopipesServerError::UnsupportedVersion => "Unsupported protocol version",
            _ => "Unknown error"
        }
    }
}

impl fmt::Debug for OctopipesError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Display for OctopipesError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Debug for OctopipesServerError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Display for OctopipesServerError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Display for OctopipesOptions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.bits())
    }
}

impl fmt::Display for OctopipesCapMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Debug for OctopipesCapMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Display for OctopipesCapError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Debug for OctopipesCapError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}