Skip to main content

storage_transfer_lib/
error.rs

1use std::fmt;
2
3use tokio::sync::Semaphore;
4
5#[derive(Debug)]
6pub enum Error {
7    InvalidLimit { field: &'static str, value: usize },
8    InvalidRoot,
9    InvalidConfig(&'static str),
10    UnsupportedEntry(String),
11    Storage(opendal::Error),
12    ConsumerClosed,
13    Unsupported(String),
14    Report(std::io::Error),
15    Task(tokio::task::JoinError),
16}
17
18pub type Result<T> = std::result::Result<T, Error>;
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::InvalidLimit { field, value } => {
24                write!(
25                    f,
26                    "{field} must be in 1..={}, got {value}",
27                    Semaphore::MAX_PERMITS
28                )
29            }
30            Self::InvalidConfig(field) => write!(f, "invalid transfer configuration: {field}"),
31            Self::InvalidRoot => write!(f, "operators must be rooted at the bucket root (/)"),
32            Self::UnsupportedEntry(path) => write!(f, "unknown entry type: {path}"),
33            Self::Storage(error) => write!(f, "storage operation failed: {error}"),
34            Self::Unsupported(reason) => write!(f, "unsupported copy: {reason}"),
35            Self::Report(error) => write!(f, "transfer report failed: {error}"),
36            Self::Task(error) => write!(f, "transfer task failed: {error}"),
37            Self::ConsumerClosed => {
38                write!(f, "object queue receiver closed before listing completed")
39            }
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Self::Storage(error) => Some(error),
48            Self::Task(error) => Some(error),
49            Self::Report(error) => Some(error),
50            _ => None,
51        }
52    }
53}
54
55impl From<opendal::Error> for Error {
56    fn from(error: opendal::Error) -> Self {
57        Self::Storage(error)
58    }
59}