snix_castore/
errors.rs

1use bstr::ByteSlice;
2
3use crate::{
4    SymlinkTargetError,
5    path::{PathComponent, PathComponentError},
6};
7
8/// Errors that occur during construction of [crate::Node]
9#[derive(Debug, thiserror::Error, PartialEq)]
10pub enum ValidateNodeError {
11    /// Invalid digest length encountered
12    #[error("invalid digest length: {0}")]
13    InvalidDigestLen(usize),
14    /// Invalid symlink target
15    #[error("Invalid symlink target: {0}")]
16    InvalidSymlinkTarget(SymlinkTargetError),
17    /// Invalid hash type encountered
18    #[error("invalid hash type: expected a 'blake3-' prefixed digest")]
19    InvalidHashType,
20}
21
22impl From<crate::digests::Error> for ValidateNodeError {
23    fn from(e: crate::digests::Error) -> Self {
24        match e {
25            crate::digests::Error::InvalidDigestLen(n) => ValidateNodeError::InvalidDigestLen(n),
26            crate::digests::Error::InvalidHashType => ValidateNodeError::InvalidHashType,
27        }
28    }
29}
30
31/// Errors that can occur when populating [crate::Directory] messages,
32/// or parsing [crate::proto::Directory]
33#[derive(Debug, thiserror::Error, PartialEq)]
34pub enum DirectoryError {
35    /// Multiple elements with the same name encountered
36    #[error("{:?} is a duplicate name", .0)]
37    DuplicateName(PathComponent),
38    /// Node failed validation
39    #[error("invalid node with name {}: {:?}", .0.as_bstr(), .1.to_string())]
40    InvalidNode(bytes::Bytes, ValidateNodeError),
41    #[error("Total size exceeds u64::MAX")]
42    SizeOverflow,
43    /// Invalid name encountered
44    #[error("Invalid name: {0}")]
45    InvalidName(PathComponentError),
46    /// This can occur if a protobuf node with a name is passed where we expect
47    /// it to be anonymous.
48    #[error("Name is set when it shouldn't")]
49    NameInAnonymousNode,
50    /// Elements are not in sorted order. Can only happen on protos
51    #[error("{:?} is not sorted", .0.as_bstr())]
52    WrongSorting(bytes::Bytes),
53    /// This can only happen if there's an unknown entry type (on protos)
54    #[error("No entry set")]
55    NoEntrySet,
56}