Skip to main content

ValueCodec

Trait ValueCodec 

Source
pub trait ValueCodec: Send + Sync {
    // Required methods
    fn encode(&self, value: &[u8]) -> Vec<u8> ;
    fn decode(&self, stored: &[u8]) -> Result<Vec<u8>, NookError>;
}
Expand description

Storage value-codec injection seam (extension seam §6a). Implement ValueCodec in an external crate to transform stored values (e.g. at-rest encryption) without forking or modifying nookdb-core; IdentityCodec is the default pass-through (plain JSON on disk). Seam for transforming stored values at the storage read/write boundary.

Implement this in an external crate to inject an alternate codec (e.g. at-rest encryption) without forking or modifying nookdb-core. The default is IdentityCodec (pass-through → plain JSON on disk).

§Examples

use nookdb_core::{ValueCodec, IdentityCodec};

// The default codec is a pass-through.
let codec = IdentityCodec;
assert_eq!(codec.encode(b"hi"), b"hi");
assert_eq!(codec.decode(b"hi").unwrap(), b"hi");

// An external crate can implement the seam against the PUBLIC path.
struct Xor7;
impl ValueCodec for Xor7 {
    fn encode(&self, v: &[u8]) -> Vec<u8> {
        v.iter().map(|b| b ^ 7).collect()
    }
    fn decode(&self, v: &[u8]) -> Result<Vec<u8>, nookdb_core::NookError> {
        Ok(v.iter().map(|b| b ^ 7).collect())
    }
}
let x = Xor7;
assert_eq!(x.decode(&x.encode(b"abc")).unwrap(), b"abc");

Required Methods§

Source

fn encode(&self, value: &[u8]) -> Vec<u8>

Source

fn decode(&self, stored: &[u8]) -> Result<Vec<u8>, NookError>

§Errors

Returns NookError::Corruption if stored bytes cannot be decoded.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§