async_coap/
error.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use std::fmt::{Debug, Display, Formatter};
17
18/// Type for errors encountered while sending or receiving CoAP requests and responses.
19#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
20pub enum Error {
21    /// One or more of the supplied arguments are not valid for the given operation.
22    InvalidArgument,
23
24    /// There is not enough space in the given buffer to complete the operation.
25    OutOfSpace,
26
27    /// An error was encountered while attempting to parse the data.
28    ParseFailure,
29
30    /// Operation timed out waiting for a response.
31    ResponseTimeout,
32
33    /// The response was well-formed, but not appropriate for the given request.
34    BadResponse,
35
36    /// The [message code][async-coap::message::MsgCode] was not recognized by this
37    /// version of rust-async-coap.
38    UnknownMessageCode,
39
40    /// A critical option present in the message was not supported.
41    UnhandledCriticalOption,
42
43    /// An I/O error occurred while performing this operation.
44    IOError,
45
46    /// This operation has been cancelled.
47    Cancelled,
48
49    /// Unable to look up the given host because it was not found.
50    HostNotFound,
51
52    /// Unable to look up the given host for an unspecified reason.
53    HostLookupFailure,
54
55    /// The response indicated that the given resource was not found.
56    ResourceNotFound,
57
58    /// The response indicated that the request was unauthorized.
59    Unauthorized,
60
61    /// The response indicated that the request was forbidden.
62    Forbidden,
63
64    /// The response indicated an unspecified client error.
65    ClientRequestError,
66
67    /// The response indicated an unspecified server error.
68    ServerError,
69
70    /// The transaction was reset.
71    Reset,
72
73    /// More than one instance of an option marked as non-repeatable was encountered.
74    OptionNotRepeatable,
75
76    /// The given URI scheme is not supported by the associated local endpoint.
77    UnsupportedUriScheme,
78
79    /// An unspecified error has occurred.
80    Unspecified,
81}
82
83#[cfg(feature = "std")]
84impl std::convert::From<std::io::Error> for Error {
85    fn from(_: std::io::Error) -> Self {
86        Error::IOError
87    }
88}
89
90impl std::convert::From<Error> for core::fmt::Error {
91    fn from(_: Error) -> Self {
92        core::fmt::Error
93    }
94}
95
96impl From<std::fmt::Error> for crate::Error {
97    fn from(_err: std::fmt::Error) -> Self {
98        Error::OutOfSpace
99    }
100}
101
102impl Display for Error {
103    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
104        <Self as Debug>::fmt(self, f)
105    }
106}
107
108impl Default for Error {
109    fn default() -> Self {
110        Error::Unspecified
111    }
112}
113
114impl Extend<Result<(), Error>> for Error {
115    fn extend<T: IntoIterator<Item = Result<(), Error>>>(&mut self, iter: T) {
116        if let Some(Err(err)) = iter.into_iter().next() {
117            *self = err;
118        }
119    }
120}