aboutsummaryrefslogtreecommitdiff
path: root/src/rust/protover/errors.rs
blob: 04397ac4fea5e848b8bf5072ac080a107caaa326 (plain)
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
// Copyright (c) 2018-2019, The Tor Project, Inc.
// Copyright (c) 2018, isis agora lovecruft
// See LICENSE for licensing information

//! Various errors which may occur during protocol version parsing.

use std::fmt;
use std::fmt::Display;

/// All errors which may occur during protover parsing routines.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[allow(missing_docs)] // See Display impl for error descriptions
pub enum ProtoverError {
    Overlap,
    LowGreaterThanHigh,
    Unparseable,
    ExceedsMax,
    ExceedsExpansionLimit,
    UnknownProtocol,
    ExceedsNameLimit,
    InvalidProtocol,
}

/// Descriptive error messages for `ProtoverError` variants.
impl Display for ProtoverError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ProtoverError::Overlap => write!(
                f,
                "Two or more (low, high) protover ranges would overlap once expanded."
            ),
            ProtoverError::LowGreaterThanHigh => write!(
                f,
                "The low in a (low, high) protover range was greater than high."
            ),
            ProtoverError::Unparseable => write!(f, "The protover string was unparseable."),
            ProtoverError::ExceedsMax => write!(
                f,
                "The high in a (low, high) protover range exceeds 63."
            ),
            ProtoverError::ExceedsExpansionLimit => write!(
                f,
                "The protover string would exceed the maximum expansion limit."
            ),
            ProtoverError::UnknownProtocol => write!(
                f,
                "A protocol in the protover string we attempted to parse is unknown."
            ),
            ProtoverError::ExceedsNameLimit => {
                write!(f, "An unrecognised protocol name was too long.")
            }
            ProtoverError::InvalidProtocol => {
                write!(f, "A protocol name includes invalid characters.")
            }
        }
    }
}