revision/
error.rs

1use std::{io, str::Utf8Error};
2
3/// An error which occurs when revisioned serialization / deserialization fails.
4#[derive(Debug)]
5pub enum Error {
6	/// An IO error occured.
7	Io(io::Error),
8	/// Tried to deserialize a boolean value with an invalid byte value.
9	InvalidBoolValue(u8),
10	/// Deserialization encountered integer encoding which is not suported.
11	InvalidIntegerEncoding,
12	/// Deserialization encountered an integer with a value which did not fit the target type..
13	IntegerOverflow,
14	/// Path contains invalid utf-8 characters
15	InvalidPath,
16	/// Invalid character encoding
17	InvalidCharEncoding,
18	/// Error parsing a string
19	Utf8Error(Utf8Error),
20	/// Failed to serialize character.
21	Serialize(String),
22	/// Generic deserialization error.
23	Deserialize(String),
24	/// Semantic translation/validation error.
25	Conversion(String),
26}
27
28impl std::error::Error for Error {
29	#[inline]
30	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31		match self {
32			Error::Io(ref x) => Some(x),
33			Error::Utf8Error(ref x) => Some(x),
34			_ => None,
35		}
36	}
37}
38
39impl std::fmt::Display for Error {
40	#[inline]
41	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
42		match self {
43			Self::Io(e) => write!(f, "An IO error occured: {}", e),
44			Self::InvalidBoolValue(_) => {
45				write!(f, "Tried to deserialize a boolean value with an invalid byte value.")
46			}
47			Self::InvalidIntegerEncoding => {
48				write!(f, "Encountered invalid integer encoding.")
49			}
50			Self::IntegerOverflow => {
51				write!(f, "Encountered integer which doesn't fit the target integer type during deserialization.")
52			}
53			Self::InvalidPath => {
54				write!(f, "Path contained invalid UTF-8 characters.")
55			}
56			Self::InvalidCharEncoding => {
57				write!(f, "Invalid character encoding.")
58			}
59			Self::Utf8Error(x) => {
60				write!(f, "Invalid UTF-8 characters in string: {x}")
61			}
62			Self::Serialize(e) => write!(f, "A serialization error occured: {}", e),
63			Self::Deserialize(e) => write!(f, "A deserialization error occured: {}", e),
64			Self::Conversion(e) => write!(f, "A user generated conversion error occured: {}", e),
65		}
66	}
67}