use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::iter;
#[derive(Clone, Debug)]
pub struct TemporaryStorage {
locations: Vec<String>,
}
impl TemporaryStorage {
pub fn new(locations: Vec<String>) -> Self {
TemporaryStorage { locations }
}
pub fn find_scheme<'a, 'b>(&'a self, scheme: &'b str) -> Option<&'a str> {
assert!(scheme.ends_with(':'));
self.locations
.iter()
.find(|l| l.starts_with(scheme))
.map(|l| l.as_str())
}
pub fn random_tag() -> String {
let mut rng = thread_rng();
iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(10)
.collect::<String>()
}
}
#[test]
fn find_schema() {
let storage = TemporaryStorage::new(vec![
"s3://example/".to_string(),
"gs://example/1/".to_string(),
"gs://example/2/".to_string(),
]);
assert_eq!(storage.find_scheme("s3:"), Some("s3://example/"));
assert_eq!(storage.find_scheme("gs:"), Some("gs://example/1/"));
}
#[test]
fn random_tag() {
assert_eq!(TemporaryStorage::random_tag().len(), 10);
}