Skip to main content

ostrya_core/
error.rs

1//! The error type for the format-primitive layer.
2//!
3//! Hand-rolled with a manual [`Display`] and [`std::error::Error`] impl, the
4//! same style as the sibling `ostrya-gvariant` crate, so this crate pulls in
5//! no derive dependency. The higher-level `ostrya` crate wraps these in its
6//! own `thiserror`-based error.
7
8use std::fmt;
9
10/// Errors produced by the core format primitives.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum Error {
13    /// A GVariant (de)serialization error from the codec layer.
14    Gvariant(ostrya_gvariant::Error),
15    /// A checksum string or byte sequence was malformed.
16    InvalidChecksum(&'static str),
17    /// A base64 string was malformed (invalid character or truncated group).
18    InvalidBase64(&'static str),
19    /// An object-type numeric tag did not name a known type.
20    InvalidObjectType(u32),
21    /// A LEB128 varint was truncated or overflowed 64 bits.
22    InvalidVarint(&'static str),
23    /// An xattr set failed validation (empty, duplicate, or unsorted name).
24    InvalidXattrs(&'static str),
25    /// An `ostree.sizes` entry was malformed.
26    InvalidSizeEntry(&'static str),
27    /// A GKeyFile/INI document or a typed lookup was malformed.
28    KeyFile(String),
29    /// A commit object violated a value-level convention.
30    InvalidCommit(&'static str),
31    /// A dirtree object violated a value-level convention (bad file name,
32    /// checksum length, or sort order).
33    InvalidDirTree(&'static str),
34    /// A dirmeta object violated a value-level convention.
35    InvalidDirMeta(&'static str),
36    /// A file content-object header violated a value-level convention
37    /// (nonzero rdev, non-REG/non-LNK mode, or a malformed blob reference).
38    InvalidFileHeader(&'static str),
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Error::Gvariant(e) => write!(f, "gvariant: {e}"),
45            Error::InvalidChecksum(reason) => write!(f, "invalid checksum: {reason}"),
46            Error::InvalidBase64(reason) => write!(f, "invalid base64: {reason}"),
47            Error::InvalidObjectType(v) => write!(f, "invalid object type tag {v}"),
48            Error::InvalidVarint(reason) => write!(f, "invalid varint: {reason}"),
49            Error::InvalidXattrs(reason) => write!(f, "invalid xattrs: {reason}"),
50            Error::InvalidSizeEntry(reason) => write!(f, "invalid ostree.sizes entry: {reason}"),
51            Error::KeyFile(reason) => write!(f, "keyfile: {reason}"),
52            Error::InvalidCommit(reason) => write!(f, "invalid commit: {reason}"),
53            Error::InvalidDirTree(reason) => write!(f, "invalid dirtree: {reason}"),
54            Error::InvalidDirMeta(reason) => write!(f, "invalid dirmeta: {reason}"),
55            Error::InvalidFileHeader(reason) => write!(f, "invalid file header: {reason}"),
56        }
57    }
58}
59
60impl std::error::Error for Error {
61    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
62        match self {
63            Error::Gvariant(e) => Some(e),
64            _ => None,
65        }
66    }
67}
68
69impl From<ostrya_gvariant::Error> for Error {
70    fn from(e: ostrya_gvariant::Error) -> Self {
71        Error::Gvariant(e)
72    }
73}
74
75/// Result alias for the core format primitives.
76pub type Result<T> = std::result::Result<T, Error>;