use {crate::rcfile::CustomType, serde::Serialize};
#[derive(Clone, Debug, Serialize)]
pub enum Strategy {
NameAndVersionProps,
NamedVersionString,
UnnamedVersionString,
VersionsByName,
InvalidConfig,
}
impl Strategy {
pub fn new(strategy: &str) -> Strategy {
match strategy {
"name~version" => Strategy::NameAndVersionProps,
"name@version" => Strategy::NamedVersionString,
"version" => Strategy::UnnamedVersionString,
"versionsByName" => Strategy::VersionsByName,
_ => Strategy::InvalidConfig,
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct DependencyType {
pub name_path: Option<String>,
pub name: String,
pub path: String,
pub strategy: Strategy,
}
impl DependencyType {
pub fn new(name: &str, config: &CustomType) -> DependencyType {
DependencyType {
name_path: config.name_path.clone().map(normalize_path),
name: name.to_string(),
path: normalize_path(config.path.clone()),
strategy: Strategy::new(config.strategy.as_str()),
}
}
}
fn normalize_path(path: String) -> String {
let mut normalized_path = String::from("/");
normalized_path.push_str(&path.replace('.', "/"));
normalized_path
}