expose map and load map

This commit is contained in:
Aravinth Manivannan 2021-04-08 22:00:41 +05:30
parent 475c897316
commit d33c5cb16a
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 130 additions and 17 deletions

40
Cargo.lock generated
View file

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.9.0" version = "0.9.0"
@ -18,6 +20,7 @@ dependencies = [
"mime", "mime",
"mime_guess", "mime_guess",
"serde", "serde",
"serde_json",
"sha2", "sha2",
"walkdir", "walkdir",
] ]
@ -138,6 +141,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.16" version = "0.3.16"
@ -178,6 +187,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.6" version = "1.0.6"
@ -192,6 +207,31 @@ name = "serde"
version = "1.0.125" version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "sha2" name = "sha2"

View file

@ -18,4 +18,6 @@ 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"
serde_json = "1"
serde = { version = "1", features = ["derive"]}

3
actix-example/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
prod
tarpaulin-report.html

View file

@ -77,12 +77,10 @@ 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 let _ = file_map.add(
.add(
source.to_str().unwrap().into(), source.to_str().unwrap().into(),
destination.to_str().unwrap().into(), destination.to_str().unwrap().into(),
) );
.unwrap();
} }
} }
} }
@ -121,12 +119,10 @@ 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 let _ = file_map.add(
.add(
source.to_str().unwrap().into(), source.to_str().unwrap().into(),
destination.to_str().unwrap().into(), destination.to_str().unwrap().into(),
) );
.unwrap();
} }
} }
} }
@ -188,7 +184,7 @@ impl Buster {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub mod tests {
use super::*; use super::*;
#[test] #[test]
@ -218,6 +214,7 @@ mod tests {
assert_eq!(src.exists(), dest.exists()); assert_eq!(src.exists(), dest.exists());
} }
cleanup(&config);
} }
#[test] #[test]
@ -239,7 +236,7 @@ mod tests {
.unwrap(); .unwrap();
config.init().unwrap(); config.init().unwrap();
let mut files = config.try_hash().unwrap(); let mut files = config.hash().unwrap();
for (k, v) in files.map.drain() { for (k, v) in files.map.drain() {
let src = Path::new(&k); let src = Path::new(&k);
@ -247,5 +244,11 @@ mod tests {
assert_eq!(src.exists(), dest.exists()); assert_eq!(src.exists(), dest.exists());
} }
cleanup(&config);
}
pub fn cleanup(config: &Buster) {
let _ = fs::remove_dir_all(&config.result);
} }
} }

View file

@ -33,6 +33,6 @@
//! ``` //! ```
pub mod hash; pub mod hash;
pub mod map;
pub use hash::BusterBuilder; pub use hash::BusterBuilder;
pub mod map;
pub use map::Files; pub use map::Files;

View file

@ -6,14 +6,26 @@
*/ */
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
#[derive(Debug, Default, Clone)] use serde::{Deserialize, Serialize};
const ENV_VAR_NAME: &str = "CACHE_BUSTER_FILE_MAP";
#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)]
pub struct Files { pub struct Files {
pub map: HashMap<String, String>, pub map: HashMap<String, String>,
base_dir: String, base_dir: String,
} }
impl Files { impl Files {
pub fn new(base_dir: &str) -> Self {
Files {
map: HashMap::default(),
base_dir: base_dir.into(),
}
}
pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> { pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> {
self.map.get(path) self.map.get(path)
} }
@ -26,10 +38,32 @@ impl Files {
Ok(()) Ok(())
} }
} }
pub fn to_env(&self) {
println!(
"cargo:rustc-env={}={}",
ENV_VAR_NAME,
serde_json::to_string(&self).unwrap()
);
// needed for testing load()
// if the above statement fails(println), then something's broken
// with the rust compiler. So not really worried about that.
#[cfg(test)]
env::set_var(ENV_VAR_NAME, serde_json::to_string(&self).unwrap());
}
pub fn load() -> Self {
let env = env::var(ENV_VAR_NAME)
.expect("unable to read env var, might be a bug in lib. Please report on GitHub");
let res: Files = serde_json::from_str(&env).unwrap();
res
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::hash::tests::cleanup;
use crate::hash::*; use crate::hash::*;
use super::*; use super::*;
@ -64,6 +98,7 @@ mod tests {
assert!(!file_exists("dist/log-out.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)); assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
cleanup(&config);
} }
fn file_exists(path: &str, files: &Files) -> bool { fn file_exists(path: &str, files: &Files) -> bool {
@ -73,4 +108,34 @@ mod tests {
false false
} }
} }
#[test]
fn load_works() {
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
.result("/tmp/prod3")
.mime_types(types)
.copy(true)
.follow_links(true)
.build()
.unwrap();
config.init().unwrap();
let files = config.hash().unwrap();
files.to_env();
let x = Files::load();
assert_eq!(files, x);
cleanup(&config);
}
} }