diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-10-02 19:22:48 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-10-02 20:40:17 +0200 |
commit | 54ccce860f8ef00819f81a7b44ae3b3c1857660b (patch) | |
tree | c318c2261535d8daaf1887e9925fba13561cab52 | |
parent | 3ecc24a33f1fda8945fbdd39fb822fe7e8b25aeb (diff) |
Reimplement example, compile only with "json" feature enabled
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | examples/async_source/main.rs | 120 |
1 files changed, 66 insertions, 54 deletions
diff --git a/examples/async_source/main.rs b/examples/async_source/main.rs index 005a473..d806844 100644 --- a/examples/async_source/main.rs +++ b/examples/async_source/main.rs @@ -1,72 +1,84 @@ -use std::error::Error; +#[cfg(feature = "json")] +mod example { + use std::error::Error; -use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map}; + use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, Map}; -use async_trait::async_trait; -use futures::{select, FutureExt}; -use warp::Filter; + use async_trait::async_trait; + use warp::Filter; -// Example below presents sample configuration server and client. -// -// Server serves simple configuration on HTTP endpoint. -// Client consumes it using custom HTTP AsyncSource built on top of reqwest. + // Example below presents sample configuration server and client. + // + // Server serves simple configuration on HTTP endpoint. + // Client consumes it using custom HTTP AsyncSource built on top of reqwest. -#[tokio::main] -async fn main() -> Result<(), Box<dyn Error>> { - select! { - r = run_server().fuse() => r, - r = run_client().fuse() => r - } -} -async fn run_server() -> Result<(), Box<dyn Error>> { - let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#); + pub async fn run_server() -> Result<(), Box<dyn Error>> { + let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#); - println!("Running server on localhost:5001"); + println!("Running server on localhost:5001"); - warp::serve(service).bind(([127, 0, 0, 1], 5001)).await; + warp::serve(service).bind(([127, 0, 0, 1], 5001)).await; - Ok(()) -} + Ok(()) + } -async fn run_client() -> Result<(), Box<dyn Error>> { - // Good enough for an example to allow server to start - tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; + pub async fn run_client() -> Result<(), Box<dyn Error>> { + // Good enough for an example to allow server to start + tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; - let config = ConfigBuilder::<AsyncState>::default() - .add_async_source(HttpSource { - uri: "http://localhost:5001/configuration".into(), - format: FileFormat::Json, - }) - .build() - .await?; + let config = ConfigBuilder::<AsyncState>::default() + .add_async_source(HttpSource { + uri: "http://localhost:5001/configuration".into(), + format: FileFormat::Json, + }) + .build() + .await?; - println!("Config value is {}", config.get::<String>("value")?); + println!("Config value is {}", config.get::<String>("value")?); - Ok(()) -} + Ok(()) + } -// Actual implementation of AsyncSource can be found below + // Actual implementation of AsyncSource can be found below + + #[derive(Debug)] + struct HttpSource { + uri: String, + format: FileFormat, + } + + #[async_trait] + impl AsyncSource for HttpSource { + async fn collect(&self) -> Result<Map<String, config::Value>, ConfigError> { + reqwest::get(&self.uri) + .await + .map_err(|e| ConfigError::Foreign(Box::new(e)))? // error conversion is possible from custom AsyncSource impls + .text() + .await + .map_err(|e| ConfigError::Foreign(Box::new(e))) + .and_then(|text| { + self.format + .parse(Some(&self.uri), &text) + .map_err(|e| ConfigError::Foreign(e)) + }) + } + } -#[derive(Debug)] -struct HttpSource { - uri: String, - format: FileFormat, } -#[async_trait] -impl AsyncSource for HttpSource { - async fn collect(&self) -> Result<Map<String, config::Value>, ConfigError> { - reqwest::get(&self.uri) - .await - .map_err(|e| ConfigError::Foreign(Box::new(e)))? // error conversion is possible from custom AsyncSource impls - .text() - .await - .map_err(|e| ConfigError::Foreign(Box::new(e))) - .and_then(|text| { - self.format - .parse(Some(&self.uri), &text) - .map_err(|e| ConfigError::Foreign(e)) - }) +#[cfg(feature = "json")] +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + use futures::{select, FutureExt}; + select! { + r = example::run_server().fuse() => r, + r = example::run_client().fuse() => r } } + +#[cfg(not(feature = "json"))] +fn main() { + println!("This example needs the 'json' feature enabled"); +} + |