#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
use std::fs;
use std::path::PathBuf;
pub fn normalizePasswordStorePath(storePath: &str) -> Result<PathBuf, String> {
let mut storePath = storePath.to_string();
if storePath.is_empty() { return Err("The store path cannot be empty".to_string()); }
if storePath.starts_with("~/") { storePath = format!("$HOME/{}", &storePath[2..]); }
{
let s = storePath;
let mut out = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c != '$' { out.push(c); continue; }
match chars.peek() {
Some(&'{') => {
chars.next();
let mut name = String::new();
for ch in chars.by_ref() {
if ch == '}' { break; }
name.push(ch);
}
out.push_str(&std::env::var(&name).unwrap_or_default());
}
Some(&ch) if ch.is_ascii_alphabetic() || ch == '_' => {
let mut name = String::new();
while let Some(&next) = chars.peek() {
if next.is_ascii_alphanumeric() || next == '_' {
name.push(next);
chars.next();
} else { break; }
}
out.push_str(&std::env::var(&name).unwrap_or_default());
}
_ => out.push('$'),
}
}
storePath = out;
}
let directStorePath = fs::canonicalize(&storePath) .map_err(|e| format!("{e}"))?; let storePath = directStorePath;
let stat = fs::metadata(&storePath) .map_err(|e| format!("{e}"))?; if !stat.is_dir() { return Err("The specified path exists, but is not a directory".to_string()); }
Ok(storePath) }
#[allow(non_snake_case)]
const _: () = ();