You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.0 KiB

use super::*;
use std::hash::{Hash, Hasher};
use std::borrow::Borrow;
pub mod builder;
#[derive(Debug, Serialize, Deserialize)] // Clone, PartialEq, Eq, Hash
pub struct Entry
{
name: String,
description: String,
/// The key used to encrypt the file, if any.
key: Option<aes::AesKey>,
/// The hash of the *unencrypted* data.
hash: sha256::Sha256Hash,
/// The tags for this entry.
///
/// # Note
/// This should be updated the same as the `Store`'s `tags` and `tag_mappings`. The duplication here is for lookup convenience / caching.
pub(super) tags: Vec<String>,
/// The *original* filename of the item, *not* the path to the real file for this item (see `location`).
filename: OsString,
/// Filename *relative* to the root of the store
location: PathBuf,
/// The state of caching this entry is currently in. This should not be saved as it has no meaning regarding the actual data itself.
#[serde(skip)]
cache: DataCacheState,
}
// Accessors
impl Entry
{
/// The name of this entry
pub fn name(&self) -> &str
{
&self.name
}
pub fn description(&self) -> &str
{
&self.description
}
/// The *original* filename of this entry
pub fn filename(&self) -> &Path
{
Path::new(&self.filename)
}
/// The path of this entry's file relative to the root of its store
pub fn location(&self) -> &Path
{
self.location.as_path()
}
/// The tags for this entry
pub fn tags(&self) -> &[String]
{
&self.tags[..]
}
/// Is the file of this entry encrypted?
pub fn is_encrypted(&self) -> bool
{
self.key.is_some()
}
/// The sha256 hash of the data in this entry
pub fn hash(&self) -> &sha256::Sha256Hash
{
&self.hash
}
}
impl Entry
{
/// Consume and drop the cache. Only that useful as shim for mapping.
#[inline(always)] pub(super) fn with_no_cache(self) -> Self
{
Self {
cache: Default::default(),
..self
}
}
/// Clone the data needed for a refresh of this entry in a store before it is mutated.
#[inline(always)] pub(super) fn prepare_for_refresh(&self) -> (EntryKey, Box<[String]>)
{
(self.hash().clone(), self.tags.clone().into_boxed_slice())
}
}
impl Borrow<sha256::Sha256Hash> for Entry
{
#[inline] fn borrow(&self) -> &sha256::Sha256Hash
{
&self.hash
}
}
impl Hash for Entry {
fn hash<H: Hasher>(&self, state: &mut H) {
self.hash.hash(state)
}
}
impl PartialEq for Entry
{
fn eq(&self, other: &Self) -> bool
{
macro_rules! eq {
($one:ident) => (self.$one == other.$one);
($($many:ident),*) => {
$(
eq!($many)
)&& *
};
}
eq!(name, description, key, tags, filename, location)
}
}
impl Eq for Entry{}
impl Clone for Entry
{
fn clone(&self) -> Self {
macro_rules! clone {
($($many:ident),*) => {
Self {
cache: Default::default(),
$($many: self.$many.clone()),*
}
}
}
clone!(name, description, key, tags, filename, location, hash)
}
}