async_coap_uri/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::ops::Range;
17
18/// Error type for resolving a target URI against a base URI.
19///
20/// Emitted by [`AnyUriRef::write_resolved`], [`AnyUriRef::resolved`],
21/// and a few others.
22///
23/// [`AnyUriRef::write_resolved`]: async-coap-uri::AnyUriRef::write_resolved
24/// [`AnyUriRef::resolved`]: async-coap-uri::AnyUriRef::resolved
25#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
26pub enum ResolveError {
27 /// The URI-reference being given as a base cannot be used as a base for the given
28 /// target URI-reference.
29 CannotBeABase,
30
31 /// Unable to write to the given [`core::fmt::Write`] instance.
32 WriteFailure,
33}
34
35/// Transparent conversions from [`core::fmt::Error`] to [`ResolveError`].
36impl From<core::fmt::Error> for ResolveError {
37 fn from(_: core::fmt::Error) -> Self {
38 ResolveError::WriteFailure
39 }
40}
41
42/// URI parse error type.
43///
44/// This type indicates the details of an error that occurs while parsing a URI.
45#[derive(Debug, Clone, Eq, PartialEq, Hash)]
46pub struct ParseError {
47 desc: &'static str,
48 span: Option<Range<usize>>,
49}
50
51impl ParseError {
52 /// Constructor for URI parse errors.
53 pub fn new(desc: &'static str, span: Option<Range<usize>>) -> ParseError {
54 ParseError { desc, span }
55 }
56
57 /// The location in the input string of the error. Optional.
58 pub fn span(&self) -> Option<Range<usize>> {
59 self.span.clone()
60 }
61
62 /// A debugging description of the error.
63 pub fn desc(&self) -> &'static str {
64 self.desc
65 }
66}