Skip to content

Commit 157e086

Browse files
authored
utility: add support for encoding string slice (#4570)
* add support for encoding string slice * add helpeful conversion from recordkey * clippy fix
1 parent 7ee0c1b commit 157e086

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

crates/fluvio-protocol/src/core/encoder.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,42 @@ where
463463
}
464464
}
465465

466+
impl Encoder for &str {
467+
fn write_size(&self, _version: Version) -> usize {
468+
2 + self.len()
469+
}
470+
471+
fn encode<T>(&self, dest: &mut T, _version: Version) -> Result<(), Error>
472+
where
473+
T: BufMut,
474+
{
475+
if dest.remaining_mut() < 2 + self.len() {
476+
return Err(Error::new(
477+
ErrorKind::UnexpectedEof,
478+
"not enough capacity for string",
479+
));
480+
}
481+
482+
dest.put_u16(self.len() as u16);
483+
484+
let mut writer = dest.writer();
485+
let bytes_written = writer.write(<str>::as_bytes(self))?;
486+
487+
if bytes_written != self.len() {
488+
return Err(Error::new(
489+
ErrorKind::UnexpectedEof,
490+
format!(
491+
"out of {} bytes, {} not written",
492+
self.len(),
493+
self.len() - bytes_written
494+
),
495+
));
496+
}
497+
498+
Ok(())
499+
}
500+
}
501+
466502
#[cfg(test)]
467503
mod test {
468504

crates/fluvio-protocol/src/record/data.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ impl RecordKey {
8080
}
8181
}
8282

83+
impl From<RecordData> for RecordKey {
84+
fn from(k: RecordData) -> Self {
85+
Self(RecordKeyInner::Key(k))
86+
}
87+
}
88+
89+
impl From<RecordKey> for Option<RecordData> {
90+
fn from(k: RecordKey) -> Self {
91+
match k.0 {
92+
RecordKeyInner::Key(data) => Some(data),
93+
RecordKeyInner::Null => None,
94+
}
95+
}
96+
}
97+
8398
#[derive(Hash)]
8499
enum RecordKeyInner {
85100
Null,
@@ -808,4 +823,15 @@ mod test {
808823
};
809824
assert_eq!(record.timestamp(), 1_000_000_800);
810825
}
826+
827+
#[test]
828+
fn test_key_conversion() {
829+
let null_key = RecordKey::NULL;
830+
let data: Option<RecordData> = null_key.into();
831+
assert_eq!(data, None);
832+
let data = RecordData::from("test");
833+
let key = RecordKey::from(data.clone());
834+
let data2: Option<RecordData> = key.into();
835+
assert_eq!(data2, Some(data));
836+
}
811837
}

crates/fluvio-protocol/tests/str.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::io::Cursor;
2+
3+
use fluvio_protocol::{Decoder, Encoder};
4+
5+
#[test]
6+
fn test_encode_str() {
7+
let value = "hello";
8+
assert_eq!(value.write_size(0), 7); // 7 bytes for the string "hello"
9+
let mut dest = Vec::new();
10+
value.encode(&mut dest, 0).expect("Failed to encode string");
11+
12+
// decode the string back to its original form
13+
let mut buf = Cursor::new(dest);
14+
let decoded = String::decode_from(&mut buf, 0).expect("Failed to decode string");
15+
assert_eq!(decoded, "hello");
16+
}

0 commit comments

Comments
 (0)