Skip to content

Commit

Permalink
massively improved Builder architecture, altered crate structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sokorototo committed May 14, 2024
1 parent 12aa1da commit 4fe012b
Show file tree
Hide file tree
Showing 25 changed files with 638 additions and 609 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
vach-benchmarks/target

.vscode
.idea
Expand Down
36 changes: 28 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions vach/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ lz4_flex = { version = "0.11.1", optional = true, default-features = false, feat
"frame",
] }
snap = { version = "1.1.1", optional = true }
brotli = { version = "3.4.0", optional = true }
brotli = { version = "6.0.0", optional = true }

# Multithreaded features
parking_lot = { version = "0.12.1" }
rayon = { version = "1.8.0", optional = true }
[dev-dependencies]
rayon = "1.10.0"

[features]
default = ["builder", "archive"]
Expand All @@ -47,7 +46,7 @@ archive = []
builder = []

crypto = ["ed25519-dalek", "aes-gcm", "rand"]
multithreaded = ["rayon"]
multithreaded = []
compression = ["snap", "lz4_flex", "brotli"]

[package.metadata.docs.rs]
Expand Down
2 changes: 1 addition & 1 deletion vach/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aes_gcm::aead::Aead;
use aes_gcm::aes::cipher::consts::U12;
use aes_gcm::{Aes256Gcm, Nonce, KeyInit};

pub use ed25519_dalek::{SigningKey, VerifyingKey, SecretKey, Signature};
pub use ed25519_dalek::{SigningKey, VerifyingKey, Signature};

use crate::prelude::{InternalResult, InternalError};

Expand Down
5 changes: 1 addition & 4 deletions vach/src/crypto_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

use {
rand::rngs::OsRng,
crate::{
crypto,
global::{error::InternalError, result::InternalResult},
},
crate::{crypto, global::error::*},
std::io::Read,
};

Expand Down
4 changes: 2 additions & 2 deletions vach/src/global/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#![cfg_attr(docsrs, doc(cfg(feature = "compression")))]

use std::io::{self, Read, Write};
use crate::prelude::Flags;

use super::{error::InternalError, result::InternalResult};
use crate::prelude::Flags;
use super::error::*;

use lz4_flex as lz4;
use snap;
Expand Down
12 changes: 6 additions & 6 deletions vach/src/global/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{io, error};
use std::{error, io, sync::Arc};
use thiserror::Error;

/// Internal `Result` type alias used by `vach`. Basically equal to: `Result<T, InternalError>`
pub type InternalResult<T = ()> = Result<T, InternalError>;

