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