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
use cached_path::CacheBuilder;
use rustbreak::PathDatabase;
use snafu::ResultExt;
use url::Url;

use super::{object::Objects, package::Packages, *};

use crate::extra::persist::Pot;

use super::ReadBackend;

#[derive(Debug)]
pub struct RemoteBackend {
    objects: Objects,
    packages: Packages,
}

impl ReadBackend for RemoteBackend {
    type Source = Url;

    fn open(source: Self::Source) -> crate::store::StoreResult<Self> {
        let cache = CacheBuilder::default().build().context(CacheSnafu)?;

        let path = cache.cached_path(source.as_str()).context(CacheSnafu)?;

        let db = PathDatabase::<(Objects, Packages), Pot>::load_from_path(path)
            .context(RustbreakLoadSnafu)?;
        let (objects, packages) = db.get_data(false).context(RustbreakLoadDataSnafu)?;

        Ok(Self { objects, packages })
    }

    fn packages(&self) -> &Packages {
        &self.packages
    }

    fn objects(&self) -> &Objects {
        &self.objects
    }
}