Skip to content

feat: make IntegrityChecker and IntegrityOpts an optional feature #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ rust-version = "1.57.0"

[dependencies]
base64 = "0.21.0"
digest = "0.10.6"
digest = { version = "0.10.6", optional = true }
hex = "0.4.3"
miette = "5.7.0"
serde = { version = "1.0.152", optional = true }
sha-1 = "0.10.0"
sha2 = "0.10.6"
sha-1 = { version = "0.10.0", optional = true }
sha2 = { version = "0.10.6", optional = true }
thiserror = "1.0.40"
xxhash-rust = { version = "0.8.6", features = ["xxh3"] }
xxhash-rust = { version = "0.8.6", features = ["xxh3"], optional = true }

[features]
default = ["serde"]
default = ["hasher", "serde"]

# Enable [IntegrityChecker], [IntegrityOpts], and all algorithms
hasher = ["digest", "sha-1", "sha2", "xxhash-rust"]

[dev-dependencies]
serde_derive = "1.0.152"
Expand Down
14 changes: 13 additions & 1 deletion src/integrity.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::fmt;

use crate::algorithm::Algorithm;
#[cfg(feature = "hasher")]
use crate::checker::IntegrityChecker;
use crate::errors::Error;
use crate::hash::Hash;
#[cfg(feature = "hasher")]
use crate::opts::IntegrityOpts;

use base64::Engine as _;
Expand Down Expand Up @@ -131,6 +133,7 @@ impl Integrity {
/// let sri = Integrity::from(b"hello");
/// assert_eq!(sri.to_string(), "sha256-LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=".to_owned());
/// ```
#[cfg(feature = "hasher")]
pub fn from<B: AsRef<[u8]>>(data: B) -> Integrity {
IntegrityOpts::new()
.algorithm(Algorithm::Sha256)
Expand Down Expand Up @@ -185,6 +188,7 @@ impl Integrity {
/// let algorithm = sri.check(b"hello").unwrap();
/// assert_eq!(algorithm, Algorithm::Sha256);
/// ```
#[cfg(feature = "hasher")]
pub fn check<B: AsRef<[u8]>>(&self, data: B) -> Result<Algorithm, Error> {
let mut checker = IntegrityChecker::new(self.clone());
checker.input(&data);
Expand Down Expand Up @@ -245,7 +249,10 @@ impl Integrity {

#[cfg(test)]
mod tests {
use super::{Algorithm, Hash, Integrity, IntegrityOpts};
use super::{Algorithm, Hash, Integrity};

#[cfg(feature = "hasher")]
use super::IntegrityOpts;

#[test]
fn parse() {
Expand All @@ -260,6 +267,7 @@ mod tests {
}

#[test]
#[cfg(feature = "hasher")]
fn from_hex() {
let expected_integrity = Integrity::from(b"hello world");
let hex = String::from("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
Expand All @@ -270,6 +278,7 @@ mod tests {
}

#[test]
#[cfg(feature = "hasher")]
fn to_hex() {
let sri = Integrity::from(b"hello world");
assert_eq!(
Expand All @@ -282,6 +291,7 @@ mod tests {
}

#[test]
#[cfg(feature = "hasher")]
fn matches() {
let sri1 = IntegrityOpts::new()
.algorithm(Algorithm::Sha512)
Expand All @@ -296,6 +306,7 @@ mod tests {
}

#[test]
#[cfg(feature = "serde")]
fn de_json() {
use serde_derive::Deserialize;

Expand All @@ -316,6 +327,7 @@ mod tests {
}

#[test]
#[cfg(feature = "serde")]
fn ser_json() {
use serde_derive::Serialize;

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ incremental/streamed data input.
*/

mod algorithm;
#[cfg(feature = "hasher")]
mod checker;
mod errors;
mod hash;
mod integrity;
#[cfg(feature = "hasher")]
mod opts;

pub use algorithm::Algorithm::{self, *};
#[cfg(feature = "hasher")]
pub use checker::IntegrityChecker;
pub use errors::Error;
pub use hash::Hash;
pub use integrity::Integrity;
#[cfg(feature = "hasher")]
pub use opts::IntegrityOpts;