Skip to main content

noxu_bind/
byte_array_binding.rs

1//! Raw byte array binding.
2//!
3
4use noxu_db::DatabaseEntry;
5
6use crate::entry_binding::EntryBinding;
7use crate::error::Result;
8
9/// A simple pass-through binding for raw byte arrays (`Vec<u8>`).
10///
11/// This binding stores and retrieves byte arrays without any transformation.
12///
13///
14#[derive(Debug, Clone, Copy, Default)]
15pub struct ByteArrayBinding;
16
17impl ByteArrayBinding {
18    /// Creates a new `ByteArrayBinding`.
19    pub fn new() -> Self {
20        Self
21    }
22}
23
24impl EntryBinding<Vec<u8>> for ByteArrayBinding {
25    fn entry_to_object(&self, entry: &DatabaseEntry) -> Result<Vec<u8>> {
26        Ok(entry.data().to_vec())
27    }
28
29    fn object_to_entry(
30        &self,
31        object: &Vec<u8>,
32        entry: &mut DatabaseEntry,
33    ) -> Result<()> {
34        entry.set_data(object);
35        Ok(())
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_round_trip() {
45        let binding = ByteArrayBinding::new();
46        let data = vec![1, 2, 3, 4, 5];
47        let mut entry = DatabaseEntry::new();
48        binding.object_to_entry(&data, &mut entry).unwrap();
49        let result = binding.entry_to_object(&entry).unwrap();
50        assert_eq!(data, result);
51    }
52
53    #[test]
54    fn test_empty() {
55        let binding = ByteArrayBinding::new();
56        let data = vec![];
57        let mut entry = DatabaseEntry::new();
58        binding.object_to_entry(&data, &mut entry).unwrap();
59        let result = binding.entry_to_object(&entry).unwrap();
60        assert_eq!(data, result);
61    }
62}