oci_spec/error.rs
1//! Error types of the crate.
2
3use std::{borrow::Cow, io};
4use thiserror::Error;
5
6/// Spezialized result type for oci spec operations. It is
7/// used for any operation that might produce an error. This
8/// typedef is generally used to avoid writing out
9/// [OciSpecError] directly and is otherwise a direct mapping
10/// to [Result](std::result::Result).
11pub type Result<T> = std::result::Result<T, OciSpecError>;
12
13/// Error type for oci spec errors.
14#[derive(Error, Debug)]
15pub enum OciSpecError {
16 /// Will be returned if an error occurs that cannot
17 /// be mapped to a more specialized error variant.
18 #[error("{0}")]
19 Other(String),
20
21 /// Will be returned when an error happens during
22 /// io operations.
23 #[error("io operation failed")]
24 Io(#[from] io::Error),
25
26 /// Will be returned when an error happens during
27 /// serialization or deserialization.
28 #[error("serde failed")]
29 SerDe(#[from] serde_json::Error),
30
31 /// Builder specific errors.
32 #[error("uninitialized field")]
33 Builder(#[from] derive_builder::UninitializedFieldError),
34}
35
36pub(crate) fn oci_error<'a, M>(message: M) -> OciSpecError
37where
38 M: Into<Cow<'a, str>>,
39{
40 let message = message.into();
41 match message {
42 Cow::Borrowed(s) => OciSpecError::Other(s.to_owned()),
43 Cow::Owned(s) => OciSpecError::Other(s),
44 }
45}