@@ -2,6 +2,8 @@ use std::collections::HashMap;
2
2
use std:: fmt:: Debug ;
3
3
use std:: str:: FromStr ;
4
4
5
+ use async_trait:: async_trait;
6
+
5
7
use crate :: error:: * ;
6
8
use crate :: path;
7
9
use crate :: value:: { Value , ValueKind } ;
@@ -34,6 +36,45 @@ fn set_value(cache: &mut Value, key: &String, value: &Value) {
34
36
}
35
37
}
36
38
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
+
37
78
impl Clone for Box < dyn Source + Send + Sync > {
38
79
fn clone ( & self ) -> Box < dyn Source + Send + Sync > {
39
80
self . clone_into_box ( )
0 commit comments