-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_checksum_native.rs
More file actions
67 lines (60 loc) · 2.2 KB
/
http_checksum_native.rs
File metadata and controls
67 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// This example demonstrates a fully native streaming pipeline by extending
// Hypershell with custom handlers for checksum calculation and hex encoding.
// It builds upon the `http_checksum_client` example by replacing the
// `sha256sum` and `cut` commands with native implementations.
//
// The Hypershell program is defined as a `Program` type. It performs the
// following steps:
//
// 1. A `StreamingHttpRequest` handler fetches content from a URL, similar to
// the previous example.
//
// 2. The response stream is piped to a custom `Checksum<Sha256>` handler. This
// handler is defined in the `hypershell-hash-components` crate and computes
// the SHA256 checksum natively using the `sha2` crate.
//
// 3. The raw byte output of the `Checksum` handler is piped to another custom
// handler, `BytesToHex`, which encodes the bytes into a hexadecimal string.
//
// 4. The final hex string is piped to `StreamToStdout`.
//
// Since `Checksum` and `BytesToHex` are not part of the default preset, a
// custom `MyAppPreset` is defined to extend `HypershellPreset`. This new
// preset overrides the `HandlerComponent` to include the providers for the
// new handlers (`HandleStreamChecksum` and `HandleBytesToHex`).
//
// The `MyApp` context is configured to use `MyAppPreset`, enabling it to
// execute the program with the new native handlers.
//
// The `main` function initializes the `MyApp` context and executes the program.
#![recursion_limit = "256"]
use hypershell::prelude::*;
use hypershell_examples::presets::HypershellChecksumPreset;
use hypershell_hash_components::dsl::{BytesToHex, Checksum};
use reqwest::Client;
use sha2::Sha256;
pub type Program = hypershell! {
StreamingHttpRequest<
GetMethod,
FieldArg<"url">,
WithHeaders[ ],
>
| Checksum<Sha256>
| BytesToHex
| StreamToStdout
};
#[cgp_inherit(HypershellChecksumPreset)]
#[derive(HasField)]
pub struct MyApp {
pub http_client: Client,
pub url: String,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let app = MyApp {
http_client: Client::new(),
url: "https://nixos.org/manual/nixpkgs/unstable/".to_owned(),
};
app.handle(PhantomData::<Program>, Vec::new()).await?;
Ok(())
}