-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsave_webpage.rs
More file actions
55 lines (48 loc) · 1.61 KB
/
save_webpage.rs
File metadata and controls
55 lines (48 loc) · 1.61 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
// This example demonstrates how to download a webpage and save it to a file.
//
// The Hypershell program is defined as a `Program` type. It performs the
// following steps:
//
// 1. A `StreamingHttpRequest` handler fetches content from a given `url`.
// The URL is provided dynamically from the `url` field of the `MyApp` context.
//
// 2. The response stream is piped to the `WriteFile` handler.
//
// 3. The `WriteFile` handler saves the incoming stream to a file at a given
// path. The path is provided dynamically from the `file_path` field of the
// `MyApp` context.
//
// The `MyApp` context provides the `http_client` for the request, the `url` to
// download, and the `file_path` where the content should be saved.
//
// The `main` function initializes the `MyApp` context with these values and
// runs the program. A confirmation message is printed to the console upon
// completion.
use hypershell::prelude::*;
use reqwest::Client;
pub type Program = hypershell! {
StreamingHttpRequest<
GetMethod,
FieldArg<"url">,
WithHeaders[ ],
>
| WriteFile<FieldArg<"file_path">>
};
#[cgp_inherit(HypershellPreset)]
#[derive(HasField)]
pub struct MyApp {
pub http_client: Client,
pub url: String,
pub file_path: String,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let app = MyApp {
http_client: Client::new(),
url: "https://nixos.org/manual/nixpkgs/unstable/".to_owned(),
file_path: "nix_manual.html".to_owned(),
};
app.handle(PhantomData::<Program>, Vec::new()).await?;
println!("Webpage saved to nix_manual.html");
Ok(())
}