mirror of https://github.com/zkat/cacache-rs.git
feat(path): ported content_path
This commit is contained in:
parent
e5b375525d
commit
0f768fa5c0
|
|
@ -0,0 +1 @@
|
|||
pub mod path;
|
||||
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
mod content;
|
||||
pub use crate::content::path;
|
||||
Loading…
Reference in New Issue