Skip to content

Commit

Permalink
Merge pull request #1 from domodwyer/dom/feature-flags
Browse files Browse the repository at this point in the history
feat: Display base64 digest impl
  • Loading branch information
domodwyer committed Jun 17, 2023
2 parents ca27e13 + cbc6885 commit 769c9c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
39 changes: 10 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,28 @@ on: [push, pull_request]

jobs:
test:
strategy:
matrix:
feature_flags: ["no-default-features", "all-features"]
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
override: true

- name: fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
run: cargo fmt --all -- --check

- name: build
uses: actions-rs/cargo@v1
with:
command: build
args: --verbose
run: cargo build --${{matrix.feature_flags}} --verbose

- name: test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --lib --examples --tests --verbose
run: cargo test --${{matrix.feature_flags}} --lib --examples --tests --verbose

- name: build benchmarks
uses: actions-rs/cargo@v1
with:
command: test
args: --benches --no-run --verbose
run: cargo test --${{matrix.feature_flags}} --benches --no-run --verbose

- name: clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features
run: cargo clippy --${{matrix.feature_flags}}

- name: docs
uses: actions-rs/cargo@v1
with:
command: doc
args: --document-private-items --no-deps
run: cargo doc --${{matrix.feature_flags}} --document-private-items --no-deps
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["data-structures", "cryptography"]

[dependencies]
siphasher = "0.3.10"
base64 = { version = "0.21.2", optional = true }

[dev-dependencies]
assert_matches = "1.5.0"
Expand All @@ -21,6 +22,10 @@ insta = "1.29.0"
paste = "1.0.12"
proptest = "1.2.0"

[features]
default = ["digest_base64"]
digest_base64 = ["dep:base64"]

[lib]
bench = false

Expand Down
9 changes: 9 additions & 0 deletions src/digest/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ impl<const N: usize> AsRef<[u8]> for Digest<N> {
}
}

#[cfg(feature = "digest_base64")]
impl<const N: usize> std::fmt::Display for Digest<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use base64::{display::Base64Display, engine::general_purpose::STANDARD};

Base64Display::new(&self.0, &STANDARD).fmt(f)
}
}

/// Extract the number of leading 0's when expressed as base 16 digits, defining
/// the tree level the hash should reside at.
pub(crate) fn level<const N: usize>(v: &Digest<N>) -> u8 {
Expand Down
53 changes: 37 additions & 16 deletions src/digest/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,10 @@ impl RootHash {
}
}

/// Type wrapper over a [`Digest`] of a tree value, for readability / clarity /
/// compile-time safety.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ValueDigest<const N: usize>(Digest<N>);

impl<const N: usize> std::ops::Deref for ValueDigest<N> {
type Target = Digest<N>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<const N: usize> ValueDigest<N> {
pub(crate) const fn new(value: Digest<N>) -> Self {
Self(value)
#[cfg(feature = "digest_base64")]
impl std::fmt::Display for RootHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

Expand All @@ -65,3 +53,36 @@ impl PageDigest {
Self(value)
}
}

#[cfg(feature = "digest_base64")]
impl std::fmt::Display for PageDigest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

/// Type wrapper over a [`Digest`] of a tree value, for readability / clarity /
/// compile-time safety.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ValueDigest<const N: usize>(Digest<N>);

impl<const N: usize> std::ops::Deref for ValueDigest<N> {
type Target = Digest<N>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<const N: usize> ValueDigest<N> {
pub(crate) const fn new(value: Digest<N>) -> Self {
Self(value)
}
}

#[cfg(feature = "digest_base64")]
impl<const N: usize> std::fmt::Display for ValueDigest<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

0 comments on commit 769c9c4

Please sign in to comment.