use anyhow::Result;
use crate::utils;
#[derive(Debug, Clone)]
pub struct ArgActionValue {
pub name: String,
pub value: String,
}
impl ArgActionValue {
pub fn new_with_check(name: &str, value: &str) -> Self {
if let Ok(path) = Self::resolve_path(value) {
return Self {
name: name.to_string(),
value: path,
};
}
Self::new(name, value)
}
fn new(name: &str, value: &str) -> Self {
Self {
name: name.to_string(),
value: value.to_string(),
}
}
fn resolve_path(val: &str) -> Result<String> {
if val.starts_with('/') || val.starts_with('~') || val.starts_with('.') {
utils::path::resolve(val)
.map(|p| p.display().to_string())
.map_err(|e| anyhow::anyhow!("Failed to resolve path '{}': {}", val, e))
} else {
anyhow::bail!("Not a path: '{}'", val)
}
}
}