ayun_storage/drivers/
mod.rs

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
use crate::traits::{GetResponse, PutResponse, StorageResult, StoreDriver};
use async_trait::async_trait;
use bytes::Bytes;
use std::path::Path;

pub mod local;
pub mod mem;
pub mod null;

pub struct Adapter {
    inner: Box<dyn object_store::ObjectStore>,
}

impl Adapter {
    pub fn new(inner: Box<dyn object_store::ObjectStore>) -> Self {
        Self { inner }
    }
}

#[async_trait]
impl StoreDriver for Adapter {
    async fn put(&self, path: &Path, content: &Bytes) -> StorageResult<PutResponse> {
        let path = object_store::path::Path::from(path.display().to_string());
        let result = self.inner.put(&path, content.clone().into()).await?;

        Ok(PutResponse {
            e_tag: result.e_tag,
            version: result.version,
        })
    }

    async fn get(&self, path: &Path) -> StorageResult<GetResponse> {
        let path = object_store::path::Path::from(path.display().to_string());

        Ok(self.inner.get(&path).await?)
    }

    async fn delete(&self, path: &Path) -> StorageResult<()> {
        let path = object_store::path::Path::from(path.display().to_string());

        Ok(self.inner.delete(&path).await?)
    }

    async fn rename(&self, from: &Path, to: &Path) -> StorageResult<()> {
        let from = object_store::path::Path::from(from.display().to_string());
        let to = object_store::path::Path::from(to.display().to_string());

        Ok(self.inner.rename(&from, &to).await?)
    }

    async fn copy(&self, from: &Path, to: &Path) -> StorageResult<()> {
        let from = object_store::path::Path::from(from.display().to_string());
        let to = object_store::path::Path::from(to.display().to_string());

        Ok(self.inner.copy(&from, &to).await?)
    }

    async fn exists(&self, path: &Path) -> StorageResult<bool> {
        let path = object_store::path::Path::from(path.display().to_string());

        Ok(self.inner.get(&path).await.is_ok())
    }
}