use crate::fetch::Progress;
#[cfg(any(not(feature = "mirror-http"), not(feature = "mirror-s3")))]
use crate::DatastoreError;
use crate::{ArtifactKey, Result};
use std::io::Write;
use std::time::Duration;
#[cfg(feature = "mirror-http")]
pub(crate) mod http;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mirror {
Http { base_url: String, writable: bool },
S3 {
bucket: String,
prefix: String,
region: String,
writable: bool,
},
}
pub(crate) trait MirrorRead: Send + Sync {
fn fetch(&self, key: &ArtifactKey, sink: &mut dyn Write) -> Result<bool>;
fn location(&self, key: &ArtifactKey) -> String;
}
#[cfg_attr(not(feature = "mirror-http"), allow(unused_variables))]
pub(crate) fn open(
mirror: &Mirror,
timeout: Duration,
progress: Progress,
) -> Result<Box<dyn MirrorRead>> {
match mirror {
#[cfg(feature = "mirror-http")]
Mirror::Http { base_url, .. } => Ok(Box::new(http::HttpMirror::new(
base_url, timeout, progress,
)?)),
#[cfg(not(feature = "mirror-http"))]
Mirror::Http { .. } => Err(DatastoreError::Mirror(
"this build has no HTTP mirror support (feature `mirror-http`)".into(),
)),
#[cfg(feature = "mirror-s3")]
Mirror::S3 {
bucket,
prefix,
region,
..
} => Ok(Box::new(crate::S3Mirror::new(bucket, prefix, region)?)),
#[cfg(not(feature = "mirror-s3"))]
Mirror::S3 { .. } => Err(DatastoreError::Mirror(
"this build has no S3 mirror support (feature `mirror-s3`)".into(),
)),
}
}