Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions confik/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `Json5` source support under `Json5Source`.

## 0.15.0

- Add a new `confik(skip)` attribute. This allows skipping the field in the builder (and so it need not implement `Configuration` or be deserializable), however it must use `confik(default)` or `confik(default = ...)`, otherwise it can't be built. E.g.
Expand Down
2 changes: 2 additions & 0 deletions confik/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = ["env", "toml"]
# Source types
env = ["dep:envious"]
json = ["dep:serde_json"]
json5 = ["dep:json5"]
toml = ["dep:toml"]

# Destination types
Expand All @@ -50,6 +51,7 @@ serde = { version = "1", default-features = false, features = ["std", "derive"]
thiserror = "2"

envious = { version = "0.2", optional = true }
json5 = { version = "0.4", optional = true }
serde_json = { version = "1", optional = true }
toml = { version = "0.9", optional = true, default-features = false, features = ["parse", "serde"] }

Expand Down
2 changes: 2 additions & 0 deletions confik/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mod third_party;
use self::path::Path;
#[cfg(feature = "env")]
pub use self::sources::env_source::EnvSource;
#[cfg(feature = "json5")]
pub use self::sources::json5_source::Json5Source;
#[cfg(feature = "json")]
pub use self::sources::json_source::JsonSource;
#[cfg(feature = "toml")]
Expand Down
93 changes: 93 additions & 0 deletions confik/src/sources/json5_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{borrow::Cow, error::Error, fmt};

use crate::{ConfigurationBuilder, Source};

/// A [`Source`] containing raw JSON data.
#[derive(Clone)]
pub struct Json5Source<'a> {
contents: Cow<'a, str>,
allow_secrets: bool,
}

impl<'a> Json5Source<'a> {
/// Creates a [`Source`] containing raw JSON data.
pub fn new(contents: impl Into<Cow<'a, str>>) -> Self {
Self {
contents: contents.into(),
allow_secrets: false,
}
}

/// Allows this source to contain secrets.
pub fn allow_secrets(mut self) -> Self {
self.allow_secrets = true;
self
}
}

impl<'a> Source for Json5Source<'a> {

Check failure on line 28 in confik/src/sources/json5_source.rs

View workflow job for this annotation

GitHub Actions / clippy

missing generics for trait `sources::Source`

Check failure on line 28 in confik/src/sources/json5_source.rs

View workflow job for this annotation

GitHub Actions / docs

missing generics for trait `sources::Source`

Check failure on line 28 in confik/src/sources/json5_source.rs

View workflow job for this annotation

GitHub Actions / Test / stable

missing generics for trait `sources::Source`
fn allows_secrets(&self) -> bool {
self.allow_secrets
}

fn provide<T: ConfigurationBuilder>(&self) -> Result<T, Box<dyn Error + Sync + Send>> {
Ok(json5::from_str(&self.contents)?)
}
}

impl<'a> fmt::Debug for Json5Source<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Json5Source")
.field("allow_secrets", &self.allow_secrets)
.finish_non_exhaustive()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::Configuration;

#[test]
fn defaults() {
let source = Json5Source::new("{}");
assert!(!source.allows_secrets());
}

#[test]
fn clone() {
let source = Json5Source::new("{}").allow_secrets();
assert!(source.allows_secrets());
assert!(source.clone().allow_secrets);
}

#[test]
fn json5() {
#[derive(Configuration, Debug, PartialEq)]
struct Config {
message: String,
n: i32,
}

let config = "
{
// A traditional message.
message: 'hello world',

// A number for some reason.
n: 42,
}
";

assert_eq!(
Config::builder()
.override_with(Json5Source::new(config))
.try_build()
.expect("Failed to build config"),
Config {
message: "hello world".to_string(),
n: 42,
},
);
}
}
3 changes: 3 additions & 0 deletions confik/src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub(crate) mod file_source;
#[cfg(feature = "toml")]
pub(crate) mod toml_source;

#[cfg(feature = "json5")]
pub(crate) mod json5_source;

#[cfg(feature = "json")]
pub(crate) mod json_source;

Expand Down
Loading