feat(path): ported content_path

This commit is contained in:
Kat Marchán 2019-05-20 17:34:29 -07:00
parent e5b375525d
commit 0f768fa5c0
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
3 changed files with 41 additions and 0 deletions

1
src/content/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod path;

38
src/content/path.rs Normal file
View File

@ -0,0 +1,38 @@
use std::path::{PathBuf, Path};
use ssri::Integrity;
const CONTENT_VERSION: &'static str = "2";
// Current format of content file path:
//
// sha512-BaSE64Hex= ->
// ~/.my-cache/content-v2/sha512/ba/da/55deadbeefc0ffee
//
pub fn content_path(cache: &Path, sri: &Integrity) -> PathBuf {
let mut path = PathBuf::new();
let (algo, hex) = sri.to_hex();
path.push(cache);
path.push(format!("content-v{}", CONTENT_VERSION));
path.push(algo.to_string());
path.push(&hex[0..2]);
path.push(&hex[2..4]);
path.push(&hex[4..]);
path
}
#[cfg(test)]
mod tests {
use super::content_path;
use ssri::{Integrity, Algorithm};
use std::path::Path;
#[test]
fn basic_test() {
let sri = Integrity::from(b"hello world", Algorithm::Sha256);
let cpath = content_path(Path::new("~/.my-cache"), &sri);
assert_eq!(
cpath.to_str().unwrap(),
"~/.my-cache/content-v2/sha256/b9/4d/27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
);
}
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
mod content;
pub use crate::content::path;