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