Skip to content

Commit 48e4a66

Browse files
szarykottmatthiasbeyer
authored andcommitted
Add AsyncSource trait
This patch adds the AsyncSource trait, the interface for providing async source functionality for this crate. Signed-off-by: Matthias Beyer <[email protected]> Reviewed-by: Matthias Beyer <[email protected]>
1 parent bbb61a6 commit 48e4a66

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ini = ["rust-ini"]
2323
json5 = ["json5_rs"]
2424

2525
[dependencies]
26+
async-trait = "0.1.50"
2627
lazy_static = "1.0"
2728
serde = "1.0.8"
2829
nom = "6"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub use crate::config::Config;
6969
pub use crate::env::Environment;
7070
pub use crate::error::ConfigError;
7171
pub use crate::file::{File, FileFormat, FileSourceFile, FileSourceString};
72+
pub use crate::source::AsyncSource;
7273
pub use crate::source::Source;
7374
pub use crate::value::Value;
7475
pub use crate::value::ValueKind;

src/source.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::collections::HashMap;
22
use std::fmt::Debug;
33
use std::str::FromStr;
44

5+
use async_trait::async_trait;
6+
57
use crate::error::*;
68
use crate::path;
79
use crate::value::{Value, ValueKind};
@@ -34,6 +36,45 @@ fn set_value(cache: &mut Value, key: &String, value: &Value) {
3436
}
3537
}
3638

39+
/// Describes a generic _source_ of configuration properties capable of using an async runtime.
40+
///
41+
/// At the moment this library does not implement it, although it allows using its implementations
42+
/// within builders. Due to the scattered landscape of asynchronous runtimes, it is impossible to
43+
/// cater to all needs with one implementation. Also, this trait might be most useful with remote
44+
/// configuration sources, reachable via the network, probably using HTTP protocol. Numerous HTTP
45+
/// libraries exist, making it even harder to find one implementation that rules them all.
46+
///
47+
/// For those reasons, it is left to other crates to implement runtime-specific or proprietary
48+
/// details.
49+
///
50+
/// It is advised to use `async_trait` crate while implementing this trait.
51+
///
52+
/// See examples for sample implementation.
53+
#[async_trait]
54+
pub trait AsyncSource: Debug + Sync {
55+
// Sync is supertrait due to https://docs.rs/async-trait/0.1.50/async_trait/index.html#dyn-traits
56+
57+
/// Collects all configuration properties available from this source and return
58+
/// a HashMap as an async operations.
59+
async fn collect(&self) -> Result<HashMap<String, Value>>;
60+
61+
/// Collects all configuration properties to a provided cache.
62+
async fn collect_to(&self, cache: &mut Value) -> Result<()> {
63+
self.collect()
64+
.await?
65+
.iter()
66+
.for_each(|(key, val)| set_value(cache, key, val));
67+
68+
Ok(())
69+
}
70+
}
71+
72+
impl Clone for Box<dyn AsyncSource + Send + Sync> {
73+
fn clone(&self) -> Self {
74+
self.to_owned()
75+
}
76+
}
77+
3778
impl Clone for Box<dyn Source + Send + Sync> {
3879
fn clone(&self) -> Box<dyn Source + Send + Sync> {
3980
self.clone_into_box()

0 commit comments

Comments
 (0)