1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
//! # A Key Store Trait //! //! This module defines the [`KeyStore`] trait as well as a [`KeyStoreValue`] trait. //! The key store defines a simple CRUD API with IDs of type [`KeyStore::KeyStoreId`] //! and stores values that implement the (de)serialization define by the //! [`KeyStoreValue`] trait. use std::fmt::Debug; use crate::types::Status; /// The Key Store trait pub trait KeyStore: Send + Sync { /// The type of the identifier used by the key store to identify values in /// the key store. type KeyStoreId; /// The error type returned by the [`KeyStore`]. type Error: Debug + Clone + PartialEq + Into<String>; /// Store a value `v` that implements the [`KeyStoreValue`] trait for /// serialization with [`Status`] `s` under ID `k`. /// /// Returns an error if storing fails. fn store_with_status( &self, k: &Self::KeyStoreId, v: &impl KeyStoreValue, s: Status, ) -> Result<(), Self::Error> where Self: Sized; /// Store a value `v` that implements the [`KeyStoreValue`] trait for /// serialization for ID `k`. /// The status will always be [`Status::Extractable`]. /// To set the status of the value `v` use /// [`store_with_status`](`KeyStore::store_with_status`). /// /// Returns an error if storing fails. fn store(&self, k: &Self::KeyStoreId, v: &impl KeyStoreValue) -> Result<(), Self::Error> where Self: Sized; /// Read and return a value stored for ID `k` that implements the /// [`KeyStoreValue`] trait for deserialization. /// If the value is marked as `Status::Hidden`, an error will be returned. /// /// Returns an error if storing fails. fn read<V: KeyStoreValue>(&self, k: &Self::KeyStoreId) -> Result<V, Self::Error> where Self: Sized; /// Update a value stored for ID `k` with a new value `v` that implements the /// [`KeyStoreValue`] trait for serialization. /// /// Returns an error if storing fails. fn update(&self, k: &Self::KeyStoreId, v: &impl KeyStoreValue) -> Result<(), Self::Error> where Self: Sized; /// Delete a value stored for ID `k`. /// /// Returns an error if storing fails. fn delete(&self, k: &Self::KeyStoreId) -> Result<(), Self::Error> where Self: Sized; } /// Any value that is stored in the key store must implement this trait. /// In most cases these are the raw bytes of the object. /// /// The generic `Serialization` defaults to `Vec<u8>` but can be defined /// differently. The `serialize` return type `SerializedValue` must implement /// `Into<Serialization>`. pub trait KeyStoreValue<Serialization = Vec<u8>> { /// The error type returned by the [`KeyStoreValue`]. type Error: Debug + Clone + PartialEq + Into<String>; /// The type of a serialized key store value. type SerializedValue: Into<Serialization>; /// Serialize the value and return it as [`SerializedValue`](`KeyStoreValue::SerializedValue`). /// /// Returns an [`Error`](`KeyStoreValue::Error`) if the serialization fails. fn serialize(&self) -> Result<Self::SerializedValue, Self::Error>; /// Deserialize the byte slice and return the object. /// /// Returns an [`Error`](`KeyStoreValue::Error`) if the deserialization fails. fn deserialize(raw: &mut [u8]) -> Result<Self, Self::Error> where Self: Sized; }