-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnix_manual.rs
More file actions
71 lines (65 loc) · 2.05 KB
/
nix_manual.rs
File metadata and controls
71 lines (65 loc) · 2.05 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
68
69
70
71
// This example demonstrates a pipeline that combines a native HTTP request
// with subsequent processing using external CLI tools.
//
// The Hypershell program is defined as a `Program` type. It performs the
// following steps:
//
// 1. A `StreamingHttpRequest` handler fetches the content of the Nixpkgs manual.
// The URL is hardcoded as a `StaticArg`.
//
// 2. The downloaded content is streamed to a `StreamingExec` handler that runs
// `tr "[:lower:]" "[:upper:]"` to convert the text to uppercase.
//
// 3. The transformed text is then streamed to another `StreamingExec` handler
// that runs `grep`. `grep` filters the stream for lines containing a
// `keyword`, performing a case-insensitive search (`-i`).
//
// 4. The `keyword` is provided dynamically by the `keyword` field of the
// `MyApp` context.
//
// 5. The final filtered output is piped to `StreamToStdout`.
//
// The `MyApp` context provides the `http_client` for the HTTP request and the
// `keyword` for the `grep` command.
//
// The `main` function initializes the `MyApp` context and runs the program.
use hypershell::prelude::*;
use hypershell::presets::HypershellPreset;
use reqwest::Client;
pub type Program = hypershell! {
StreamingHttpRequest<
GetMethod,
StaticArg<"https://nixos.org/manual/nixpkgs/unstable/">,
WithHeaders<Nil>,
>
| StreamingExec<
StaticArg<"tr">,
WithStaticArgs [
"[:lower:]",
"[:upper:]",
],
>
| StreamingExec<
StaticArg<"grep">,
WithArgs [
StaticArg<"-i">,
FieldArg<"keyword">,
],
>
| StreamToStdout
};
#[cgp_inherit(HypershellPreset)]
#[derive(HasField)]
pub struct MyApp {
pub http_client: Client,
pub keyword: String,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let app = MyApp {
http_client: Client::new(),
keyword: "Nix".to_owned(),
};
app.handle(PhantomData::<Program>, Vec::new()).await?;
Ok(())
}