cipherstash-client 0.34.1-alpha.1

The official CipherStash SDK
Documentation
//! Custom serialization formats for EQL types.

/// MessagePack Base85 encoding/decoding for `EncryptedRecord`.
///
/// This module provides custom serde serialization that encodes encrypted records
/// as MessagePack Base85 strings for efficient and compact representation.
pub(super) mod mp_base85 {
    use super::super::*;
    use serde::Deserialize;

    /// Serializes an `EncryptedRecord` to MessagePack Base85 format.
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the record cannot be encoded to MessagePack Base85.
    pub fn serialize<S>(
        ciphertext: &Option<EncryptedRecord>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        // encrypted_record::formats::mp_base85
        match ciphertext {
            Some(record) => {
                let s = record.to_mp_base85().map_err(serde::ser::Error::custom)?;
                serializer.serialize_some(&s)
            }

            None => serializer.serialize_none(),
        }
    }

    /// Deserializes an `EncryptedRecord` from MessagePack Base85 format.
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the string cannot be decoded from MessagePack Base85.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<EncryptedRecord>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s: Option<String> = Option::deserialize(deserializer)?;
        if let Some(s) = s {
            Ok(Some(
                EncryptedRecord::from_mp_base85(&s).map_err(serde::de::Error::custom)?,
            ))
        } else {
            Ok(None)
        }
    }
}

/// Formats a binary SEM term (e.g., HMAC-SHA256 hash) as a hexadecimal string.
///
/// This function is intentionally kept separate from `format_sem_term_ore_bytea`
/// for semantic clarity, even though both implementations are identical.
/// This separation allows future divergence if different formatting is needed
/// for different SEM term types.
///
/// # Arguments
///
/// * `bytes` - The binary data to format (typically an HMAC hash)
///
/// # Returns
///
/// A hexadecimal string representation of the binary data.
pub(super) fn format_sem_term_binary(bytes: &Vec<u8>) -> String {
    hex::encode(bytes)
}

/// Formats an ORE (Order Revealing Encryption) SEM term as a hexadecimal string.
///
/// This function is intentionally kept separate from `format_sem_term_binary`
/// for semantic clarity, even though both implementations are identical.
/// This separation allows future divergence if different formatting is needed
/// for different SEM term types.
///
/// # Arguments
///
/// * `bytes` - The ORE bytea data to format
///
/// # Returns
///
/// A hexadecimal string representation of the ORE data.
pub(super) fn format_sem_term_ore_bytea(bytes: &Vec<u8>) -> String {
    hex::encode(bytes)
}

/// Formats an array of ORE SEM terms into hexadecimal strings.
///
/// Converts a slice of byte vectors (representing ORE SEM blocks) into a vector of
/// hexadecimal string representations.
///
/// # Arguments
///
/// * `vec_of_bytes` - A slice of byte vectors containing ORE SEM blocks
///
/// # Returns
///
/// A vector of hexadecimal strings, one for each ORE block in the input.
pub(super) fn format_sem_term_ore_array(vec_of_bytes: &[Vec<u8>]) -> Vec<String> {
    vec_of_bytes.iter().map(format_sem_term_ore_bytea).collect()
}

/// Formats a single ORE index term as a single-element vector of hexadecimal string.
///
/// This is a convenience wrapper around `format_sem_term_ore_bytea` that returns
/// the result in a single-element vector, matching the expected format for ORE indexes.
///
/// # Arguments
///
/// * `bytes` - The ORE index bytes to format
///
/// # Returns
///
/// A single-element vector containing the hexadecimal string representation.
pub(super) fn format_sem_term_ore(bytes: &Vec<u8>) -> Vec<String> {
    vec![format_sem_term_ore_bytea(bytes)]
}