From 54ccce860f8ef00819f81a7b44ae3b3c1857660b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Oct 2021 19:22:48 +0200 Subject: Reimplement example, compile only with "json" feature enabled Signed-off-by: Matthias Beyer --- examples/async_source/main.rs | 120 +++++++++++++++++++++++------------------- 1 file 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> { - select! { - r = run_server().fuse() => r, - r = run_client().fuse() => r - } -} -async fn run_server() -> Result<(), Box> { - let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#); + pub async fn run_server() -> Result<(), Box> { + 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> { - // 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> { + // Good enough for an example to allow server to start + tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; - let config = ConfigBuilder::::default() - .add_async_source(HttpSource { - uri: "http://localhost:5001/configuration".into(), - format: FileFormat::Json, - }) - .build() - .await?; + let config = ConfigBuilder::::default() + .add_async_source(HttpSource { + uri: "http://localhost:5001/configuration".into(), + format: FileFormat::Json, + }) + .build() + .await?; - println!("Config value is {}", config.get::("value")?); + println!("Config value is {}", config.get::("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, 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, 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> { + 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"); +} + -- cgit v1.2.3