use std::{collections::BTreeMap, fmt::Display, path::Path};
use garde::Validate;
use itertools::Itertools;
use crate::internal::sops::{self, SopsCreationRule, SopsFormat};
pub fn all_unique(s: &str, _: &()) -> garde::Result {
if s.chars().all_unique() {
Ok(())
} else {
Err(garde::Error::new("must not contain duplicate characters"))
}
}
pub fn is_file_path(path: &Path, _: &()) -> garde::Result {
if path.file_name().is_some() {
Ok(())
} else {
Err(garde::Error::new("must be a file path"))
}
}
pub fn dive_vec<T>(v: &[T], _: &()) -> garde::Result
where
T: Validate,
<T as Validate>::Context: Default,
{
for item in v {
item.validate().map_err(garde::Error::new)?;
}
Ok(())
}
pub fn dive_btreemap_values<K, T>(v: &BTreeMap<K, T>, _: &()) -> garde::Result
where
K: Display,
T: Validate,
<T as Validate>::Context: Default,
{
for value in v.values() {
value.validate().map_err(garde::Error::new)?;
}
Ok(())
}
pub fn is_sops_args_valid(config: &SopsCreationRule, _: &()) -> garde::Result {
if config.enable {
sops::encrypt(
Path::new("test"),
"test".to_string(),
config,
SopsFormat::Binary,
)
.map_err(garde::Error::new)?;
}
Ok(())
}