oth_rvault 0.2.0

Partial Ansible Vault encoder and decoder
Documentation
use crate::types::{TypedValue, ValueType};

const VAULT_PREFIX: &str = "$ANSIBLE_VAULT;";

/// Check if a value is already Ansible Vault encrypted.
/// Returns (is_encrypted, vault_string_from_prefix).
pub fn is_encrypted(value: &TypedValue) -> (bool, Option<String>) {
    let TypedValue::String(s) = value else {
        return (false, None);
    };
    match s.find(VAULT_PREFIX) {
        Some(idx) => (true, Some(s[idx..].to_string())),
        None => (false, None),
    }
}

/// Returns true if the value is of a type that can be encrypted (not Object, Array, or Null).
pub fn is_encryptable(vt: ValueType) -> bool {
    matches!(
        vt,
        ValueType::String | ValueType::Integer | ValueType::Bool | ValueType::Number
    )
}