41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
use std::path::PathBuf;
|
|
|
|
#[derive(Clone)]
|
|
pub struct NixStorePath {
|
|
pub hash: String,
|
|
pub name: String,
|
|
}
|
|
|
|
impl NixStorePath {
|
|
pub fn from_str_symlink(path: &str) -> Result<Self, String> {
|
|
Ok(Self::from_path_buf_symlink(PathBuf::from(path))?)
|
|
}
|
|
|
|
pub fn from_path_buf_symlink(path: PathBuf) -> Result<Self, String> {
|
|
Ok(Self::from_path_buf(path.read_link().map_err(|err| err.to_string())?)?)
|
|
}
|
|
|
|
pub fn from_path_buf(path: PathBuf) -> Result<Self, String> {
|
|
let store_path_name = path.iter().nth(3)
|
|
.ok_or_else(|| String::from("Can't read store path name"))?
|
|
.to_str()
|
|
.ok_or_else(|| String::from("Failed converting store path name to string"))?
|
|
.to_string();
|
|
Ok(Self::from_store_path_name(store_path_name)?)
|
|
}
|
|
|
|
pub fn from_store_path_name(store_path_name: String) -> Result<Self, String> {
|
|
let (hash, name) = store_path_name
|
|
.split_once("-")
|
|
.ok_or_else(|| String::from("Failed splitting store path name for hash and name"))?;
|
|
Ok(Self {
|
|
hash: hash.to_string(),
|
|
name: name.to_string(),
|
|
})
|
|
}
|
|
|
|
pub fn to_prometheus_metric(self, infix: String) -> Result<String, String> {
|
|
return Ok(format!("nixos_{}_hash{{hash=\"{}\"}} 1\nnixos_{}_name{{name=\"{}\"}} 1\n", infix, self.hash, infix, self.name));
|
|
}
|
|
}
|