Skip to content

Commit 317cf97

Browse files
committed
Reimplement example, compile only with "json" feature enabled
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 parent 69d337a commit 317cf97

File tree

1 file changed

+66
-54
lines changed

1 file changed

+66
-54
lines changed

examples/async_source/main.rs

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,84 @@
1-
use std::error::Error;
1+
#[cfg(feature = "json")]
2+
mod example {
3+
use std::error::Error;
24

3-
use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map};
5+
use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map};
46

5-
use async_trait::async_trait;
6-
use futures::{select, FutureExt};
7-
use warp::Filter;
7+
use async_trait::async_trait;
8+
use futures::{select, FutureExt};
9+
use warp::Filter;
810

9-
// Example below presents sample configuration server and client.
10-
//
11-
// Server serves simple configuration on HTTP endpoint.
12-
// Client consumes it using custom HTTP AsyncSource built on top of reqwest.
11+
// Example below presents sample configuration server and client.
12+
//
13+
// Server serves simple configuration on HTTP endpoint.
14+
// Client consumes it using custom HTTP AsyncSource built on top of reqwest.
1315

14-
#[tokio::main]
15-
async fn main() -> Result<(), Box<dyn Error>> {
16-
select! {
17-
r = run_server().fuse() => r,
18-
r = run_client().fuse() => r
19-
}
20-
}
2116

22-
async fn run_server() -> Result<(), Box<dyn Error>> {
23-
let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#);
17+
async fn run_server() -> Result<(), Box<dyn Error>> {
18+
let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#);
2419

25-
println!("Running server on localhost:5001");
20+
println!("Running server on localhost:5001");
2621

27-
warp::serve(service).bind(([127, 0, 0, 1], 5001)).await;
22+
warp::serve(service).bind(([127, 0, 0, 1], 5001)).await;
2823

29-
Ok(())
30-
}
24+
Ok(())
25+
}
3126

32-
async fn run_client() -> Result<(), Box<dyn Error>> {
33-
// Good enough for an example to allow server to start
34-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
27+
async fn run_client() -> Result<(), Box<dyn Error>> {
28+
// Good enough for an example to allow server to start
29+
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
3530

36-
let config = ConfigBuilder::<AsyncState>::default()
37-
.add_async_source(HttpSource {
38-
uri: "http://localhost:5001/configuration".into(),
39-
format: FileFormat::Json,
40-
})
41-
.build()
42-
.await?;
31+
let config = ConfigBuilder::<AsyncState>::default()
32+
.add_async_source(HttpSource {
33+
uri: "http://localhost:5001/configuration".into(),
34+
format: FileFormat::Json,
35+
})
36+
.build()
37+
.await?;
4338

44-
println!("Config value is {}", config.get::<String>("value")?);
39+
println!("Config value is {}", config.get::<String>("value")?);
4540

46-
Ok(())
47-
}
41+
Ok(())
42+
}
4843

49-
// Actual implementation of AsyncSource can be found below
44+
// Actual implementation of AsyncSource can be found below
45+
46+
#[derive(Debug)]
47+
struct HttpSource {
48+
uri: String,
49+
format: FileFormat,
50+
}
51+
52+
#[async_trait]
53+
impl AsyncSource for HttpSource {
54+
async fn collect(&self) -> Result<Map<String, config::Value>, ConfigError> {
55+
reqwest::get(&self.uri)
56+
.await
57+
.map_err(|e| ConfigError::Foreign(Box::new(e)))? // error conversion is possible from custom AsyncSource impls
58+
.text()
59+
.await
60+
.map_err(|e| ConfigError::Foreign(Box::new(e)))
61+
.and_then(|text| {
62+
self.format
63+
.parse(Some(&self.uri), &text)
64+
.map_err(|e| ConfigError::Foreign(e))
65+
})
66+
}
67+
}
5068

51-
#[derive(Debug)]
52-
struct HttpSource {
53-
uri: String,
54-
format: FileFormat,
5569
}
5670

57-
#[async_trait]
58-
impl AsyncSource for HttpSource {
59-
async fn collect(&self) -> Result<Map<String, config::Value>, ConfigError> {
60-
reqwest::get(&self.uri)
61-
.await
62-
.map_err(|e| ConfigError::Foreign(Box::new(e)))? // error conversion is possible from custom AsyncSource impls
63-
.text()
64-
.await
65-
.map_err(|e| ConfigError::Foreign(Box::new(e)))
66-
.and_then(|text| {
67-
self.format
68-
.parse(Some(&self.uri), &text)
69-
.map_err(|e| ConfigError::Foreign(e))
70-
})
71+
#[cfg(feature = "json")]
72+
#[tokio::main]
73+
async fn main() -> Result<(), Box<dyn Error>> {
74+
select! {
75+
r = example::run_server().fuse() => r,
76+
r = example::run_client().fuse() => r
7177
}
7278
}
79+
80+
#[cfg(not(feature = "json"))]
81+
fn main() {
82+
println!("This example needs the 'json' feature enabled");
83+
}
84+

0 commit comments

Comments
 (0)