|
1 |
| -use std::error::Error; |
| 1 | +#[cfg(feature = "json")] |
| 2 | +mod example { |
| 3 | + use std::error::Error; |
2 | 4 |
|
3 |
| -use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map}; |
| 5 | + use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map}; |
4 | 6 |
|
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; |
8 | 10 |
|
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. |
13 | 15 |
|
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 |
| -} |
21 | 16 |
|
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 }"#); |
24 | 19 |
|
25 |
| - println!("Running server on localhost:5001"); |
| 20 | + println!("Running server on localhost:5001"); |
26 | 21 |
|
27 |
| - warp::serve(service).bind(([127, 0, 0, 1], 5001)).await; |
| 22 | + warp::serve(service).bind(([127, 0, 0, 1], 5001)).await; |
28 | 23 |
|
29 |
| - Ok(()) |
30 |
| -} |
| 24 | + Ok(()) |
| 25 | + } |
31 | 26 |
|
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; |
35 | 30 |
|
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?; |
43 | 38 |
|
44 |
| - println!("Config value is {}", config.get::<String>("value")?); |
| 39 | + println!("Config value is {}", config.get::<String>("value")?); |
45 | 40 |
|
46 |
| - Ok(()) |
47 |
| -} |
| 41 | + Ok(()) |
| 42 | + } |
48 | 43 |
|
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 | + } |
50 | 68 |
|
51 |
| -#[derive(Debug)] |
52 |
| -struct HttpSource { |
53 |
| - uri: String, |
54 |
| - format: FileFormat, |
55 | 69 | }
|
56 | 70 |
|
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 |
71 | 77 | }
|
72 | 78 | }
|
| 79 | + |
| 80 | +#[cfg(not(feature = "json"))] |
| 81 | +fn main() { |
| 82 | + println!("This example needs the 'json' feature enabled"); |
| 83 | +} |
| 84 | + |
0 commit comments