//! Data structures for the in-memory map. use super::*; use std::{ collections::HashMap, }; use generational_arena::{ Arena, Index, }; use bitflags::bitflags; use cryptohelpers::{ rsa::{ RsaPublicKey, RsaPrivateKey, Signature, }, aes::AesKey, sha256::Sha256Hash, }; /// An absolute (nested) index #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct AbsoluteIndex(Vec); /// Possible value types of the data map #[derive(Debug, Serialize, Deserialize)] pub enum Data { Byte(u8), Char(char), Bool(bool), /// Signed integer SI(i64), /// Unsigned integer UI(u64), /// Floating-point integer FI(f64), /// A UTF-8 text string Text(String), /// A binary blob Binary(Vec), /// A reference index to an item within the same `Datamap` as this one. RelativeRef(Index), /// A reference to an item N deep within nested `Map` elements. /// /// The first `Index` specifies the `Map` data item at the root `Datamap` that contains the next, et cetera. The pointed to value is the last index. AbsoluteRef(AbsoluteIndex), /// A list of atoms List(Vec), /// Another datamap Map(Datamap), /// An AES key AesKey(AesKey), /// An RSA keypair RsaKeypair(RsaPrivateKey, RsaPublicKey), /// An RSA private key RsaPrivate(RsaPrivateKey), /// An RSA public key RsaPublic(RsaPublicKey), /// A SHA256 hash Hash(Sha256Hash), /// A unique ID Uuid(uuid::Uuid), /// An RSA signature Signature(Signature), /// Nothing Null, } /// An entry that may or may not be encrypted #[derive(Debug, Serialize, Deserialize)] pub enum MaybeEncrypted { Encrypted(Vec), Unencrypted(Data), } bitflags! { /// And additional metadata for values #[derive(Serialize, Deserialize)] struct Tags: u16 { /// Default const NONE = 0; /// This value should be cloned on write unless specified elsewhere. const COW = 1<<0; /// This should not show up in searches const HIDDEN = 1<<1; } } /// Information about a map entry. #[derive(Debug, Serialize, Deserialize)] pub struct Info { alt_name: Option<(String, String)>, created: u64, modified: u64, enable_versioning: bool, tags: Tags, owner: Option>, //starts as the user that created (i.e. same as `created_by`), or `None` if ownership is disabled signed: Option>, perms: user::Permissions, created_by: user::UserID, log: Vec, } /// The root data containing map #[derive(Debug, Serialize, Deserialize)] pub struct Datamap { data: Arena, ident: HashMap, } /// A value in a datamap, contains the information about the value and the value itself. /// /// May also contain previous versions of this atom #[derive(Debug, Serialize, Deserialize)] pub struct Atom(MaybeEncrypted, Info, Vec); /// An identifier for an item in a `Datamap`, or an item nested within many `Datamap`s. #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct Identifier(String);