ps_datachunk/
lib.rs

1#![allow(clippy::missing_errors_doc)]
2#![allow(clippy::module_name_repetitions)]
3pub mod aligned;
4pub mod borrowed;
5pub mod encrypted;
6pub mod error;
7pub mod mbuf;
8pub mod owned;
9pub mod serialized;
10pub mod shared;
11pub mod typed;
12pub mod utils;
13pub use aligned::AlignedDataChunk;
14pub use borrowed::BorrowedDataChunk;
15pub use bytes::Bytes;
16pub use encrypted::EncryptedDataChunk;
17pub use error::PsDataChunkError;
18pub use error::Result;
19pub use mbuf::MbufDataChunk;
20pub use owned::OwnedDataChunk;
21pub use ps_hash::Hash;
22pub use ps_mbuf::Mbuf;
23pub use serialized::SerializedDataChunk;
24pub use shared::SharedDataChunk;
25pub use typed::ToDataChunk;
26pub use typed::ToTypedDataChunk;
27pub use typed::TypedDataChunk;
28
29use std::sync::Arc;
30
31/// represents any representation of a chunk of data
32pub trait DataChunk
33where
34    Self: Sized,
35{
36    fn data_ref(&self) -> &[u8];
37    fn hash_ref(&self) -> &Hash;
38
39    fn hash(&self) -> Arc<Hash>;
40
41    fn encrypt(&self) -> Result<EncryptedDataChunk> {
42        self.serialize()?.encrypt()
43    }
44
45    fn decrypt(&self, key: &[u8]) -> Result<SerializedDataChunk> {
46        utils::decrypt(self.data_ref(), key)
47    }
48
49    fn borrow(&self) -> BorrowedDataChunk {
50        BorrowedDataChunk::from_parts(self.data_ref(), self.hash())
51    }
52
53    fn serialize(&self) -> Result<SerializedDataChunk> {
54        SerializedDataChunk::from_parts(self.data_ref(), self.hash())
55    }
56
57    /// Transforms this [`DataChunk`] into [`Bytes`].
58    fn into_bytes(self) -> Bytes {
59        Bytes::from_owner(Arc::from(self.data_ref()))
60    }
61
62    /// Copies this [`DataChunk`] into a new [`OwnedDataChunk`].
63    fn into_owned(self) -> OwnedDataChunk {
64        OwnedDataChunk::from_data_and_hash(Arc::from(self.data_ref()), self.hash())
65    }
66
67    fn try_as<T: rkyv::Archive>(self) -> Result<TypedDataChunk<Self, T>>
68    where
69        T::Archived:
70            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, rancor::Error>>,
71    {
72        TypedDataChunk::<Self, T>::from_data_chunk(self)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_encryption_decryption() -> Result<()> {
82        let original_data = "Neboť tak Bůh miluje svět, že dal [svého] jediného Syna, aby žádný, kdo v něho věří, nezahynul, ale měl život věčný. Vždyť Bůh neposlal [svého] Syna na svět, aby svět odsoudil, ale aby byl svět skrze něj zachráněn.".as_bytes().to_owned();
83
84        let data_chunk = BorrowedDataChunk::from_data(&original_data)?;
85
86        let encrypted_chunk = data_chunk.encrypt()?;
87        let decrypted_chunk = encrypted_chunk.decrypt()?;
88
89        assert_eq!(decrypted_chunk.data_ref(), original_data);
90
91        Ok(())
92    }
93
94    #[test]
95    fn test_serialization() -> Result<()> {
96        let original_data = vec![1, 2, 3, 4, 5];
97        let hash = ps_hash::hash(&original_data)?.into();
98        let data_chunk = OwnedDataChunk::from_data_and_hash(original_data.clone(), hash);
99
100        let serialized = data_chunk.serialize()?;
101
102        assert_eq!(serialized.data_ref(), original_data);
103
104        Ok(())
105    }
106}