Skip to content

Commit

Permalink
feat: add feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen committed May 30, 2024
1 parent 7633653 commit 19f045c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion metre-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metre-macros"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
license = "Apache-2.0"
description = "Macros for the metre crate"
Expand Down
41 changes: 34 additions & 7 deletions metre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,41 @@ keywords = ["configuration", "config", "loader", "env"]
categories = ["config", "environment-variables"]
authors = ["Ramiro Aisen <ramiroaisen@gmail.com>"]

[features]
default = [
"derive",
"env",
"toml",
"json",
]
full = [
"derive",
"env",
"toml",
"yaml",
"json",
"jsonc",
"url_blocking",
"url_async"
]
env = []
derive = [ "dep:metre-macros" ]
toml = [ "dep:toml" ]
yaml = [ "dep:serde_yaml" ]
json = [ "dep:serde_json" ]
jsonc = [ "dep:json_comments", "dep:serde_json" ]
url_blocking = ["dep:reqwest", "reqwest/blocking"]
url_async = ["dep:reqwest"]
reqwest-rustls-tls = ["reqwest?/rustls-tls"]


[dependencies]
metre-macros = { version = "0.1.1", path = "../metre-macros" }
json_comments = "0.2.2"
owo-colors = "4.0.0"
reqwest = { version = "0.12.4", features = ["blocking", "rustls-tls"] }
metre-macros = { version = "0.2.0", path = "../metre-macros", optional = true }
json_comments = { version = "0.2.2", optional = true }
owo-colors = { version = "4.0.0" }
reqwest = { version = "0.12.4", features = ["blocking", "rustls-tls"], optional = true }
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
serde_yaml = "0.9.34"
serde_json = { version = "1.0.117", optional = true }
serde_yaml = { version = "0.9.34", optional = true }
thiserror = "1.0.61"
toml = "0.8.13"
toml = { version = "0.8.13", optional = true }
14 changes: 13 additions & 1 deletion metre/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use owo_colors::*;
use std::convert::Infallible;
use std::sync::Arc;

#[allow(unused)]
use crate::LoadLocation;

/// An error that can happen anywhere in the config loading process
#[derive(Debug, Clone, thiserror::Error)]
pub enum Error {
/// A network error loading a configuration from a url
#[error("Network error loading config from {}", url.yellow())]
#[cfg(any(feature = "url_blocking", feature = "url_async"))]
Network {
url: String,
#[source]
Expand All @@ -26,6 +28,7 @@ pub enum Error {
},

/// A JSON or JSONC error when deserialzing a partial configuration
#[cfg(any(feature = "json", feature = "jsonc"))]
#[error("JSON error loading config from {}", location)]
Json {
#[source]
Expand All @@ -34,6 +37,7 @@ pub enum Error {
},

/// A TOML error when deserialzing a partial configuration
#[cfg(feature = "toml")]
#[error("TOML error loading config from {}", location)]
Toml {
#[source]
Expand All @@ -43,13 +47,15 @@ pub enum Error {

/// A YAML error when deserialzing a partial configuration
#[error("YAML error loading config from {}", location)]
#[cfg(feature = "yaml")]
Yaml {
#[source]
source: Arc<serde_yaml::Error>,
location: LoadLocation,
},

/// An error loading a partial configuration from an environment variable
#[cfg(feature = "env")]
#[error(transparent)]
FromEnv(#[from] FromEnvError),

Expand All @@ -73,6 +79,7 @@ pub struct MergeError {
}

/// Error parsing a value from an environment variable
#[cfg(feature = "env")]
#[derive(Debug, Clone, thiserror::Error)]
#[error("error parsing var {} from env for field: {}: {}", key.yellow(), field.yellow(), message)]
pub struct FromEnvError {
Expand Down Expand Up @@ -108,9 +115,14 @@ macro_rules! impl_from_infallible {
}
}


impl_from_infallible!(
Error
MergeError
FromEnvError
FromPartialError
);

#[cfg(feature = "env")]
impl_from_infallible!(
FromEnvError
);
Loading

0 comments on commit 19f045c

Please # to comment.