Skip to main content

radicle/storage/refs/sigrefs/git/object/
error.rs

1use std::path::PathBuf;
2
3use radicle_oid::Oid;
4use thiserror::Error;
5
6type StdError = dyn std::error::Error + Send + Sync + 'static;
7
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum ReadCommit {
11    #[error(transparent)]
12    IncorrectObject(NotCommit),
13    #[error(transparent)]
14    Other(Box<StdError>),
15}
16
17impl ReadCommit {
18    pub fn incorrect_object_error<K>(oid: Oid, kind: K) -> Self
19    where
20        K: ToString,
21    {
22        Self::IncorrectObject(NotCommit {
23            oid,
24            kind: kind.to_string(),
25        })
26    }
27
28    pub fn other<E>(err: E) -> Self
29    where
30        E: std::error::Error + Send + Sync + 'static,
31    {
32        Self::Other(Box::new(err))
33    }
34}
35
36#[derive(Debug, Error)]
37#[non_exhaustive]
38#[error("the object {oid} is a {kind}, not a commit")]
39pub struct NotCommit {
40    oid: Oid,
41    kind: String,
42}
43
44#[derive(Debug, Error)]
45#[non_exhaustive]
46#[error(transparent)]
47pub enum ReadBlob {
48    #[error(transparent)]
49    CommitNotFound(CommitNotFound),
50    #[error(transparent)]
51    IncorrectObject(NotBlob),
52    #[error(transparent)]
53    Other(Box<StdError>),
54}
55
56#[derive(Debug, Error)]
57#[non_exhaustive]
58#[error("could not find commit {oid}")]
59pub struct CommitNotFound {
60    oid: Oid,
61}
62
63#[derive(Debug, Error)]
64#[non_exhaustive]
65#[error("the object at {path:?} in commit {commit} is a {kind}, not a blob")]
66pub struct NotBlob {
67    commit: Oid,
68    path: PathBuf,
69    kind: String,
70}
71
72impl ReadBlob {
73    pub fn commit_not_found_error(oid: Oid) -> Self {
74        Self::CommitNotFound(CommitNotFound { oid })
75    }
76
77    pub fn incorrect_object_error<K>(commit: Oid, path: PathBuf, kind: K) -> Self
78    where
79        K: ToString,
80    {
81        Self::IncorrectObject(NotBlob {
82            commit,
83            path,
84            kind: kind.to_string(),
85        })
86    }
87
88    pub fn other<E>(err: E) -> Self
89    where
90        E: std::error::Error + Send + Sync + 'static,
91    {
92        Self::Other(Box::new(err))
93    }
94}
95
96#[derive(Debug, Error)]
97#[non_exhaustive]
98pub enum WriteTree {
99    #[error("failed to write reference blob for signed references")]
100    Refs(Box<StdError>),
101    #[error("failed to write signature blob for signed references")]
102    Signature(Box<StdError>),
103    #[error(transparent)]
104    Write(Box<StdError>),
105}
106
107impl WriteTree {
108    pub fn refs_error<E>(err: E) -> Self
109    where
110        E: std::error::Error + Send + Sync + 'static,
111    {
112        Self::Refs(Box::new(err))
113    }
114
115    pub fn signature_error<E>(err: E) -> Self
116    where
117        E: std::error::Error + Send + Sync + 'static,
118    {
119        Self::Signature(Box::new(err))
120    }
121
122    pub fn write_error<E>(err: E) -> Self
123    where
124        E: std::error::Error + Send + Sync + 'static,
125    {
126        Self::Write(Box::new(err))
127    }
128}
129
130#[derive(Debug, Error)]
131#[non_exhaustive]
132#[error(transparent)]
133pub struct WriteCommit {
134    source: Box<StdError>,
135}
136
137impl WriteCommit {
138    pub fn other<E>(err: E) -> Self
139    where
140        E: std::error::Error + Send + Sync + 'static,
141    {
142        Self {
143            source: Box::new(err),
144        }
145    }
146}