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