reifydb_core/util/encoding/format/mod.rs
1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
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 use raw::Raw;
12
13mod raw;
14
15/// Formats encoded keys and values.
16pub trait Formatter {
17 /// Formats a key.
18 fn key(key: &[u8]) -> String;
19
20 /// Formats a value. Also takes the key to determine the ty of value.
21 fn value(key: &[u8], value: &[u8]) -> String;
22
23 /// Formats a key/encoded pair.
24 fn key_value(key: &[u8], row: impl AsRef<[u8]>) -> String {
25 Self::key_maybe_value(key, Some(row))
26 }
27
28 /// Formats a key/encoded pair, where the value may not exist.
29 fn key_maybe_value(key: &[u8], value: Option<impl AsRef<[u8]>>) -> String {
30 let fmtkey = Self::key(key);
31 let fmtvalue = value.map_or("None".to_string(), |v| Self::value(key, v.as_ref()));
32 format!("{fmtkey} => {fmtvalue}")
33 }
34}