Skip to main content

reifydb_core/util/encoding/format/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4// This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
5// originally licensed under the Apache License, Version 2.0.
6// Original copyright:
7//   Copyright (c) 2024 Erik Grinaker
8//
9// The original Apache License can be found at:
10//   http://www.apache.org/licenses/LICENSE-2.0
11
12//! Pluggable formatters for human-readable key and value rendering.
13//!
14//! The `Formatter` trait takes a raw key byte slice and an optional value byte slice and returns a printable string;
15//! the `raw` submodule provides the default hex rendering used by tools and tests. Implementors typically dispatch on
16//! the leading `KeyKind` byte to produce a structured rendering of catalog keys.
17pub mod raw;
18
19pub trait Formatter {
20	fn key(key: &[u8]) -> String;
21
22	fn value(key: &[u8], value: &[u8]) -> String;
23
24	fn key_value(key: &[u8], row: impl AsRef<[u8]>) -> String {
25		Self::key_maybe_value(key, Some(row))
26	}
27
28	fn key_maybe_value(key: &[u8], value: Option<impl AsRef<[u8]>>) -> String {
29		let fmtkey = Self::key(key);
30		let fmtvalue = value.map_or("None".to_string(), |v| Self::value(key, v.as_ref()));
31		format!("{fmtkey} => {fmtvalue}")
32	}
33}