···
61
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError>;
61
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>>;
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError>
67
+
pub trait SubPrefixBytes<T> {
68
+
fn sub_prefix(input: T) -> EncodingResult<Vec<u8>>;
···
pub fn from_pair(prefix: P, suffix: S) -> Self {
79
-
pub fn from_prefix_to_db_bytes(prefix: &P) -> Result<Vec<u8>, EncodingError> {
83
+
pub fn from_prefix_to_db_bytes(prefix: &P) -> EncodingResult<Vec<u8>> {
82
-
pub fn to_prefix_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
86
+
pub fn to_prefix_db_bytes(&self) -> EncodingResult<Vec<u8>> {
self.prefix.to_db_bytes()
85
-
pub fn prefix_range_end(prefix: &P) -> Result<Vec<u8>, EncodingError> {
89
+
pub fn prefix_range_end(prefix: &P) -> EncodingResult<Vec<u8>> {
let prefix_bytes = prefix.to_db_bytes()?;
let (_, Bound::Excluded(range_end)) = prefix_to_range(&prefix_bytes) else {
return Err(EncodingError::BadRangeBound);
92
-
pub fn range_end(&self) -> Result<Vec<u8>, EncodingError> {
96
+
pub fn range_end(&self) -> EncodingResult<Vec<u8>> {
Self::prefix_range_end(&self.prefix)
pub fn range(&self) -> Result<Range<Vec<u8>>, EncodingError> {
···
111
+
impl<P: DbBytes + Default, S: DbBytes + Default> Default for DbConcat<P, S> {
112
+
fn default() -> Self {
114
+
prefix: Default::default(),
115
+
suffix: Default::default(),
impl<P: DbBytes + std::fmt::Debug, S: DbBytes + std::fmt::Debug> fmt::Debug for DbConcat<P, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DbConcat<{:?} || {:?}>", self.prefix, self.suffix)
···
impl<P: DbBytes, S: DbBytes> DbBytes for DbConcat<P, S> {
114
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
127
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
let mut combined = self.prefix.to_db_bytes()?;
combined.append(&mut self.suffix.to_db_bytes()?);
···
#[derive(Debug, Default, PartialEq)]
impl DbBytes for DbEmpty {
150
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
163
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
fn from_db_bytes(_: &[u8]) -> Result<(Self, usize), EncodingError> {
···
impl<S: StaticStr> DbBytes for DbStaticStr<S> {
179
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
192
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
S::static_str().to_string().to_db_bytes()
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
···
T: BincodeEncode + BincodeDecode<()> + UseBincodePlz + Sized + std::fmt::Debug,
206
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
219
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
Ok(encode_to_vec(self, bincode_conf())?)
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
···
/// helper trait: impl on a type to get helpers to implement DbBytes
pub trait SerdeBytes: serde::Serialize + for<'a> serde::Deserialize<'a> {
216
-
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError>
229
+
fn to_bytes(&self) -> EncodingResult<Vec<u8>>
···
impl<const N: usize> UseBincodePlz for [u8; N] {}
impl DbBytes for Vec<u8> {
232
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
245
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
···
/// TODO: wrap in another type. it's actually probably not desirable to serialize strings this way
/// *except* where needed as a prefix.
impl DbBytes for String {
250
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
263
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
let mut v = self.as_bytes().to_vec();
return Err(EncodingError::StringContainedNull);
···
Err(EncodingError::UnterminatedString)
283
+
impl SubPrefixBytes<&str> for String {
284
+
fn sub_prefix(input: &str) -> EncodingResult<Vec<u8>> {
285
+
let v = input.as_bytes();
286
+
if v.contains(&0x00) {
287
+
return Err(EncodingError::StringContainedNull);
289
+
// NO null terminator!!
···
let me = Self::new(s).map_err(EncodingError::BadAtriumStringType)?;
276
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
300
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
Ok(encode_to_vec(self.as_ref(), bincode_conf())?)
281
-
// BUG: this needs to use the null-terminating string thing!!!!!!!!!!!!!! the whole point of all of this!!!!
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
284
-
let (s, n) = decode_from_slice(bytes, bincode_conf())?;
307
+
let (s, n) = String::from_db_bytes(bytes)?; // null-terminated DbBytes impl!!
let me = Self::new(s).map_err(EncodingError::BadAtriumStringType)?;
288
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
289
-
Ok(encode_to_vec(self.as_ref(), bincode_conf())?)
311
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
312
+
String::to_db_bytes(&self.to_string()) // null-terminated DbBytes impl!!!!
315
+
impl SubPrefixBytes<&str> for Nsid {
316
+
fn sub_prefix(input: &str) -> EncodingResult<Vec<u8>> {
317
+
String::sub_prefix(input)
···
let me = Self::new(s).map_err(EncodingError::BadAtriumStringType)?;
299
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
327
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
Ok(encode_to_vec(self.as_ref(), bincode_conf())?)
impl DbBytes for Cursor {
305
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
333
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
Ok(self.to_raw_u64().to_be_bytes().to_vec())
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
···
impl DbBytes for serde_json::Value {
319
-
fn to_db_bytes(&self) -> Result<Vec<u8>, EncodingError> {
347
+
fn to_db_bytes(&self) -> EncodingResult<Vec<u8>> {
self.to_string().to_db_bytes()
fn from_db_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
···
339
-
use super::{Cursor, DbBytes, DbConcat, DbEmpty, DbStaticStr, EncodingError, StaticStr};
368
+
Cursor, DbBytes, DbConcat, DbEmpty, DbStaticStr, EncodingResult, Nsid, StaticStr,
342
-
fn test_db_empty() -> Result<(), EncodingError> {
373
+
fn test_db_empty() -> EncodingResult<()> {
let original = DbEmpty::default();
let serialized = original.to_db_bytes()?;
assert_eq!(serialized.len(), 0);
···
353
-
fn test_string_roundtrip() -> Result<(), EncodingError> {
384
+
fn test_string_roundtrip() -> EncodingResult<()> {
···
372
-
fn test_string_serialized_lexicographic_sort() -> Result<(), EncodingError> {
403
+
fn test_string_serialized_lexicographic_sort() -> EncodingResult<()> {
let aa = "aa".to_string().to_db_bytes()?;
let b = "b".to_string().to_db_bytes()?;
···
380
-
fn test_string_cursor_prefix_roundtrip() -> Result<(), EncodingError> {
411
+
fn test_nullstring_can_prefix() -> EncodingResult<()> {
412
+
for (s, pre, is_pre, desc) in [
413
+
("", "", true, "empty strings"),
414
+
("", "a", false, "longer prefix"),
415
+
("a", "", true, "empty prefix matches"),
416
+
("a", "a", true, "whole string matches"),
417
+
("a", "b", false, "entirely different"),
418
+
("ab", "a", true, "prefix matches"),
419
+
("ab", "b", false, "shorter and entirely different"),
421
+
let serialized = s.to_string().to_db_bytes()?;
422
+
let prefixed = String::sub_prefix(pre)?;
423
+
assert_eq!(serialized.starts_with(&prefixed), is_pre, "{}", desc);
429
+
fn test_nsid_can_prefix() -> EncodingResult<()> {
430
+
for (s, pre, is_pre, desc) in [
431
+
("ab.cd.ef", "", true, "empty prefix"),
432
+
("ab.cd.ef", "a", true, "tiny prefix"),
433
+
("ab.cd.ef", "abc", false, "bad prefix"),
434
+
("ab.cd.ef", "ab", true, "segment prefix"),
435
+
("ab.cd.ef", "ab.cd", true, "multi-segment prefix"),
436
+
("ab.cd.ef", "ab.cd.ef", true, "full match"),
437
+
("ab.cd.ef", "ab.cd.ef.g", false, "prefix longer"),
439
+
let serialized = Nsid::new(s.to_string()).unwrap().to_db_bytes()?;
440
+
let prefixed = Nsid::sub_prefix(pre)?;
441
+
assert_eq!(serialized.starts_with(&prefixed), is_pre, "{}", desc);
447
+
fn test_string_cursor_prefix_roundtrip() -> EncodingResult<()> {
type TwoThings = DbConcat<String, Cursor>;
for (lazy_prefix, tired_suffix, desc) in [
("", 0, "empty string and cursor"),
···
405
-
fn test_cursor_string_prefix_roundtrip() -> Result<(), EncodingError> {
472
+
fn test_cursor_string_prefix_roundtrip() -> EncodingResult<()> {
type TwoThings = DbConcat<Cursor, String>;
for (tired_prefix, sad_suffix, desc) in [
(0, "", "empty string and cursor"),
···
430
-
fn test_static_str() -> Result<(), EncodingError> {
497
+
fn test_static_str() -> EncodingResult<()> {
#[derive(Debug, PartialEq)]
impl StaticStr for AStaticStr {
···
451
-
fn test_static_str_empty() -> Result<(), EncodingError> {
518
+
fn test_static_str_empty() -> EncodingResult<()> {
#[derive(Debug, PartialEq)]
impl StaticStr for AnEmptyStr {
···
471
-
fn test_static_prefix() -> Result<(), EncodingError> {
538
+
fn test_static_prefix() -> EncodingResult<()> {
#[derive(Debug, PartialEq)]
impl StaticStr for AStaticPrefix {