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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::fmt::{Debug, Display, Formatter};

/// Type for errors encountered while sending or receiving CoAP requests and responses.
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
pub enum Error {
    /// One or more of the supplied arguments are not valid for the given operation.
    InvalidArgument,

    /// There is not enough space in the given buffer to complete the operation.
    OutOfSpace,

    /// An error was encountered while attempting to parse the data.
    ParseFailure,

    /// Operation timed out waiting for a response.
    ResponseTimeout,

    /// The response was well-formed, but not appropriate for the given request.
    BadResponse,

    /// The [message code][async-coap::message::MsgCode] was not recognized by this
    /// version of rust-async-coap.
    UnknownMessageCode,

    /// A critical option present in the message was not supported.
    UnhandledCriticalOption,

    /// An I/O error occurred while performing this operation.
    IOError,

    /// This operation has been cancelled.
    Cancelled,

    /// Unable to look up the given host because it was not found.
    HostNotFound,

    /// Unable to look up the given host for an unspecified reason.
    HostLookupFailure,

    /// The response indicated that the given resource was not found.
    ResourceNotFound,

    /// The response indicated that the request was unauthorized.
    Unauthorized,

    /// The response indicated that the request was forbidden.
    Forbidden,

    /// The response indicated an unspecified client error.
    ClientRequestError,

    /// The response indicated an unspecified server error.
    ServerError,

    /// The transaction was reset.
    Reset,

    /// More than one instance of an option marked as non-repeatable was encountered.
    OptionNotRepeatable,

    /// The given URI scheme is not supported by the associated local endpoint.
    UnsupportedUriScheme,

    /// An unspecified error has occurred.
    Unspecified,
}

#[cfg(feature = "std")]
impl std::convert::From<std::io::Error> for Error {
    fn from(_: std::io::Error) -> Self {
        Error::IOError
    }
}

impl std::convert::From<Error> for core::fmt::Error {
    fn from(_: Error) -> Self {
        core::fmt::Error
    }
}

impl From<std::fmt::Error> for crate::Error {
    fn from(_err: std::fmt::Error) -> Self {
        Error::OutOfSpace
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        <Self as Debug>::fmt(self, f)
    }
}

impl Default for Error {
    fn default() -> Self {
        Error::Unspecified
    }
}

impl Extend<Result<(), Error>> for Error {
    fn extend<T: IntoIterator<Item = Result<(), Error>>>(&mut self, iter: T) {
        if let Some(Err(err)) = iter.into_iter().next() {
            *self = err;
        }
    }
}