route prefix

This commit is contained in:
Aravinth Manivannan 2021-04-12 18:23:56 +05:30
parent 09fbbe186c
commit d5593b2db6
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 59 additions and 3 deletions

View file

@ -1,5 +1,8 @@
## 0.1.1
### Added:
- Optional route prefix to `Processor`
### Changed:
- `Files::load()` became `Files::new()`

View file

@ -17,6 +17,7 @@
- [x] `SHA-256` based name generation during compile-time
- [x] Processes files based on provided MIME filters
- [x] Exposes modified names to program during runtime
- [x] Route prefixes(optional)
## Usage:

View file

@ -60,6 +60,9 @@ pub struct Buster {
/// directory for writing results
#[builder(setter(into))]
result: String,
#[builder(setter(into, strip_option), default)]
/// route prefixes
prefix: Option<String>,
/// copy other non-hashed files from source dire to result dir?
copy: bool,
/// follow symlinks?
@ -74,6 +77,7 @@ impl Buster {
fs::remove_dir_all(&self.result).unwrap();
}
println!("{}", &self.result);
fs::create_dir(&self.result).unwrap();
self.create_dir_structure(Path::new(&self.source))?;
Ok(())
@ -195,8 +199,22 @@ impl Buster {
// helper fn to generate filemap
fn gen_map<'a>(&self, source: &'a Path, name: &str) -> (&'a Path, PathBuf) {
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
let destination = Path::new(&self.result).join(rel_location).join(name);
(source, destination)
if let Some(prefix) = &self.prefix {
//panic!("{}", &prefix);
let mut result = self.result.as_str();
if result.chars().nth(0) == Some('/') {
result = &self.result[1..];
}
let destination = Path::new(prefix)
.join(&self.result[1..])
.join(rel_location)
.join(name);
(source, destination.into())
} else {
let destination = Path::new(&self.result).join(rel_location).join(name);
(source, destination.into())
}
}
// helper fn to copy files
@ -231,7 +249,6 @@ impl Buster {
Ok(())
}
}
/// Filemap struct
///
/// maps original names to generated names
@ -318,6 +335,7 @@ pub mod tests {
assert_eq!(src.exists(), dest.exists());
}
cleanup(&config);
}
@ -355,4 +373,38 @@ pub mod tests {
pub fn cleanup(config: &Buster) {
let _ = fs::remove_dir_all(&config.result);
}
#[test]
fn prefix_works() {
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
.result("/tmp/prod2i")
.mime_types(types)
.copy(true)
.follow_links(true)
.prefix("/test")
.build()
.unwrap();
config.process().unwrap();
let mut files = Files::load();
if let Some(prefix) = &config.prefix {
for (k, v) in files.map.drain() {
let src = Path::new(&k);
println!("{}", &v);
let dest = Path::new(&v[prefix.len()..]);
assert_eq!(src.exists(), dest.exists());
}
}
cleanup(&config);
}
}