meow meow
This commit is contained in:
38
src/nix_path.rs
Normal file
38
src/nix_path.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
const NIX_STORE_PREFIX: &str = "/nix/store/";
|
||||
const NIX_HASH_LENGTH: usize = 32;
|
||||
pub fn extract_package_name(path: &str) -> Option<String> {
|
||||
let without_prefix = path.strip_prefix(NIX_STORE_PREFIX)?;
|
||||
|
||||
if without_prefix.len() <= NIX_HASH_LENGTH + 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let after_hash = &without_prefix[NIX_HASH_LENGTH + 1..];
|
||||
let parts: Vec<&str> = after_hash.split('-').collect();
|
||||
|
||||
if parts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name_parts: Vec<&str> = parts
|
||||
.iter()
|
||||
.take_while(|part| !part.chars().next().map_or(false, |c| c.is_ascii_digit()))
|
||||
.copied()
|
||||
.collect();
|
||||
|
||||
if name_parts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(name_parts.join("-"))
|
||||
}
|
||||
|
||||
pub fn extract_full_name(path: &str) -> Option<&str> {
|
||||
let without_prefix = path.strip_prefix(NIX_STORE_PREFIX)?;
|
||||
|
||||
if without_prefix.len() <= NIX_HASH_LENGTH + 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(&without_prefix[NIX_HASH_LENGTH + 1..])
|
||||
}
|
||||
Reference in New Issue
Block a user