use super::{Config, StringList, Value};
use serde::{de::Error, Deserialize};
use std::path::PathBuf;
#[derive(Debug, Deserialize, PartialEq, Clone)]
#[serde(transparent)]
pub struct ConfigRelativePath(Value<String>);
impl ConfigRelativePath {
pub fn raw_value(&self) -> &str {
&self.0.val
}
pub fn resolve_path(&self, config: &Config) -> PathBuf {
self.0.definition.root(config).join(&self.0.val)
}
pub fn resolve_program(self, config: &Config) -> PathBuf {
config.string_to_path(self.0.val, &self.0.definition)
}
}
#[derive(Debug, Clone)]
pub struct PathAndArgs {
pub path: ConfigRelativePath,
pub args: Vec<String>,
}
impl<'de> serde::Deserialize<'de> for PathAndArgs {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let vsl = Value::<StringList>::deserialize(deserializer)?;
let mut strings = vsl.val.0;
if strings.is_empty() {
return Err(D::Error::invalid_length(0, &"at least one element"));
}
let first = strings.remove(0);
let crp = Value {
val: first,
definition: vsl.definition,
};
Ok(PathAndArgs {
path: ConfigRelativePath(crp),
args: strings,
})
}
}