#![allow(unused)] use recordbox::traits::{ Record, RecordId, UniqueRecordId, FastUniqueRecordId }; use std::{ array::TryFromSliceError, ops::Deref, convert::{ TryFrom, Infallible } }; /// A key #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Key { /// The bytes inner: Vec } impl Key { /// Creates a key pub fn new(bytes: T) -> Self where T: AsRef<[u8]> { Self { inner: bytes.as_ref().into() } } /// Creates a key from a hex literal pub fn from_hex(hex: T) -> Self where T: AsRef<[u8]> { let inner = hex::decode(hex).expect("Invalid hex literal"); Self { inner } } /// The bytes as slice pub fn as_slice(&self) -> &[u8] { &self.inner } } impl From for Vec { fn from(record: Key) -> Self { record.inner } } /// A raw byte record #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteRecord { /// The bytes inner: Vec } impl ByteRecord { /// Creates a new byte record pub fn new(bytes: T) -> Self where T: AsRef<[u8]> { Self { inner: bytes.as_ref().into() } } /// Creates a byte record from a hex literal pub fn from_hex(hex: T) -> Self where T: AsRef<[u8]> { let inner = hex::decode(hex).expect("Invalid hex literal"); Self { inner } } /// The bytes as slice pub fn as_slice(&self) -> &[u8] { &self.inner } } impl Record for ByteRecord { type Error = Infallible; fn encode(&self) -> std::result::Result, Self::Error> { Ok(self.inner.clone()) } fn decode(encoded: D) -> std::result::Result where D: AsRef<[u8]> { Ok(Self::new(encoded.as_ref())) } } /// A raw byte record ID #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteRecordId { /// The bytes inner: Vec } impl ByteRecordId { /// Creates a new byte record ID pub fn new(bytes: T) -> Self where T: AsRef<[u8]> { Self { inner: bytes.as_ref().into() } } /// Creates a byte record ID from a hex literal pub fn from_hex(hex: T) -> Self where T: AsRef<[u8]> { let inner = hex::decode(hex).expect("Invalid hex literal"); Self { inner } } /// The bytes as slice pub fn as_slice(&self) -> &[u8] { &self.inner } } impl RecordId for ByteRecordId { type Error = Infallible; fn encode(&self) -> std::result::Result, Self::Error> { Ok(self.inner.clone()) } } impl UniqueRecordId for ByteRecordId { /* No members to implement */ } impl FastUniqueRecordId for ByteRecordId { type Error = TryFromSliceError; fn encode_fast(&self) -> std::result::Result<[u8; LEN], ::Error> { <[u8; LEN]>::try_from(self.inner.as_slice()) } } /// A ciphertext #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Ciphertext { /// The bytes inner: Vec } impl Ciphertext { /// Creates a ciphertext pub fn new(bytes: T) -> Self where T: AsRef<[u8]> { Self { inner: bytes.as_ref().into() } } /// Creates a ciphertext from a hex literal pub fn from_hex(hex: T) -> Self where T: AsRef<[u8]> { let inner = hex::decode(hex).expect("Invalid hex literal"); Self { inner } } /// The bytes as slice pub fn as_slice(&self) -> &[u8] { &self.inner } }