/// All errors manifestable within `vach` collected into a neat enum
#[derive(Debug, Error)]
pub enum InternalError {
Expand All @@ -25,7 +28,7 @@ pub enum InternalError {
MissingResourceError(String),
/// Thrown when a leaf with an identical ID to a queued leaf is add with the `Builder::add(---)` functions
#[error("[VachError::LeafAppendError] A leaf with the ID: {0} already exists. Consider changing the ID to prevent collisions")]
LeafAppendError(String),
LeafAppendError(Arc<str>),
/// Thrown when no `Keypair` is provided and an encrypted [Leaf](crate::builder::Leaf) is encountered
#[error("[VachError::NoKeypairError] Unable to continue with cryptographic operation, as no keypair was supplied")]
NoKeypairError,
Expand All @@ -42,11 +45,8 @@ pub enum InternalError {
/// An error that is thrown when the current archive attempts to load an incompatible version, contains the incompatible version
#[error("The provided archive source has version: {}. While the current implementation has a spec-version: {}. The provided source is incompatible!", .0, crate::VERSION)]
IncompatibleArchiveVersionError(u16),
/// An error that is thrown when if `Mutex` is poisoned, when a message doesn't go though an `mspc::sync_channel` or other sync related issues
#[error("[VachError::SyncError] {0}")]
SyncError(String),
/// Errors thrown during compression or decompression
#[error("[VachError::CompressorDecompressorError]: {0}")]
#[cfg(feature = "compression")]
DeCompressionError(#[from] lz4_flex::frame::Error)
DeCompressionError(#[from] lz4_flex::frame::Error),
}
5 changes: 3 additions & 2 deletions vach/src/global/flags.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fmt;
use super::{error::InternalError, result::InternalResult};
use super::error::*;

/// Abstracted flag access and manipulation `struct`.
/// A knock-off minimal [bitflags](https://crates.io/crates/bitflags) of sorts.
#[derive(Copy, Clone, Default, PartialEq)]
#[derive(Copy, Clone, Default)]
#[repr(transparent)]
pub struct Flags {
pub(crate) bits: u32,
}
Expand Down
2 changes: 1 addition & 1 deletion vach/src/global/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, io::Read, str};

#[cfg(feature = "crypto")]
use crate::crypto;
use super::{error::InternalError, result::InternalResult, flags::Flags};
use super::{error::*, flags::Flags};

/// Used to configure and give extra information to the [`Archive`](crate::archive::Archive) loader.
/// Used exclusively in archive source and integrity validation.
Expand Down
5 changes: 3 additions & 2 deletions vach/src/global/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Globally available exports
pub mod compressor;
pub mod error;

pub mod flags;
pub mod header;
pub mod reg_entry;
pub mod result;

pub mod compressor;
34 changes: 24 additions & 10 deletions vach/src/global/reg_entry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{io::Read, fmt};
use super::{result::InternalResult, flags::Flags};
use std::{fmt, io::Read, sync::Arc};
use super::{error::*, flags::Flags};

#[cfg(feature = "crypto")]
use crate::crypto;

/// Stand-alone meta-data for an archive entry(Leaf). This can be fetched without reading from the archive.
#[derive(Debug, Clone)]
pub struct RegistryEntry {
/// Self explanatory?
pub id: Arc<str>,
/// The flags extracted from the archive entry and parsed into a accessible struct
pub flags: Flags,
/// The content version of the extracted archive entry
Expand All @@ -28,6 +30,7 @@ impl RegistryEntry {
#[inline(always)]
pub(crate) fn empty() -> RegistryEntry {
RegistryEntry {
id: Arc::from("<EMPTY ID>"),
flags: Flags::empty(),
content_version: 0,
location: 0,
Expand All @@ -41,7 +44,7 @@ impl RegistryEntry {
/// Given a read handle, will proceed to read and parse bytes into a [`RegistryEntry`] struct. (de-serialization)
/// ### Errors
/// Produces `io` errors and if the bytes in the id section is not valid UTF-8
pub(crate) fn from_handle<T: Read>(mut handle: T) -> InternalResult<(RegistryEntry, String)> {
pub(crate) fn from_handle<T: Read>(mut handle: T) -> InternalResult<RegistryEntry> {
let mut buffer: [u8; RegistryEntry::MIN_SIZE] = [0u8; RegistryEntry::MIN_SIZE];
handle.read_exact(&mut buffer)?;

Expand All @@ -66,8 +69,6 @@ impl RegistryEntry {
// If the `crypto` feature is turned off then the bytes are just read then discarded
#[cfg(feature = "crypto")]
{
use super::error::InternalError;

let sig = match crypto::Signature::try_from(sig_bytes) {
Ok(sig) => sig,
Err(err) => return Err(InternalError::ParseError(err.to_string())),
Expand All @@ -83,6 +84,7 @@ impl RegistryEntry {

// Build entry step manually, to prevent unnecessary `Default::default()` call, then changing fields individually
let entry = RegistryEntry {
id: id.into(),
flags,
content_version,
location,
Expand All @@ -92,26 +94,38 @@ impl RegistryEntry {
signature,
};

Ok((entry, id))
Ok(entry)
}

/// Serializes a [`RegistryEntry`] struct into an array of bytes
pub(crate) fn bytes(&self, id_length: &u16) -> Vec<u8> {
let mut buffer = Vec::with_capacity(RegistryEntry::MIN_SIZE + 64);
pub(crate) fn bytes(&self) -> InternalResult<Vec<u8>> {
// Make sure the ID is not too big or else it will break the archive
let id = &self.id;

if id.len() >= crate::MAX_ID_LENGTH {
let copy = id.to_string();
return Err(InternalError::IDSizeOverflowError(copy));
};

let mut buffer = Vec::with_capacity(RegistryEntry::MIN_SIZE + id.len());
let len = id.len() as u16;

buffer.extend_from_slice(&self.flags.bits().to_le_bytes());
buffer.extend_from_slice(&self.content_version.to_le_bytes());
buffer.extend_from_slice(&self.location.to_le_bytes());
buffer.extend_from_slice(&self.offset.to_le_bytes());
buffer.extend_from_slice(&id_length.to_le_bytes());
buffer.extend_from_slice(&len.to_le_bytes());

// Only write signature if one exists
#[cfg(feature = "crypto")]
if let Some(signature) = self.signature {
buffer.extend_from_slice(&signature.to_bytes())
};

buffer
// Append id
buffer.extend_from_slice(id.as_bytes());

Ok(buffer)
}
}

Expand Down
4 changes: 0 additions & 4 deletions vach/src/global/result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
use super::error::InternalError;

/// Internal `Result` type alias used by `vach`. Basically equal to: `Result<T, InternalError>`
pub type InternalResult<T = ()> = Result<T, InternalError>;
19 changes: 4 additions & 15 deletions vach/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ pub(crate) mod writer;
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
pub use rand;

#[cfg(feature = "multithreaded")]
#[cfg_attr(docsrs, doc(cfg(feature = "multithreaded")))]
pub use rayon;

/// Current [`vach`](crate) spec version. increments by ten with every spec change
pub const VERSION: u16 = 30;

Expand All @@ -160,9 +156,7 @@ pub const DEFAULT_MAGIC: &[u8; crate::MAGIC_LENGTH] = b"VfACH";

/// Consolidated import for crate logic; This module stores all `structs` associated with this crate. Constants can be accesses [directly](#constants) with `crate::<CONSTANT>`
pub mod prelude {
pub use crate::global::{
error::InternalError, result::InternalResult, flags::Flags, header::ArchiveConfig, reg_entry::RegistryEntry,
};
pub use crate::global::{error::*, flags::Flags, header::ArchiveConfig, reg_entry::RegistryEntry};

#[cfg(feature = "crypto")]
pub use crate::crypto::*;
Expand All @@ -181,14 +175,9 @@ pub mod crypto;
#[cfg(feature = "builder")]
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
pub mod builder {
pub use crate::writer::{
builder::{Builder, BuilderConfig},
leaf::Leaf,
};
pub use crate::global::{error::InternalError, result::InternalResult, flags::Flags};
pub use crate::writer::*;
pub use crate::global::{error::*, flags::Flags};

#[cfg(feature = "compression")]
pub use crate::writer::compress_mode::CompressMode;
#[cfg(feature = "compression")]
pub use crate::global::compressor::CompressionAlgorithm;
}
Expand All @@ -199,7 +188,7 @@ pub mod builder {
pub mod archive {
pub use crate::loader::{archive::Archive, resource::Resource};
pub use crate::global::{
reg_entry::RegistryEntry, header::ArchiveConfig, error::InternalError, result::InternalResult, flags::Flags,
reg_entry::RegistryEntry, header::ArchiveConfig, error::*, flags::Flags,
};
#[cfg(feature = "compression")]
pub use crate::global::compressor::CompressionAlgorithm;
Expand Down
Loading

0 comments on commit 4fe012b

Please sign in to comment.