noxu_bind/entry_binding.rs
1//! Core binding traits for converting between database entries and Rust types.
2//!
3
4use noxu_db::DatabaseEntry;
5
6use crate::error::Result;
7
8/// Converts between a `DatabaseEntry` and a Rust type.
9///
10/// This is the fundamental binding trait, analogous to `EntryBinding<T>`.
11/// Implementations define how to serialize an object into a database entry
12/// and how to deserialize it back.
13///
14///
15pub trait EntryBinding<T> {
16 /// Converts a `DatabaseEntry` to an object.
17 ///
18 /// # Errors
19 ///
20 /// Returns `BindError` if the entry data cannot be deserialized.
21 fn entry_to_object(&self, entry: &DatabaseEntry) -> Result<T>;
22
23 /// Converts an object to a `DatabaseEntry`.
24 ///
25 /// # Errors
26 ///
27 /// Returns `BindError` if the object cannot be serialized.
28 fn object_to_entry(
29 &self,
30 object: &T,
31 entry: &mut DatabaseEntry,
32 ) -> Result<()>;
33}
34
35/// Converts between key+data entries and an entity object.
36///
37/// This trait is used for entity bindings where the key and data are stored
38/// separately but represent a single logical entity.
39///
40///
41pub trait EntityBinding<E> {
42 /// Converts key and data entries to an entity object.
43 ///
44 /// # Errors
45 ///
46 /// Returns `BindError` if the entries cannot be deserialized.
47 fn entry_to_object(
48 &self,
49 key: &DatabaseEntry,
50 data: &DatabaseEntry,
51 ) -> Result<E>;
52
53 /// Extracts the key from an entity object and writes it to a `DatabaseEntry`.
54 ///
55 /// # Errors
56 ///
57 /// Returns `BindError` if the key cannot be serialized.
58 fn object_to_key(&self, object: &E, key: &mut DatabaseEntry) -> Result<()>;
59
60 /// Extracts the data from an entity object and writes it to a `DatabaseEntry`.
61 ///
62 /// # Errors
63 ///
64 /// Returns `BindError` if the data cannot be serialized.
65 fn object_to_data(
66 &self,
67 object: &E,
68 data: &mut DatabaseEntry,
69 ) -> Result<()>;
70}