confik/sources/
json_source.rs1use std::{borrow::Cow, error::Error, fmt};
2
3use crate::{ConfigurationBuilder, Source};
4
5#[derive(Clone)]
7pub struct JsonSource<'a> {
8 contents: Cow<'a, str>,
9 allow_secrets: bool,
10}
11
12impl<'a> JsonSource<'a> {
13 pub fn new(contents: impl Into<Cow<'a, str>>) -> Self {
15 Self {
16 contents: contents.into(),
17 allow_secrets: false,
18 }
19 }
20
21 pub fn allow_secrets(mut self) -> Self {
23 self.allow_secrets = true;
24 self
25 }
26}
27
28impl<T: ConfigurationBuilder> Source<T> for JsonSource<'_> {
29 fn allows_secrets(&self) -> bool {
30 self.allow_secrets
31 }
32
33 fn provide(&self) -> Result<T, Box<dyn Error + Sync + Send>> {
34 Ok(serde_json::from_str(&self.contents)?)
35 }
36}
37
38impl fmt::Debug for JsonSource<'_> {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.debug_struct("JsonSource")
41 .field("allow_secrets", &self.allow_secrets)
42 .finish_non_exhaustive()
43 }
44}