Skip to main content

reifydb_core/util/encoding/format/
mod.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
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
11pub mod raw;
12
13/// Formats encoded keys and values.
14pub trait Formatter {
15	/// Formats a key.
16	fn key(key: &[u8]) -> String;
17
18	/// Formats a value. Also takes the key to determine the ty of value.
19	fn value(key: &[u8], value: &[u8]) -> String;
20
21	/// Formats a key/encoded pair.
22	fn key_value(key: &[u8], row: impl AsRef<[u8]>) -> String {
23		Self::key_maybe_value(key, Some(row))
24	}
25
26	/// Formats a key/encoded pair, where the value may not exist.
27	fn key_maybe_value(key: &[u8], value: Option<impl AsRef<[u8]>>) -> String {
28		let fmtkey = Self::key(key);
29		let fmtvalue = value.map_or("None".to_string(), |v| Self::value(key, v.as_ref()));
30		format!("{fmtkey} => {fmtvalue}")
31	}
32}