Skip to content

Commit

Permalink
Add 'Profile::const_new().' Remove 'magic' feature.
Browse files Browse the repository at this point in the history
The 'magic' values no longer use serde's 'derive'. Without the
introduction of the derive dependencies, there is no need for a 'magic'
feature, so the feature was removed.
  • Loading branch information
SergioBenitez committed Oct 16, 2020
1 parent 25f5812 commit a8bd671
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,6 @@ json = ["serde_json"]
yaml = ["serde_yaml"]
parse-value = ["pear"]
test = ["tempfile", "parking_lot"]
magic = ["serde/derive"]
# toml = ["toml"]

[dependencies]
Expand All @@ -33,6 +32,7 @@ parking_lot = { version = "0.11", optional = true }
[dev-dependencies]
tempfile = "3"
parking_lot = "0.11"
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
version_check = "0.9"
Expand Down
1 change: 0 additions & 1 deletion scripts/test.sh
Expand Up @@ -74,7 +74,6 @@ if [ "$1" = "--core" ]; then
json
yaml
test
magic
)

echo ":: Building and testing core [no features]..."
Expand Down
6 changes: 3 additions & 3 deletions src/figment.rs
Expand Up @@ -413,7 +413,7 @@ impl Figment {
self.metadata.get(&self.find_value(key).ok()?.tag())
}

/// Returns the metadata with the given `id` if this figment contains a
/// Returns the metadata with the given `tag` if this figment contains a
/// value with said metadata.
///
/// # Example
Expand Down Expand Up @@ -442,8 +442,8 @@ impl Figment {
/// Ok(())
/// });
/// ```
pub fn get_metadata(&self, id: Tag) -> Option<&Metadata> {
self.metadata.get(&id)
pub fn get_metadata(&self, tag: Tag) -> Option<&Metadata> {
self.metadata.get(&tag)
}
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Expand Up @@ -285,7 +285,6 @@
//! | feature | gated namespace | description |
//! |---------|---------------------|-----------------------------------------|
//! | `test` | [`Jail`] | Semi-sandboxed environment for testing. |
//! | `magic` | [`value::magic`] | "Magic" deserializable values. |
//! | `env` | [`providers::Env`] | Environment variable [`Provider`]. |
//! | `toml` | [`providers::Toml`] | TOML file/string [`Provider`]. |
//! | `json` | [`providers::Json`] | JSON file/string [`Provider`]. |
Expand Down
32 changes: 30 additions & 2 deletions src/profile.rs
Expand Up @@ -61,11 +61,11 @@ impl From<&Profile> for ProfileTag {
impl Profile {
/// The default profile: `"default"`.
#[allow(non_upper_case_globals)]
pub const Default: Profile = Profile(Uncased::from_borrowed("default"));
pub const Default: Profile = Profile::const_new("default");

/// The global profile: `"global"`.
#[allow(non_upper_case_globals)]
pub const Global: Profile = Profile(Uncased::from_borrowed("global"));
pub const Global: Profile = Profile::const_new("global");

/// Constructs a profile with the name `name`.
///
Expand All @@ -82,6 +82,22 @@ impl Profile {
Profile(name.to_string().into())
}

/// A `const` to construct a profile with the name `name`.
///
/// # Example
///
/// ```rust
/// use figment::Profile;
///
/// const STAGING: Profile = Profile::const_new("staging");
///
/// assert_eq!(STAGING, "staging");
/// assert_eq!(STAGING, "STAGING");
/// ```
pub const fn const_new(name: &'static str) -> Profile {
Profile(Uncased::from_borrowed(name))
}

/// Constructs a profile from the value of the environment variable with
/// name `name`, if one is present. The search for `name` is
/// case-insensitive.
Expand Down Expand Up @@ -248,6 +264,18 @@ impl PartialEq<Profile> for &str {
}
}

impl PartialEq<Profile> for &Profile {
fn eq(&self, other: &Profile) -> bool {
self.as_str() == other.as_str()
}
}

impl PartialEq<&Profile> for Profile {
fn eq(&self, other: &&Profile) -> bool {
self.as_str() == other.as_str()
}
}

struct Visitor;

impl<'de> de::Deserialize<'de> for Profile {
Expand Down
4 changes: 1 addition & 3 deletions src/value/de.rs
Expand Up @@ -60,14 +60,12 @@ impl<'de: 'c, 'c> Deserializer<'de> for ConfiguredValueDe<'c> {
_fields: &'static [&'static str],
visitor: V
) -> Result<V::Value> {
#[cfg(feature = "magic")] use crate::value::magic::*;
use crate::value::magic::*;

let (config, tag) = (self.config, self.value.tag());
let result = match name {
Value::NAME => Value::deserialize_from(self, visitor),
#[cfg(feature = "magic")]
RelativePathBuf::NAME => RelativePathBuf::deserialize_from(self, visitor),
#[cfg(feature = "magic")]
Tagged::<()>::NAME => Tagged::<()>::deserialize_from(self, visitor),
// SelectedProfile::NAME => SelectedProfile::deserialize_from(self, visitor),
_ => self.deserialize_any(visitor)
Expand Down

0 comments on commit a8bd671

Please sign in to comment.