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