1 // Copyright (c) 2018-2019, The Tor Project, Inc.
2 // Copyright (c) 2018, isis agora lovecruft
3 // See LICENSE for licensing information
4 
5 //! Various errors which may occur during protocol version parsing.
6 
7 use std::fmt;
8 use std::fmt::Display;
9 
10 /// All errors which may occur during protover parsing routines.
11 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
12 #[allow(missing_docs)] // See Display impl for error descriptions
13 pub enum ProtoverError {
14     Overlap,
15     LowGreaterThanHigh,
16     Unparseable,
17     ExceedsMax,
18     ExceedsExpansionLimit,
19     UnknownProtocol,
20     ExceedsNameLimit,
21     InvalidProtocol,
22 }
23 
24 /// Descriptive error messages for `ProtoverError` variants.
25 impl Display for ProtoverError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result26     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27         match *self {
28             ProtoverError::Overlap => write!(
29                 f,
30                 "Two or more (low, high) protover ranges would overlap once expanded."
31             ),
32             ProtoverError::LowGreaterThanHigh => write!(
33                 f,
34                 "The low in a (low, high) protover range was greater than high."
35             ),
36             ProtoverError::Unparseable => write!(f, "The protover string was unparseable."),
37             ProtoverError::ExceedsMax => write!(
38                 f,
39                 "The high in a (low, high) protover range exceeds 63."
40             ),
41             ProtoverError::ExceedsExpansionLimit => write!(
42                 f,
43                 "The protover string would exceed the maximum expansion limit."
44             ),
45             ProtoverError::UnknownProtocol => write!(
46                 f,
47                 "A protocol in the protover string we attempted to parse is unknown."
48             ),
49             ProtoverError::ExceedsNameLimit => {
50                 write!(f, "An unrecognised protocol name was too long.")
51             }
52             ProtoverError::InvalidProtocol => {
53                 write!(f, "A protocol name includes invalid characters.")
54             }
55         }
56     }
57 }
58