diff --git a/cache_buster/hash/index.html b/cache_buster/hash/index.html index 1f86a81..267f47a 100644 --- a/cache_buster/hash/index.html +++ b/cache_buster/hash/index.html @@ -1,7 +1,7 @@ cache_buster::hash - Rust

Module cache_buster::hash[][src]

Structs

+ Change settings

Module cache_buster::hash[][src]

Structs

Buster
BusterBuilder

Builder for Buster.

Enums

BusterBuilderError

Error type for BusterBuilder

diff --git a/cache_buster/hash/struct.Buster.html b/cache_buster/hash/struct.Buster.html index 16f895f..8dba7c4 100644 --- a/cache_buster/hash/struct.Buster.html +++ b/cache_buster/hash/struct.Buster.html @@ -1,7 +1,7 @@ cache_buster::hash::Buster - Rust

Struct cache_buster::hash::Buster[][src]

pub struct Buster { /* fields omitted */ }

Implementations

impl Buster[src]

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

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

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

Trait Implementations

impl Clone for Buster[src]

Struct cache_buster::hash::Buster[][src]

pub struct Buster { /* fields omitted */ }

Implementations

impl Buster[src]

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

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

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

Trait Implementations

impl Clone for Buster[src]

impl Debug for Buster[src]

Auto Trait Implementations

Blanket Implementations

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

Module cache_buster::map[][src]

Structs

Files
\ No newline at end of file diff --git a/cache_buster/map/struct.Files.html b/cache_buster/map/struct.Files.html index fc04855..b9fe587 100644 --- a/cache_buster/map/struct.Files.html +++ b/cache_buster/map/struct.Files.html @@ -1,18 +1,23 @@ cache_buster::map::Files - Rust -

Struct cache_buster::map::Files[][src]

pub struct Files {
+

Struct cache_buster::map::Files[][src]

pub struct Files {
     pub map: HashMap<String, String>,
     // some fields omitted
 }

- Fields

map: HashMap<String, String>

Implementations

impl Files[src]

pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String>[src]

pub fn add(&mut self, k: String, v: String) -> Result<(), &'static str>[src]

Trait Implementations

impl Clone for Files[src]

 /*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
@@ -332,12 +335,10 @@
                         self.copy(path, &new_name);
 
                         let (source, destination) = self.gen_map(path, &&new_name);
-                        file_map
-                            .add(
-                                source.to_str().unwrap().into(),
-                                destination.to_str().unwrap().into(),
-                            )
-                            .unwrap();
+                        let _ = file_map.add(
+                            source.to_str().unwrap().into(),
+                            destination.to_str().unwrap().into(),
+                        );
                     }
                 }
             }
@@ -376,12 +377,10 @@
                         );
                         self.copy(path, &new_name);
                         let (source, destination) = self.gen_map(path, &&new_name);
-                        file_map
-                            .add(
-                                source.to_str().unwrap().into(),
-                                destination.to_str().unwrap().into(),
-                            )
-                            .unwrap();
+                        let _ = file_map.add(
+                            source.to_str().unwrap().into(),
+                            destination.to_str().unwrap().into(),
+                        );
                     }
                 }
             }
@@ -443,7 +442,7 @@
 }
 
 #[cfg(test)]
-mod tests {
+pub mod tests {
     use super::*;
 
     #[test]
@@ -473,6 +472,7 @@
 
             assert_eq!(src.exists(), dest.exists());
         }
+        cleanup(&config);
     }
 
     #[test]
@@ -494,7 +494,7 @@
             .unwrap();
 
         config.init().unwrap();
-        let mut files = config.try_hash().unwrap();
+        let mut files = config.hash().unwrap();
 
         for (k, v) in files.map.drain() {
             let src = Path::new(&k);
@@ -502,6 +502,12 @@
 
             assert_eq!(src.exists(), dest.exists());
         }
+
+        cleanup(&config);
+    }
+
+    pub fn cleanup(config: &Buster) {
+        let _ = fs::remove_dir_all(&config.result);
     }
 }
 
diff --git a/src/cache_buster/lib.rs.html b/src/cache_buster/lib.rs.html index d8fa53a..736128f 100644 --- a/src/cache_buster/lib.rs.html +++ b/src/cache_buster/lib.rs.html @@ -75,8 +75,8 @@ //! ``` pub mod hash; -pub mod map; pub use hash::BusterBuilder; +pub mod map; pub use map::Files;
diff --git a/src/cache_buster/map.rs.html b/src/cache_buster/map.rs.html index 49e3d15..9760071 100644 --- a/src/cache_buster/map.rs.html +++ b/src/cache_buster/map.rs.html @@ -1,82 +1,147 @@ map.rs - source
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
-22
-23
-24
-25
-26
-27
-28
-29
-30
-31
-32
-33
-34
-35
-36
-37
-38
-39
-40
-41
-42
-43
-44
-45
-46
-47
-48
-49
-50
-51
-52
-53
-54
-55
-56
-57
-58
-59
-60
-61
-62
-63
-64
-65
-66
-67
-68
-69
-70
-71
-72
-73
-74
-75
-76
+                Change settings
  1
+  2
+  3
+  4
+  5
+  6
+  7
+  8
+  9
+ 10
+ 11
+ 12
+ 13
+ 14
+ 15
+ 16
+ 17
+ 18
+ 19
+ 20
+ 21
+ 22
+ 23
+ 24
+ 25
+ 26
+ 27
+ 28
+ 29
+ 30
+ 31
+ 32
+ 33
+ 34
+ 35
+ 36
+ 37
+ 38
+ 39
+ 40
+ 41
+ 42
+ 43
+ 44
+ 45
+ 46
+ 47
+ 48
+ 49
+ 50
+ 51
+ 52
+ 53
+ 54
+ 55
+ 56
+ 57
+ 58
+ 59
+ 60
+ 61
+ 62
+ 63
+ 64
+ 65
+ 66
+ 67
+ 68
+ 69
+ 70
+ 71
+ 72
+ 73
+ 74
+ 75
+ 76
+ 77
+ 78
+ 79
+ 80
+ 81
+ 82
+ 83
+ 84
+ 85
+ 86
+ 87
+ 88
+ 89
+ 90
+ 91
+ 92
+ 93
+ 94
+ 95
+ 96
+ 97
+ 98
+ 99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+134
+135
+136
+137
+138
+139
+140
+141
 
 /*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
@@ -86,14 +151,26 @@
 */
 
 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 map: HashMap<String, String>,
     base_dir: String,
 }
 
 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> {
         self.map.get(path)
     }
@@ -106,10 +183,32 @@
             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)]
 mod tests {
+    use crate::hash::tests::cleanup;
     use crate::hash::*;
 
     use super::*;
@@ -144,6 +243,7 @@
 
         assert!(!file_exists("dist/log-out.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 {
@@ -153,6 +253,36 @@
             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);
+    }
 }