reifydb_core/util/encoding/format/raw.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
11
12use crate::util::encoding::format::Formatter;
13
14/// Formats raw byte slices without any decoding.
15pub struct Raw;
16
17impl Raw {
18 /// Formats raw bytes as escaped ASCII strings.
19 pub fn bytes(bytes: &[u8]) -> String {
20 let escaped = bytes.iter().copied().flat_map(std::ascii::escape_default).collect::<Vec<_>>();
21 format!("\"{}\"", String::from_utf8_lossy(&escaped))
22 }
23}
24
25impl Formatter for Raw {
26 fn key(key: &[u8]) -> String {
27 Self::bytes(key)
28 }
29
30 fn value(_key: &[u8], value: &[u8]) -> String {
31 Self::bytes(value)
32 }
33}