automerge/columnar/encoding/
col_error.rs

1#[derive(Clone, Debug)]
2pub struct DecodeColumnError {
3    path: Path,
4    error: DecodeColErrorKind,
5}
6
7impl std::error::Error for DecodeColumnError {}
8
9impl std::fmt::Display for DecodeColumnError {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        match &self.error {
12            DecodeColErrorKind::UnexpectedNull => {
13                write!(f, "unexpected null in column {}", self.path)
14            }
15            DecodeColErrorKind::InvalidValue { reason } => {
16                write!(f, "invalid value in column {}: {}", self.path, reason)
17            }
18        }
19    }
20}
21
22#[derive(Clone, Debug)]
23struct Path(Vec<String>);
24
25impl std::fmt::Display for Path {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        for (index, elem) in self.0.iter().rev().enumerate() {
28            if index != 0 {
29                write!(f, ":")?;
30            }
31            write!(f, "{}", elem)?;
32        }
33        Ok(())
34    }
35}
36
37impl Path {
38    fn push<S: AsRef<str>>(&mut self, col: S) {
39        self.0.push(col.as_ref().to_string())
40    }
41}
42
43impl<S: AsRef<str>> From<S> for Path {
44    fn from(p: S) -> Self {
45        Self(vec![p.as_ref().to_string()])
46    }
47}
48
49#[derive(Clone, Debug)]
50enum DecodeColErrorKind {
51    UnexpectedNull,
52    InvalidValue { reason: String },
53}
54
55impl DecodeColumnError {
56    pub(crate) fn decode_raw<S: AsRef<str>>(col: S, raw_err: super::raw::Error) -> Self {
57        Self {
58            path: col.into(),
59            error: DecodeColErrorKind::InvalidValue {
60                reason: raw_err.to_string(),
61            },
62        }
63    }
64
65    pub(crate) fn unexpected_null<S: AsRef<str>>(col: S) -> DecodeColumnError {
66        Self {
67            path: col.into(),
68            error: DecodeColErrorKind::UnexpectedNull,
69        }
70    }
71
72    pub(crate) fn invalid_value<S: AsRef<str>, R: AsRef<str>>(
73        col: S,
74        reason: R,
75    ) -> DecodeColumnError {
76        Self {
77            path: col.into(),
78            error: DecodeColErrorKind::InvalidValue {
79                reason: reason.as_ref().to_string(),
80            },
81        }
82    }
83
84    pub(crate) fn in_column<S: AsRef<str>>(mut self, col: S) -> DecodeColumnError {
85        self.path.push(col.as_ref());
86        self
87    }
88}