diff --git a/cache_buster/processor/index.html b/cache_buster/processor/index.html index 3dbe58b..bc95fb1 100644 --- a/cache_buster/processor/index.html +++ b/cache_buster/processor/index.html @@ -1,7 +1,7 @@ cache_buster::processor - Rust

Module cache_buster::processor[][src]

Module describing file processor that changes filenames to setup cache-busting

+ Change settings

Module cache_buster::processor[][src]

Module describing file processor that changes filenames to setup cache-busting

Run the following during build using build.rs:

diff --git a/cache_buster/processor/struct.Buster.html b/cache_buster/processor/struct.Buster.html
index 9e4b3ef..e8a9c4b 100644
--- a/cache_buster/processor/struct.Buster.html
+++ b/cache_buster/processor/struct.Buster.html
@@ -1,13 +1,13 @@
 Buster in cache_buster::processor - Rust
 
 

Struct cache_buster::processor::Buster[][src]

pub struct Buster { /* fields omitted */ }

Configuration for setting up cache-busting

-

Implementations

impl Buster[src]

pub fn process(&self) -> Result<(), Error>[src]

Processes files.

+ Change settings

Struct cache_buster::processor::Buster[][src]

pub struct Buster<'a> { /* fields omitted */ }

Configuration for setting up cache-busting

+

Implementations

impl<'a> Buster<'a>[src]

pub fn process(&self) -> Result<(), Error>[src]

Processes files.

Panics when a weird MIME is encountered.

-

Trait Implementations

impl Clone for Buster[src]

Trait Implementations

impl<'a> Clone for Buster<'a>[src]

impl Debug for Buster[src]

Auto Trait Implementations

impl RefUnwindSafe for Buster

impl Send for Buster

impl Sync for Buster

impl Unpin for Buster

impl UnwindSafe for Buster

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<'a> Debug for Buster<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Buster<'a>

impl<'a> Send for Buster<'a>

impl<'a> Sync for Buster<'a>

impl<'a> Unpin for Buster<'a>

impl<'a> UnwindSafe for Buster<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

 /*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
@@ -405,7 +460,8 @@
 
 /// Configuration for setting up cache-busting
 #[derive(Debug, Clone, Builder)]
-pub struct Buster {
+#[builder(build_fn(validate = "Self::validate"))]
+pub struct Buster<'a> {
     /// source directory
     #[builder(setter(into))]
     source: String,
@@ -421,9 +477,30 @@
     copy: bool,
     /// follow symlinks?
     follow_links: bool,
+    /// exclude these files for hashing.
+    /// They will be copied over without including a hash in the filename
+    /// Path should be relative to [self.source]
+    #[builder(default)]
+    no_hash: Vec<&'a str>,
 }
 
-impl Buster {
+impl<'a> BusterBuilder<'a> {
+    fn validate(&self) -> Result<(), String> {
+        for file in self.no_hash.iter() {
+            for file in file.iter() {
+                if !Path::new(&self.source.as_ref().unwrap())
+                    .join(file)
+                    .exists()
+                {
+                    return Err(format!("File {} doesn't exist", file));
+                }
+            }
+        }
+        Ok(())
+    }
+}
+
+impl<'a> Buster<'a> {
     // creates base_dir to output files to
     fn init(&self) -> Result<(), Error> {
         let res = Path::new(&self.result);
@@ -461,7 +538,7 @@
             let entry = entry?;
 
             let path = entry.path();
-            if !path.is_dir() {
+            if !path.is_dir() && !self.no_hash.contains(&path.to_str().unwrap()) {
                 let path = Path::new(&path);
 
                 for mime_type in self.mime_types.iter() {
@@ -504,7 +581,7 @@
     }
 
     // helper fn to generate filemap
-    fn gen_map<'a>(&self, source: &'a Path, name: &str) -> (&'a Path, PathBuf) {
+    fn gen_map<'b>(&self, source: &'b Path, name: &str) -> (&'b Path, PathBuf) {
         let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
         if let Some(prefix) = &self.prefix {
             //panic!("{}", &prefix);
@@ -672,6 +749,8 @@
             mime::IMAGE_GIF,
         ];
 
+        let no_hash = vec!["bell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+
         let config = BusterBuilder::default()
             .source("./dist")
             .result("/tmp/prod2i")
@@ -679,6 +758,7 @@
             .copy(true)
             .follow_links(true)
             .prefix("/test")
+            .no_hash(no_hash.clone())
             .build()
             .unwrap();
 
@@ -686,6 +766,14 @@
         let mut files = Files::load();
 
         if let Some(prefix) = &config.prefix {
+            no_hash.iter().for_each(|file| {
+                files.map.iter().any(|(k, v)| {
+                    let dest = Path::new(&v[prefix.len()..]);
+                    let no_hash = Path::new(file);
+                    k == file && dest.exists() && no_hash.file_name() == dest.file_name()
+                });
+            });
+
             for (k, v) in files.map.drain() {
                 let src = Path::new(&k);
                 let dest = Path::new(&v[prefix.len()..]);
@@ -697,6 +785,28 @@
         cleanup(&config);
     }
 
+    #[test]
+    fn no_hash_validation_works() {
+        let types = vec![
+            mime::IMAGE_PNG,
+            mime::IMAGE_SVG,
+            mime::IMAGE_JPEG,
+            mime::IMAGE_GIF,
+        ];
+
+        let no_hash = vec!["bbell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
+        assert!(BusterBuilder::default()
+            .source("./dist")
+            .result("/tmp/prod2i")
+            .mime_types(types)
+            .copy(true)
+            .follow_links(true)
+            .prefix("/test")
+            .no_hash(no_hash.clone())
+            .build()
+            .is_err())
+    }
+
     pub fn runner() {
         prefix_works();
         hasher_works();