helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The schema model: `Schema` of `pkg/schema/schema.go` in helm-schema, and how it is written
//! as JSON (`Schema.MarshalJSON`).

use std::collections::BTreeMap;

use serde_json::Value;

/// The type of a keyword whose value is a single scalar.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Simple {
    Str,
    Float,
    Int,
    Bool,
}

/// The keywords `Schema` stores as plain scalars, with the Go type of the field.
pub(super) const SIMPLE: [(&str, Simple); 21] = [
    ("$schema", Simple::Str),
    ("$id", Simple::Str),
    ("$comment", Simple::Str),
    ("format", Simple::Str),
    ("contentEncoding", Simple::Str),
    ("contentMediaType", Simple::Str),
    ("pattern", Simple::Str),
    ("minimum", Simple::Float),
    ("multipleOf", Simple::Float),
    ("exclusiveMaximum", Simple::Float),
    ("exclusiveMinimum", Simple::Float),
    ("maximum", Simple::Float),
    ("minLength", Simple::Int),
    ("maxLength", Simple::Int),
    ("minItems", Simple::Int),
    ("maxItems", Simple::Int),
    ("minProperties", Simple::Int),
    ("maxProperties", Simple::Int),
    ("deprecated", Simple::Bool),
    ("readOnly", Simple::Bool),
    ("writeOnly", Simple::Bool),
];

/// `BoolOrArrayOfString`: the `required` keyword. `flag` marks the property as required in its
/// parent and is never written.
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct Required {
    pub(super) strings: Vec<String>,
    pub(super) flag: bool,
}

/// One schema node. Maps are `Option` because Go distinguishes a nil map from an empty one.
#[derive(Debug, Clone, Default)]
pub(super) struct Schema {
    pub(super) types: Vec<String>,
    pub(super) required: Required,
    pub(super) properties: Option<BTreeMap<String, Schema>>,
    pub(super) pattern_properties: Option<BTreeMap<String, Schema>>,
    pub(super) definitions: Option<BTreeMap<String, Schema>>,
    pub(super) items: Option<Box<Schema>>,
    pub(super) any_of: Vec<Schema>,
    pub(super) all_of: Vec<Schema>,
    pub(super) one_of: Vec<Schema>,
    pub(super) not: Option<Box<Schema>>,
    pub(super) if_: Option<Box<Schema>>,
    pub(super) then: Option<Box<Schema>>,
    pub(super) else_: Option<Box<Schema>>,
    pub(super) contains: Option<Box<Schema>>,
    pub(super) property_names: Option<Box<Schema>>,
    pub(super) additional_properties: Option<Value>,
    pub(super) additional_items: Option<Value>,
    pub(super) dependencies: Option<BTreeMap<String, Value>>,
    pub(super) default: Option<Value>,
    pub(super) const_: Option<Value>,
    pub(super) const_was_set: bool,
    pub(super) const_from_value: bool,
    pub(super) reference: String,
    pub(super) title: String,
    pub(super) description: String,
    pub(super) examples: Vec<Value>,
    pub(super) enum_: Vec<Value>,
    pub(super) unique_items: bool,
    pub(super) simple: BTreeMap<&'static str, Value>,
    pub(super) custom: BTreeMap<String, Value>,
    pub(super) has_data: bool,
}

impl Schema {
    /// The string keyword `name` from [`SIMPLE`], empty when unset.
    pub(super) fn str_of(&self, name: &str) -> &str {
        self.simple
            .get(name)
            .and_then(Value::as_str)
            .unwrap_or_default()
    }

    /// The float keyword `name`, when set.
    pub(super) fn float_of(&self, name: &str) -> Option<f64> {
        self.simple.get(name).and_then(Value::as_f64)
    }

    /// The integer keyword `name`, when set.
    pub(super) fn int_of(&self, name: &str) -> Option<i64> {
        self.simple.get(name).and_then(Value::as_i64)
    }
}