pegase 0.1.0

cli to generate secret tree files from templates
Documentation
use std::collections::BTreeMap;

use garde::Validate;
use serde::{Deserialize, Deserializer, Serialize, de};
use serde_value::Value;

use crate::{
    generators::{GenerateSecret, GenerationError, Secret, SecretGenerator},
    internal::garde::{dive_btreemap_values, dive_vec},
};

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SecretTree {
    List(Vec<SecretTree>),
    Map(BTreeMap<String, SecretTree>),
    Secret(Secret),
}

#[derive(Debug, Serialize, Validate)]
#[serde(untagged)]
pub enum GeneratorTree {
    List(#[garde(custom(dive_vec))] Vec<GeneratorTree>),
    Map(#[garde(custom(dive_btreemap_values))] BTreeMap<String, GeneratorTree>),
    Generator(#[garde(dive)] SecretGenerator),
}

impl<'de> Deserialize<'de> for GeneratorTree {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match Value::deserialize(deserializer)? {
            Value::String(value) => {
                let mut mapping = BTreeMap::new();
                mapping.insert(Value::String("generator".to_string()), Value::String(value));

                SecretGenerator::deserialize(Value::Map(mapping))
                    .map(GeneratorTree::Generator)
                    .map_err(de::Error::custom)
            }
            Value::Seq(value) => Vec::<GeneratorTree>::deserialize(Value::Seq(value))
                .map(GeneratorTree::List)
                .map_err(de::Error::custom),
            Value::Map(value) => {
                if value.contains_key(&Value::String("generator".to_string())) {
                    SecretGenerator::deserialize(Value::Map(value))
                        .map(GeneratorTree::Generator)
                        .map_err(de::Error::custom)
                } else {
                    BTreeMap::<String, GeneratorTree>::deserialize(Value::Map(value))
                        .map(GeneratorTree::Map)
                        .map_err(de::Error::custom)
                }
            }

            Value::Bool(value) => Err(de::Error::custom(format!(
                "invalid type: boolean `{value}`, expected a sequence, a map or a string"
            ))),

            Value::U8(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::U16(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::U32(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::U64(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),

            Value::I8(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::I16(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::I32(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),
            Value::I64(value) => Err(de::Error::custom(format!(
                "invalid type: integer `{value}`, expected a sequence, a map or a string"
            ))),

            Value::F32(value) => Err(de::Error::custom(format!(
                "invalid type: float `{value}`, expected a sequence, a map or a string"
            ))),
            Value::F64(value) => Err(de::Error::custom(format!(
                "invalid type: float `{value}`, expected a sequence, a map or a string"
            ))),

            Value::Char(value) => Err(de::Error::custom(format!(
                "invalid type: char `{value}`, expected a sequence, a map or a string"
            ))),

            Value::Unit => Err(de::Error::custom(
                "invalid type: unit value, expected a sequence, a map or a string",
            )),
            Value::Option(value) => Err(de::Error::custom(format!(
                "invalid type: option `{value:?}`, expected a sequence, a map or a string"
            ))),
            Value::Newtype(value) => Err(de::Error::custom(format!(
                "invalid type: newtype `{value:?}`, expected a sequence, a map or a string"
            ))),
            Value::Bytes(value) => Err(de::Error::custom(format!(
                "invalid type: bytes `{value:?}`, expected a sequence, a map or a string"
            ))),
        }
    }
}

impl GeneratorTree {
    pub fn generate(&self) -> Result<SecretTree, GenerationError> {
        match self {
            GeneratorTree::List(generator_list) => Ok(SecretTree::List(
                generator_list
                    .iter()
                    .map(|generator| generator.generate())
                    .collect::<Result<Vec<_>, _>>()?,
            )),
            GeneratorTree::Map(generator_map) => Ok(SecretTree::Map(
                generator_map
                    .iter()
                    .map(|(key, generator)| {
                        Ok::<(String, SecretTree), GenerationError>((
                            key.clone(),
                            generator.generate()?,
                        ))
                    })
                    .collect::<Result<BTreeMap<_, _>, _>>()?,
            )),
            GeneratorTree::Generator(generator) => Ok(SecretTree::Secret(generator.generate()?)),
        }
    }
}