Skip to main content

git_stub/
errors.rs

1// Copyright 2026 Oxide Computer Company
2
3//! Error types for git stub operations.
4
5use camino::Utf8PathBuf;
6use thiserror::Error;
7
8/// An error that occurs while parsing a
9/// [`GitCommitHash`](crate::GitCommitHash).
10#[derive(Clone, Debug, Error, PartialEq)]
11#[non_exhaustive]
12pub enum CommitHashParseError {
13    /// The commit hash has an invalid length.
14    #[error(
15        "invalid length: expected 40 (SHA-1) or 64 (SHA-256) hex characters, \
16         got {0}"
17    )]
18    InvalidLength(usize),
19
20    /// The commit hash is not valid hexadecimal.
21    #[error("invalid hexadecimal")]
22    InvalidHex(hex::FromHexError),
23}
24
25/// An error that occurs while parsing a [`GitStub`](crate::GitStub).
26#[derive(Debug, Error)]
27#[non_exhaustive]
28pub enum GitStubParseError {
29    /// The input was empty or contained only whitespace.
30    #[error("git stub is empty")]
31    EmptyInput,
32
33    /// The git stub string did not contain the expected 'commit:path'
34    /// format.
35    #[error(
36        "invalid git stub format: expected 'commit:path', got {0:?} \
37         (missing ':' separator)"
38    )]
39    InvalidFormat(String),
40
41    /// The commit hash in the git stub was invalid.
42    #[error("invalid commit hash in git stub")]
43    InvalidCommitHash(#[from] CommitHashParseError),
44
45    /// The path component was empty.
46    #[error("git stub has empty path (nothing after ':')")]
47    EmptyPath,
48
49    /// The path contains a non-normal component (e.g., `..`, `.`, `/`, or a
50    /// Windows prefix). Only plain file and directory names are allowed.
51    #[error(
52        "git stub path {path:?} contains non-normal component {component:?} \
53         (only plain file/directory names are allowed)"
54    )]
55    InvalidPathComponent {
56        /// The full path that failed validation.
57        path: Utf8PathBuf,
58        /// The non-normal component that was found (e.g., `..`, `.`, `/`).
59        component: String,
60    },
61
62    /// The path contains a newline character.
63    #[error("git stub path contains a newline character")]
64    NewlineInPath,
65}