Skip to content

Commit

Permalink
Merge pull request #24 from baoyachi/laerling/separate-modules
Browse files Browse the repository at this point in the history
separate modules
  • Loading branch information
baoyachi authored Apr 7, 2024
2 parents 4184f8e + ae3702c commit 5dab840
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 271 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
override: true
components: clippy, rustfmt
- name: Check format
run: cargo fmt --all -- --check
run: cargo fmt --all
- name: Check fix
run: cargo fix && cargo fix
- name: Check with clippy
Expand All @@ -48,6 +48,7 @@ jobs:
windows-vcpkg:
name: windows-vcpkg
runs-on: windows-latest
needs: [ build ]
steps:
- uses: actions/checkout@v3
- uses: sfackler/actions/rustup@master
Expand All @@ -61,7 +62,7 @@ jobs:
override: true
components: clippy, rustfmt
- name: Check format
run: cargo fmt --all -- --check
run: cargo fmt --all
- name: Check fix
run: cargo fix && cargo fix
- name: Check with clippy
Expand All @@ -77,3 +78,15 @@ jobs:
- name: Build on nightly
run: cargo build --release

publish-crate:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs: [ windows-vcpkg ]
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v2
- uses: actions/checkout@v4
- name: Publish
shell: bash
run: |
cargo publish --token ${{ secrets.CRATES_GITHUB_TOKEN }}
24 changes: 0 additions & 24 deletions .github/workflows/publish.yml

This file was deleted.

74 changes: 74 additions & 0 deletions src/async_digest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{CalculatorSelector, TrySha256Digest};
use bytes::BytesMut;
use std::io;

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
///
/// ```
pub async fn try_async_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.async_digest().await
}

/// sha256 digest file
///
/// # Examples
///
/// ```rust
/// use sha256::{try_async_openssl_digest};
/// use std::path::Path;
/// let input = Path::new("./foo.file");
/// tokio_test::block_on(async{
/// let val = try_async_openssl_digest(input).await.unwrap();
/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1")
/// });
/// ```
#[cfg(feature = "native_openssl")]
pub async fn try_async_openssl_digest<D: TrySha256Digest>(input: D) -> Result<String, D::Error> {
input.async_openssl_digest().await
}

#[async_trait::async_trait]
pub trait AsyncCalculatorInput {
async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result<usize>;
}

pub async fn async_calc<I, S>(mut input: I, mut selector: S) -> io::Result<String>
where
I: AsyncCalculatorInput,
S: CalculatorSelector,
{
let mut buf = BytesMut::with_capacity(1024);
loop {
buf.clear();
let len = input.read_inner(&mut buf).await?;
if len == 0 {
break;
}
selector.update_inner(&buf[0..len]);
}
let hash = selector.finish_inner();
Ok(hex::encode(hash))
}

#[async_trait::async_trait]
impl<R> AsyncCalculatorInput for tokio::io::BufReader<R>
where
R: tokio::io::AsyncRead + Unpin + Send,
{
async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result<usize> {
use tokio::io::AsyncReadExt;

self.read_buf(buf).await
}
}
Loading

0 comments on commit 5dab840

Please sign in to comment.