Skip to main content

voxj_codec/
error.rs

1use base64::DecodeError;
2use serde_json::Error as JsonError;
3use std::{
4    error::Error as StdError,
5    fmt::{Display, Formatter, Result as FmtResult},
6};
7
8/// An error decoding or encoding a Voxel Json (`.voxj` / `.voxjz`) document.
9#[derive(Debug)]
10pub enum Error {
11    /// The document JSON could not be deserialized or serialized.
12    Json(JsonError),
13
14    /// A base64-encoded position or sample block could not be decoded.
15    Base64(DecodeError),
16
17    /// The document or `.voxjz` archive was readable but structurally malformed.
18    Invalid(String),
19}
20
21impl Display for Error {
22    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
23        match self {
24            Error::Json(e) => e.fmt(f),
25            Error::Base64(e) => e.fmt(f),
26            Error::Invalid(message) => write!(f, "{message}"),
27        }
28    }
29}
30
31impl StdError for Error {
32    fn source(&self) -> Option<&(dyn StdError + 'static)> {
33        match self {
34            Error::Json(e) => Some(e),
35            Error::Base64(e) => Some(e),
36            Error::Invalid(_) => None,
37        }
38    }
39}
40
41impl From<JsonError> for Error {
42    fn from(e: JsonError) -> Self {
43        Error::Json(e)
44    }
45}
46
47impl From<DecodeError> for Error {
48    fn from(e: DecodeError) -> Self {
49        Error::Base64(e)
50    }
51}