Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit 037519e

Browse files
authored
Merge pull request #188 from dtolnay/shared
Add an error variant with shared ownership
2 parents b16a401 + 2700f69 commit 037519e

1 file changed

Lines changed: 62 additions & 44 deletions

File tree

‎src/error.rs‎

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::io;
66
use std::result;
77
use std::str;
88
use std::string;
9+
use std::sync::Arc;
910
use yaml_rust::emitter;
1011
use yaml_rust::scanner::{self, Marker, ScanError};
1112

@@ -28,6 +29,9 @@ pub enum ErrorImpl {
2829
EndOfStream,
2930
MoreThanOneDocument,
3031
RecursionLimitExceeded,
32+
33+
#[allow(dead_code)] // to be used for multi-doc deserialization
34+
Shared(Arc<ErrorImpl>),
3135
}
3236

3337
#[derive(Debug)]
@@ -143,37 +147,73 @@ pub(crate) fn fix_marker(mut error: Error, marker: Marker, path: Path) -> Error
143147

144148
impl error::Error for Error {
145149
// TODO: deprecated, remove in next major version.
150+
fn description(&self) -> &str {
151+
self.0.description()
152+
}
153+
154+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
155+
self.0.source()
156+
}
157+
}
158+
159+
impl Display for Error {
160+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161+
self.0.display(f)
162+
}
163+
}
164+
165+
// Remove two layers of verbosity from the debug representation. Humans often
166+
// end up seeing this representation because it is what unwrap() shows.
167+
impl Debug for Error {
168+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169+
self.0.debug(f)
170+
}
171+
}
172+
173+
impl ser::Error for Error {
174+
fn custom<T: Display>(msg: T) -> Self {
175+
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
176+
}
177+
}
178+
179+
impl de::Error for Error {
180+
fn custom<T: Display>(msg: T) -> Self {
181+
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
182+
}
183+
}
184+
185+
impl ErrorImpl {
146186
#[allow(deprecated)]
147187
fn description(&self) -> &str {
148-
match self.0.as_ref() {
188+
match self {
149189
ErrorImpl::Message(msg, _) => msg,
150190
ErrorImpl::Emit(_) => "emit error",
151191
ErrorImpl::Scan(_) => "scan error",
152-
ErrorImpl::Io(err) => err.description(),
153-
ErrorImpl::Utf8(err) => err.description(),
154-
ErrorImpl::FromUtf8(err) => err.description(),
192+
ErrorImpl::Io(err) => error::Error::description(err),
193+
ErrorImpl::Utf8(err) => error::Error::description(err),
194+
ErrorImpl::FromUtf8(err) => error::Error::description(err),
155195
ErrorImpl::EndOfStream => "EOF while parsing a value",
156196
ErrorImpl::MoreThanOneDocument => {
157197
"deserializing from YAML containing more than one document is not supported"
158198
}
159199
ErrorImpl::RecursionLimitExceeded => "recursion limit exceeded",
200+
ErrorImpl::Shared(err) => err.description(),
160201
}
161202
}
162203

163204
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
164-
match self.0.as_ref() {
205+
match self {
165206
ErrorImpl::Scan(err) => Some(err),
166207
ErrorImpl::Io(err) => Some(err),
167208
ErrorImpl::Utf8(err) => Some(err),
168209
ErrorImpl::FromUtf8(err) => Some(err),
210+
ErrorImpl::Shared(err) => err.source(),
169211
_ => None,
170212
}
171213
}
172-
}
173214

174-
impl Display for Error {
175-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
176-
match self.0.as_ref() {
215+
fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
216+
match self {
177217
ErrorImpl::Message(msg, None) => Display::fmt(msg, f),
178218
ErrorImpl::Message(msg, Some(Pos { marker, path })) => {
179219
if path == "." {
@@ -193,44 +233,22 @@ impl Display for Error {
193233
"deserializing from YAML containing more than one document is not supported",
194234
),
195235
ErrorImpl::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
236+
ErrorImpl::Shared(err) => err.display(f),
196237
}
197238
}
198-
}
199239

200-
// Remove two layers of verbosity from the debug representation. Humans often
201-
// end up seeing this representation because it is what unwrap() shows.
202-
impl Debug for Error {
203-
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
204-
match self.0.as_ref() {
205-
ErrorImpl::Message(msg, pos) => formatter
206-
.debug_tuple("Message")
207-
.field(msg)
208-
.field(pos)
209-
.finish(),
210-
ErrorImpl::Emit(emit) => formatter.debug_tuple("Emit").field(emit).finish(),
211-
ErrorImpl::Scan(scan) => formatter.debug_tuple("Scan").field(scan).finish(),
212-
ErrorImpl::Io(io) => formatter.debug_tuple("Io").field(io).finish(),
213-
ErrorImpl::Utf8(utf8) => formatter.debug_tuple("Utf8").field(utf8).finish(),
214-
ErrorImpl::FromUtf8(from_utf8) => {
215-
formatter.debug_tuple("FromUtf8").field(from_utf8).finish()
216-
}
217-
ErrorImpl::EndOfStream => formatter.debug_tuple("EndOfStream").finish(),
218-
ErrorImpl::MoreThanOneDocument => formatter.debug_tuple("MoreThanOneDocument").finish(),
219-
ErrorImpl::RecursionLimitExceeded => {
220-
formatter.debug_tuple("RecursionLimitExceeded").finish()
221-
}
240+
fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
241+
match self {
242+
ErrorImpl::Message(msg, pos) => f.debug_tuple("Message").field(msg).field(pos).finish(),
243+
ErrorImpl::Emit(emit) => f.debug_tuple("Emit").field(emit).finish(),
244+
ErrorImpl::Scan(scan) => f.debug_tuple("Scan").field(scan).finish(),
245+
ErrorImpl::Io(io) => f.debug_tuple("Io").field(io).finish(),
246+
ErrorImpl::Utf8(utf8) => f.debug_tuple("Utf8").field(utf8).finish(),
247+
ErrorImpl::FromUtf8(from_utf8) => f.debug_tuple("FromUtf8").field(from_utf8).finish(),
248+
ErrorImpl::EndOfStream => f.debug_tuple("EndOfStream").finish(),
249+
ErrorImpl::MoreThanOneDocument => f.debug_tuple("MoreThanOneDocument").finish(),
250+
ErrorImpl::RecursionLimitExceeded => f.debug_tuple("RecursionLimitExceeded").finish(),
251+
ErrorImpl::Shared(err) => err.debug(f),
222252
}
223253
}
224254
}
225-
226-
impl ser::Error for Error {
227-
fn custom<T: Display>(msg: T) -> Self {
228-
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
229-
}
230-
}
231-
232-
impl de::Error for Error {
233-
fn custom<T: Display>(msg: T) -> Self {
234-
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
235-
}
236-
}

0 commit comments

Comments
 (0)