Skip to main content

ostrya_core/
error.rs

1//! The error type of the format primitives.
2//!
3//! This module implements `Display` and `std::error::Error` by hand, as
4//! `ostrya-gvariant` does, so this crate needs no derive dependency. The
5//! `ostrya` crate wraps this type in its own error, which uses `thiserror`.
6
7use std::fmt;
8
9/// An error of the format primitives.
10///
11/// Each fallible function of this crate returns it, except
12/// [`commit_timestamp`](crate::commit_timestamp). The I/O trait methods of
13/// [`DeflateSink`](crate::DeflateSink) and
14/// [`DeflateReader`](crate::DeflateReader) return [`std::io::Error`]. Most
15/// variants hold a fixed message that names the reason.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum Error {
18    /// An encode or decode error of the GVariant codec.
19    Gvariant(ostrya_gvariant::Error),
20    /// A checksum in hex, base64, or byte form that is not valid.
21    InvalidChecksum(&'static str),
22    /// A base64 string with an invalid character or a truncated group.
23    InvalidBase64(&'static str),
24    /// An object-type number that names no known type.
25    InvalidObjectType(u32),
26    /// A LEB128 varint that is truncated, overflows 64 bits, or is not minimal.
27    InvalidVarint(&'static str),
28    /// An xattr set with a name that is not valid.
29    ///
30    /// The name is empty, a duplicate, out of sort order, or has a missing
31    /// terminating NUL or an interior NUL.
32    InvalidXattrs(&'static str),
33    /// An `ostree.sizes` entry that is too short or has trailing bytes.
34    InvalidSizeEntry(&'static str),
35    /// A key file error.
36    ///
37    /// These are the causes:
38    ///
39    /// - The text does not parse.
40    /// - A value does not convert to the requested type.
41    /// - A set refuses a group name, a key, or a value.
42    KeyFile(String),
43    /// A commit object with a parent or root checksum of an invalid length.
44    InvalidCommit(&'static str),
45    /// A dirtree object that breaks an entry rule.
46    ///
47    /// These are the causes:
48    ///
49    /// - An entry name is not valid.
50    /// - An entry checksum is not 32 bytes.
51    /// - The names are not sorted.
52    /// - A name is in both the file and directory lists.
53    InvalidDirTree(&'static str),
54    /// A dirmeta object with a mode that is not a directory mode.
55    InvalidDirMeta(&'static str),
56    /// A file header or a framed content stream that is not valid.
57    ///
58    /// The header has a nonzero rdev, a mode that is not a regular file or a
59    /// symlink, or a regular file with a symlink target. The framed stream is
60    /// too short, has nonzero padding, or has a header length out of bounds.
61    /// A header that is longer than the framing length limit also gives this
62    /// error.
63    InvalidFileHeader(&'static str),
64    /// A ref pattern that is not valid.
65    ///
66    /// The text names the entry that failed. These are the causes:
67    ///
68    /// - The pattern is empty, or it has an empty part.
69    /// - A part holds `*` but is not `*` or `**`.
70    /// - A literal part is not a valid ref-name component.
71    /// - A pattern holds `!`, or a selector entry holds `!` after its first
72    ///   character.
73    InvalidRefPattern(String),
74}
75
76impl fmt::Display for Error {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        match self {
79            Error::Gvariant(e) => write!(f, "gvariant: {e}"),
80            Error::InvalidChecksum(reason) => write!(f, "invalid checksum: {reason}"),
81            Error::InvalidBase64(reason) => write!(f, "invalid base64: {reason}"),
82            Error::InvalidObjectType(v) => write!(f, "invalid object type tag {v}"),
83            Error::InvalidVarint(reason) => write!(f, "invalid varint: {reason}"),
84            Error::InvalidXattrs(reason) => write!(f, "invalid xattrs: {reason}"),
85            Error::InvalidSizeEntry(reason) => write!(f, "invalid ostree.sizes entry: {reason}"),
86            Error::KeyFile(reason) => write!(f, "keyfile: {reason}"),
87            Error::InvalidCommit(reason) => write!(f, "invalid commit: {reason}"),
88            Error::InvalidDirTree(reason) => write!(f, "invalid dirtree: {reason}"),
89            Error::InvalidDirMeta(reason) => write!(f, "invalid dirmeta: {reason}"),
90            Error::InvalidFileHeader(reason) => write!(f, "invalid file header: {reason}"),
91            Error::InvalidRefPattern(reason) => write!(f, "invalid ref pattern: {reason}"),
92        }
93    }
94}
95
96impl std::error::Error for Error {
97    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
98        match self {
99            Error::Gvariant(e) => Some(e),
100            _ => None,
101        }
102    }
103}
104
105impl From<ostrya_gvariant::Error> for Error {
106    fn from(e: ostrya_gvariant::Error) -> Self {
107        Error::Gvariant(e)
108    }
109}
110
111/// The result type of the format primitives.
112pub type Result<T> = std::result::Result<T, Error>;