zvault/src/bundledb/mod.rs

132 lines
2.9 KiB
Rust
Raw Normal View History

2017-03-21 10:08:01 +00:00
mod writer;
2017-03-22 08:19:16 +00:00
mod reader;
2017-03-21 10:08:01 +00:00
mod db;
2017-03-22 08:19:16 +00:00
mod cache;
2017-04-10 06:53:55 +00:00
mod uploader;
2017-03-21 10:08:01 +00:00
2017-03-22 08:19:16 +00:00
pub use self::cache::{StoredBundle, BundleCacheError};
pub use self::writer::{BundleWriter, BundleWriterError};
pub use self::reader::{BundleReader, BundleReaderError};
2017-03-21 10:08:01 +00:00
pub use self::db::*;
2017-04-10 06:53:55 +00:00
pub use self::uploader::BundleUploader;
2017-03-21 10:08:01 +00:00
2017-03-22 08:19:16 +00:00
use ::prelude::*;
use std::fmt;
use serde;
2017-04-02 18:45:35 +00:00
use rand;
2017-03-22 08:19:16 +00:00
2017-03-21 10:28:11 +00:00
pub static HEADER_STRING: [u8; 7] = *b"zvault\x01";
pub static HEADER_VERSION: u8 = 1;
2017-03-22 08:19:16 +00:00
#[derive(Hash, PartialEq, Eq, Clone, Default, Ord, PartialOrd)]
2017-03-22 08:19:16 +00:00
pub struct BundleId(pub Hash);
impl Serialize for BundleId {
2017-04-10 18:35:28 +00:00
#[inline]
2017-03-22 08:19:16 +00:00
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
self.0.serialize(ser)
}
}
2017-04-27 11:35:48 +00:00
impl<'a> Deserialize<'a> for BundleId {
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-27 11:35:48 +00:00
fn deserialize<D: serde::Deserializer<'a>>(de: D) -> Result<Self, D::Error> {
2017-03-22 08:19:16 +00:00
let hash = try!(Hash::deserialize(de));
Ok(BundleId(hash))
}
}
impl BundleId {
#[inline]
2017-04-19 15:46:51 +00:00
pub fn to_string(&self) -> String {
2017-03-22 08:19:16 +00:00
self.0.to_string()
}
2017-04-02 18:45:35 +00:00
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-02 18:45:35 +00:00
pub fn random() -> Self {
BundleId(Hash{high: rand::random(), low: rand::random()})
}
2017-03-22 08:19:16 +00:00
}
impl fmt::Display for BundleId {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", self.to_string())
}
}
impl fmt::Debug for BundleId {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", self.to_string())
}
}
#[derive(Eq, Debug, PartialEq, Clone, Copy)]
pub enum BundleMode {
2017-04-03 12:05:16 +00:00
Data, Meta
2017-03-22 08:19:16 +00:00
}
serde_impl!(BundleMode(u8) {
2017-04-03 12:05:16 +00:00
Data => 0,
2017-03-22 08:19:16 +00:00
Meta => 1
});
2017-04-03 05:57:58 +00:00
#[derive(Default, Debug, Clone)]
pub struct BundleHeader {
pub encryption: Option<Encryption>,
pub info_size: usize
}
serde_impl!(BundleHeader(u8) {
encryption: Option<Encryption> => 0,
info_size: usize => 1
});
2017-03-22 08:19:16 +00:00
#[derive(Clone)]
pub struct BundleInfo {
pub id: BundleId,
pub mode: BundleMode,
pub compression: Option<Compression>,
pub encryption: Option<Encryption>,
pub hash_method: HashMethod,
pub raw_size: usize,
pub encoded_size: usize,
pub chunk_count: usize,
2017-04-14 20:44:40 +00:00
pub chunk_list_size: usize,
pub timestamp: i64
2017-03-22 08:19:16 +00:00
}
2017-04-02 16:55:53 +00:00
serde_impl!(BundleInfo(u64?) {
2017-03-22 08:19:16 +00:00
id: BundleId => 0,
mode: BundleMode => 1,
compression: Option<Compression> => 2,
2017-04-07 16:57:49 +00:00
encryption: Option<Encryption> => 3,
2017-03-22 08:19:16 +00:00
hash_method: HashMethod => 4,
raw_size: usize => 6,
encoded_size: usize => 7,
chunk_count: usize => 8,
2017-04-14 20:44:40 +00:00
chunk_list_size: usize => 9,
timestamp: i64 => 10
2017-03-22 08:19:16 +00:00
});
impl Default for BundleInfo {
fn default() -> Self {
BundleInfo {
id: BundleId(Hash::empty()),
compression: None,
encryption: None,
hash_method: HashMethod::Blake2,
raw_size: 0,
encoded_size: 0,
chunk_count: 0,
2017-04-03 12:05:16 +00:00
mode: BundleMode::Data,
2017-04-14 20:44:40 +00:00
chunk_list_size: 0,
timestamp: 0
2017-03-22 08:19:16 +00:00
}
}
}