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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Model validation and transport errors.
use super::{Tag, Version};
use std::{error::Error as StdError, fmt, io};
/// Errors caused by a value supplied to a constructor or setter.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ValueError {
/// The text contains NUL or exceeds its fixed-width byte capacity.
InvalidString {
/// Maximum text length in bytes.
max_bytes: usize,
},
/// The chunk is unavailable in this model version.
UnsupportedVersion {
/// Tag of the affected chunk.
tag: Tag,
/// Earliest supported model version.
minimum: Version,
/// Model version selected by the caller.
actual: Version,
},
/// The field is unavailable in this model version.
UnsupportedField {
/// Tag of the affected chunk.
tag: Tag,
/// Name of the affected field.
field: &'static str,
/// Model version selected by the caller.
actual: Version,
},
/// The optional field is absent from this record.
UnavailableField {
/// Tag of the affected chunk.
tag: Tag,
/// Name of the affected field.
field: &'static str,
},
/// An index is outside the referenced collection.
IndexOutOfBounds {
/// Tag of the affected chunk.
tag: Tag,
/// Index supplied by the caller.
index: usize,
/// Number of available entries.
len: usize,
},
/// A supplied array has an unexpected length.
LengthMismatch {
/// Tag of the affected chunk.
tag: Tag,
/// Required number of entries.
expected: usize,
/// Number of entries supplied by the caller.
actual: usize,
},
/// The record count exceeds the wire-format capacity.
CountTooLarge {
/// Tag of the affected chunk.
tag: Tag,
/// Number of entries supplied by the caller.
count: usize,
},
/// The global-sequence ID cannot be represented as a signed wire value.
InvalidGlobalSequenceId {
/// Global-sequence ID supplied by the caller.
id: u32,
},
}
impl fmt::Display for ValueError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidString { max_bytes } => {
write!(f, "string must fit in {max_bytes} bytes and contain no NUL")
}
Self::UnsupportedVersion {
tag,
minimum,
actual,
} => write!(
f,
"{:?} requires version {minimum}, got {actual}",
String::from_utf8_lossy(tag)
),
Self::UnsupportedField { tag, field, actual } => write!(
f,
"{field} is unsupported in {:?} version {actual}",
String::from_utf8_lossy(tag)
),
Self::UnavailableField { tag, field } => write!(
f,
"{field} is unavailable in {:?}",
String::from_utf8_lossy(tag)
),
Self::IndexOutOfBounds { tag, index, len } => write!(
f,
"index {index} is outside {:?} length {len}",
String::from_utf8_lossy(tag)
),
Self::LengthMismatch {
tag,
expected,
actual,
} => write!(
f,
"{:?} has {actual} values; expected {expected}",
String::from_utf8_lossy(tag)
),
Self::CountTooLarge { tag, count } => write!(
f,
"{:?} count {count} exceeds u32",
String::from_utf8_lossy(tag)
),
Self::InvalidGlobalSequenceId { id } => {
write!(f, "global sequence ID {id} is reserved for no sequence")
}
}
}
}
impl StdError for ValueError {}
/// A transport failure or an error produced by a codec.
#[derive(Debug)]
pub enum IoError<E> {
/// An underlying stream operation failed.
Io(io::Error),
/// The format codec rejected the data.
Codec(E),
}
impl<E> From<io::Error> for IoError<E> {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl<E: fmt::Display> fmt::Display for IoError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(error) => error.fmt(f),
Self::Codec(error) => error.fmt(f),
}
}
}
impl<E: StdError + 'static> StdError for IoError<E> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(match self {
Self::Io(error) => error,
Self::Codec(error) => error,
})
}
}