map
This commit is contained in:
parent
9abdf468b1
commit
475c897316
6 changed files with 109 additions and 17 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
prod
|
prod
|
||||||
|
tarpaulin-report.html
|
||||||
|
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -17,6 +17,7 @@ dependencies = [
|
||||||
"derive_builder",
|
"derive_builder",
|
||||||
"mime",
|
"mime",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
|
"serde",
|
||||||
"sha2",
|
"sha2",
|
||||||
"walkdir",
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
@ -186,6 +187,12 @@ dependencies = [
|
||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.125"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha2"
|
name = "sha2"
|
||||||
version = "0.9.3"
|
version = "0.9.3"
|
||||||
|
|
|
@ -18,3 +18,4 @@ sha2 = "0.9.3"
|
||||||
derive_builder = "0.10.0"
|
derive_builder = "0.10.0"
|
||||||
data-encoding = "2.3.2"
|
data-encoding = "2.3.2"
|
||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
|
serde = "1.0.125"
|
||||||
|
|
31
src/hash.rs
31
src/hash.rs
|
@ -5,7 +5,6 @@
|
||||||
* License.
|
* License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
@ -13,6 +12,8 @@ use std::{fs, path::PathBuf};
|
||||||
use derive_builder::Builder;
|
use derive_builder::Builder;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
use crate::map::Files;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Builder)]
|
#[derive(Debug, Clone, Builder)]
|
||||||
pub struct Buster {
|
pub struct Buster {
|
||||||
// source directory
|
// source directory
|
||||||
|
@ -52,8 +53,8 @@ impl Buster {
|
||||||
// use [hash] when when they aren't
|
// use [hash] when when they aren't
|
||||||
//
|
//
|
||||||
// doesn't process files for which mime is not resolved
|
// doesn't process files for which mime is not resolved
|
||||||
pub fn try_hash(&self) -> Result<HashMap<String, String>, Error> {
|
pub fn try_hash(&self) -> Result<Files, Error> {
|
||||||
let mut file_map: HashMap<String, String> = HashMap::default();
|
let mut file_map: Files = Files::default();
|
||||||
for entry in WalkDir::new(&self.source)
|
for entry in WalkDir::new(&self.source)
|
||||||
.follow_links(self.follow_links)
|
.follow_links(self.follow_links)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -76,10 +77,12 @@ impl Buster {
|
||||||
self.copy(path, &new_name);
|
self.copy(path, &new_name);
|
||||||
|
|
||||||
let (source, destination) = self.gen_map(path, &&new_name);
|
let (source, destination) = self.gen_map(path, &&new_name);
|
||||||
file_map.insert(
|
file_map
|
||||||
|
.add(
|
||||||
source.to_str().unwrap().into(),
|
source.to_str().unwrap().into(),
|
||||||
destination.to_str().unwrap().into(),
|
destination.to_str().unwrap().into(),
|
||||||
);
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,8 +93,8 @@ impl Buster {
|
||||||
|
|
||||||
// panics when mimetypes are detected. This way you'll know which files are ignored
|
// panics when mimetypes are detected. This way you'll know which files are ignored
|
||||||
// from processing
|
// from processing
|
||||||
pub fn hash(&self) -> Result<HashMap<String, String>, Error> {
|
pub fn hash(&self) -> Result<Files, Error> {
|
||||||
let mut file_map: HashMap<String, String> = HashMap::default();
|
let mut file_map: Files = Files::default();
|
||||||
|
|
||||||
for entry in WalkDir::new(&self.source)
|
for entry in WalkDir::new(&self.source)
|
||||||
.follow_links(self.follow_links)
|
.follow_links(self.follow_links)
|
||||||
|
@ -118,10 +121,12 @@ impl Buster {
|
||||||
);
|
);
|
||||||
self.copy(path, &new_name);
|
self.copy(path, &new_name);
|
||||||
let (source, destination) = self.gen_map(path, &&new_name);
|
let (source, destination) = self.gen_map(path, &&new_name);
|
||||||
file_map.insert(
|
file_map
|
||||||
|
.add(
|
||||||
source.to_str().unwrap().into(),
|
source.to_str().unwrap().into(),
|
||||||
destination.to_str().unwrap().into(),
|
destination.to_str().unwrap().into(),
|
||||||
);
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,9 +210,9 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
config.init().unwrap();
|
config.init().unwrap();
|
||||||
let mut map = config.hash().unwrap();
|
let mut files = config.hash().unwrap();
|
||||||
|
|
||||||
for (k, v) in map.drain() {
|
for (k, v) in files.map.drain() {
|
||||||
let src = Path::new(&k);
|
let src = Path::new(&k);
|
||||||
let dest = Path::new(&v);
|
let dest = Path::new(&v);
|
||||||
|
|
||||||
|
@ -234,9 +239,9 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
config.init().unwrap();
|
config.init().unwrap();
|
||||||
let mut map = config.hash().unwrap();
|
let mut files = config.try_hash().unwrap();
|
||||||
|
|
||||||
for (k, v) in map.drain() {
|
for (k, v) in files.map.drain() {
|
||||||
let src = Path::new(&k);
|
let src = Path::new(&k);
|
||||||
let dest = Path::new(&v);
|
let dest = Path::new(&v);
|
||||||
|
|
||||||
|
|
|
@ -33,4 +33,6 @@
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
|
pub mod map;
|
||||||
pub use hash::BusterBuilder;
|
pub use hash::BusterBuilder;
|
||||||
|
pub use map::Files;
|
||||||
|
|
76
src/map.rs
Normal file
76
src/map.rs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by the Apache 2.0 and/or the MIT
|
||||||
|
* License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct Files {
|
||||||
|
pub map: HashMap<String, String>,
|
||||||
|
base_dir: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Files {
|
||||||
|
pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> {
|
||||||
|
self.map.get(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(&mut self, k: String, v: String) -> Result<(), &'static str> {
|
||||||
|
if self.map.contains_key(&k) {
|
||||||
|
Err("key exists")
|
||||||
|
} else {
|
||||||
|
self.map.insert(k, v);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::hash::*;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_works() {
|
||||||
|
let types = vec![
|
||||||
|
mime::IMAGE_PNG,
|
||||||
|
mime::IMAGE_SVG,
|
||||||
|
mime::IMAGE_JPEG,
|
||||||
|
mime::IMAGE_GIF,
|
||||||
|
];
|
||||||
|
|
||||||
|
let config = BusterBuilder::default()
|
||||||
|
.source("./dist")
|
||||||
|
.result("/tmp/prod2")
|
||||||
|
.mime_types(types)
|
||||||
|
.copy(true)
|
||||||
|
.follow_links(true)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
config.init().unwrap();
|
||||||
|
let files = config.hash().unwrap();
|
||||||
|
|
||||||
|
assert!(file_exists("./dist/log-out.svg", &files));
|
||||||
|
assert!(file_exists(
|
||||||
|
"./dist/a/b/c/d/s/d/svg/credit-card.svg",
|
||||||
|
&files
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(!file_exists("dist/log-out.svg", &files));
|
||||||
|
assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_exists(path: &str, files: &Files) -> bool {
|
||||||
|
if let Some(file) = files.get(path) {
|
||||||
|
Path::new(file).exists()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue