alpha
Login
or
Join now
microcosm.blue
/
microcosm-rs
Star
0
Fork
3
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Constellation, Spacedust, Slingshot, UFOs: atproto crates and services for microcosm
Star
0
Fork
3
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
checkpoint: byte encoding helpers
author
phil
date
1 year ago
(Mar 12, 2025, 12:28 PM -0400)
commit
dec1a139
dec1a139fa9ce29cb6f6538ad56f7fc5353d0ce2
parent
ae808c7b
ae808c7b3018032e907635aefbd283b78b956845
+77
-43
1 changed file
Expand all
Collapse all
Unified
Split
ufos
src
store_types.rs
+77
-43
ufos/src/store_types.rs
Reviewed
···
1
1
-
use crate::Nsid; //, Cursor};
1
1
+
use crate::{Cursor, Nsid};
2
2
use bincode::{
3
3
config::{standard, Config},
4
4
-
decode_from_slice, encode_to_vec,
4
4
+
de::Decode as BincodeDecode,
5
5
+
decode_from_slice,
6
6
+
enc::Encode as BincodeEncode,
7
7
+
encode_to_vec,
5
8
error::{DecodeError, EncodeError},
6
9
};
7
10
use thiserror::Error;
8
11
9
12
#[derive(Error, Debug)]
10
10
-
pub enum BlahError {
11
11
-
#[error("could not convert from utf8: {0}")]
12
12
-
NotUtf8(#[from] std::str::Utf8Error),
13
13
+
pub enum EncodingError {
13
14
#[error("failed to parse NSID: {0}")]
14
15
BadNSID(&'static str),
15
15
-
#[error("failed to encode: {0}")]
16
16
-
EncodeFailed(#[from] EncodeError),
17
17
-
#[error("failed to decode: {0}")]
18
18
-
DecodeFailed(#[from] DecodeError),
16
16
+
#[error("failed to bincode-encode: {0}")]
17
17
+
BincodeEncodeFailed(#[from] EncodeError),
18
18
+
#[error("failed to bincode-decode: {0}")]
19
19
+
BincodeDecodeFailed(#[from] DecodeError),
19
20
#[error("decode missing suffix bytes")]
20
21
DecodeMissingSuffix,
22
22
+
#[error("decode ran out of bytes")]
23
23
+
DecodeNotEnoughBytes,
21
24
}
22
25
23
23
-
fn bconf() -> impl Config {
26
26
+
fn bincode_conf() -> impl Config {
24
27
standard().with_big_endian().with_fixed_int_encoding()
25
28
}
26
29
27
27
-
trait DbBytes {
28
28
-
fn to_bincoded(&self) -> Result<Vec<u8>, BlahError>;
29
29
-
fn from_bincoded(bytes: &[u8]) -> Result<(Self, usize), BlahError>
30
30
+
pub trait DbBytes {
31
31
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError>;
32
32
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError>
30
33
where
31
34
Self: Sized;
32
35
}
33
36
34
34
-
trait DbStringType: AsRef<str> {
35
35
-
fn from_string(s: String) -> Result<Self, BlahError>
37
37
+
pub trait DbStringType: AsRef<str> {
38
38
+
fn from_string(s: String) -> Result<Self, EncodingError>
36
39
where
37
40
Self: Sized;
38
41
}
39
42
40
40
-
impl<T: DbStringType> DbBytes for T {
41
41
-
fn to_bincoded(&self) -> Result<Vec<u8>, BlahError> {
42
42
-
Ok(encode_to_vec(self.as_ref(), bconf())?)
43
43
-
}
44
44
-
fn from_bincoded(bytes: &[u8]) -> Result<(Self, usize), BlahError>
45
45
-
where
46
46
-
Self: Sized,
47
47
-
{
48
48
-
let (s, n) = decode_from_slice(bytes, bconf())?;
49
49
-
let me = Self::from_string(s)?;
50
50
-
Ok((me, n))
43
43
+
impl DbBytes for String {
44
44
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError> {
45
45
+
Ok(encode_to_vec::<&Self, _>(self, bincode_conf())?)
51
46
}
52
52
-
}
53
53
-
54
54
-
impl DbStringType for Nsid {
55
55
-
fn from_string(s: String) -> Result<Self, BlahError> {
56
56
-
Self::new(s).map_err(BlahError::BadNSID)
47
47
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
48
48
+
Ok(decode_from_slice(bytes, bincode_conf())?)
57
49
}
58
50
}
59
51
60
60
-
struct DbKeyWithPrefix<P: DbBytes, S: DbBytes> {
52
52
+
pub struct DbKeyWithPrefix<P: DbBytes, S: DbBytes> {
61
53
prefix: P,
62
54
suffix: S,
63
55
}
64
56
65
57
impl<P: DbBytes, S: DbBytes> DbKeyWithPrefix<P, S> {
66
66
-
fn to_prefix_bincoded(&self) -> Result<Vec<u8>, BlahError> {
67
67
-
self.prefix.to_bincoded()
58
58
+
pub fn to_prefix_bincoded(&self) -> Result<Vec<u8>, EncodingError> {
59
59
+
self.prefix.to_bytes()
68
60
}
69
61
}
70
62
71
63
impl<P: DbBytes, S: DbBytes> DbBytes for DbKeyWithPrefix<P, S> {
72
72
-
fn to_bincoded(&self) -> Result<Vec<u8>, BlahError> {
73
73
-
let mut combined = self.prefix.to_bincoded()?;
74
74
-
combined.append(&mut self.suffix.to_bincoded()?);
64
64
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError> {
65
65
+
let mut combined = self.prefix.to_bytes()?;
66
66
+
combined.append(&mut self.suffix.to_bytes()?);
75
67
Ok(combined)
76
68
}
77
77
-
fn from_bincoded(bytes: &[u8]) -> Result<(Self, usize), BlahError>
69
69
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError>
78
70
where
79
71
Self: Sized,
80
72
{
81
81
-
let (prefix, eaten) = P::from_bincoded(bytes)?;
73
73
+
let (prefix, eaten) = P::from_bytes(bytes)?;
82
74
let Some(suffix_bytes) = bytes.get(eaten..) else {
83
83
-
return Err(BlahError::DecodeMissingSuffix);
75
75
+
return Err(EncodingError::DecodeMissingSuffix);
84
76
};
85
85
-
let (suffix, also_eaten) = S::from_bincoded(suffix_bytes)?;
77
77
+
let (suffix, also_eaten) = S::from_bytes(suffix_bytes)?;
86
78
Ok((Self { prefix, suffix }, eaten + also_eaten))
87
79
}
88
80
}
89
81
90
90
-
fn blah(nsd: Nsid) {
91
91
-
let _ = nsd.to_bincoded().unwrap();
82
82
+
trait Bincodeable: BincodeEncode + BincodeDecode<()> + Sized {}
83
83
+
84
84
+
impl<T> DbBytes for T
85
85
+
where
86
86
+
T: Bincodeable,
87
87
+
{
88
88
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError> {
89
89
+
Ok(encode_to_vec(self, bincode_conf())?)
90
90
+
}
91
91
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
92
92
+
Ok(decode_from_slice(bytes, bincode_conf())?)
93
93
+
}
94
94
+
}
95
95
+
96
96
+
//////
97
97
+
98
98
+
impl DbBytes for Nsid {
99
99
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
100
100
+
let (s, n) = decode_from_slice(bytes, bincode_conf())?;
101
101
+
let me = Self::new(s).map_err(EncodingError::BadNSID)?;
102
102
+
Ok((me, n))
103
103
+
}
104
104
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError> {
105
105
+
Ok(encode_to_vec(self.as_ref(), bincode_conf())?)
106
106
+
}
107
107
+
}
108
108
+
109
109
+
impl DbBytes for Cursor {
110
110
+
fn to_bytes(&self) -> Result<Vec<u8>, EncodingError> {
111
111
+
Ok(self.to_raw_u64().to_be_bytes().to_vec())
112
112
+
}
113
113
+
fn from_bytes(bytes: &[u8]) -> Result<(Self, usize), EncodingError> {
114
114
+
let Ok(bytes8) = TryInto::<[u8; 8]>::try_into(bytes) else {
115
115
+
return Err(EncodingError::DecodeNotEnoughBytes);
116
116
+
};
117
117
+
let cursor = Cursor::from_raw_u64(u64::from_be_bytes(bytes8));
118
118
+
Ok((cursor, 8))
119
119
+
}
120
120
+
}
121
121
+
122
122
+
///////
123
123
+
124
124
+
pub fn blah(nsd: Nsid) {
125
125
+
let _ = nsd.to_bytes().unwrap();
92
126
}