1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::error::Error;
use std::sync::Arc;

use async_trait::async_trait;

pub type S3ClientError = Arc<dyn Error + Send + Sync + 'static>;

#[derive(Debug, thiserror::Error, Clone)]
pub struct GetObjectBytesError(pub S3ClientError);

impl std::ops::Deref for GetObjectBytesError {
    type Target = S3ClientError;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for GetObjectBytesError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "GetObjectBytesError: {}", self.0)
    }
}

impl From<aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::get_object::GetObjectError>>
    for GetObjectBytesError
{
    fn from(
        error: aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::get_object::GetObjectError>,
    ) -> Self {
        Self(Arc::new(error))
    }
}

impl From<aws_smithy_types::byte_stream::error::Error> for GetObjectBytesError {
    fn from(error: aws_smithy_types::byte_stream::error::Error) -> Self {
        Self(Arc::new(error))
    }
}

#[derive(Debug, thiserror::Error, Clone)]
pub struct ListCommonPrefixesError(pub S3ClientError);

impl std::fmt::Display for ListCommonPrefixesError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ListCommonPrefixesError: {}", self.0)
    }
}

impl std::ops::Deref for ListCommonPrefixesError {
    type Target = S3ClientError;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::list_objects_v2::ListObjectsV2Error>>
    for ListCommonPrefixesError
{
    fn from(
        error: aws_sdk_s3::error::SdkError<
            aws_sdk_s3::operation::list_objects_v2::ListObjectsV2Error,
        >,
    ) -> Self {
        Self(Arc::new(error))
    }
}

#[async_trait]
pub trait S3Client: Send + Sync {
    async fn get_object_bytes(
        &self,
        bucket: &str,
        prefix: &str,
    ) -> Result<Vec<u8>, GetObjectBytesError>;

    async fn list_common_prefixes(
        &self,
        bucket: &str,
        start_after_prefix: &str,
    ) -> Result<Vec<String>, ListCommonPrefixesError>;
}