From 28895808789eb10e5f28b2748b89ad4fb07fd260 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 16 Aug 2024 17:59:17 -0700 Subject: [PATCH] Fix 'Env::raw()' doc example comments. Resolves #118. --- src/providers/env.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/providers/env.rs b/src/providers/env.rs index 4ab8ac6..64c8965 100644 --- a/src/providers/env.rs +++ b/src/providers/env.rs @@ -238,21 +238,32 @@ impl Env { /// considered. /// /// ```rust + /// # use std::collections::BTreeMap; + /// # use figment::util::map; /// use figment::{Jail, providers::Env}; /// /// Jail::expect_with(|jail| { /// jail.set_env("FOO_FOO", 100); /// jail.set_env("BAR_FOO", "hi"); - /// jail.set_env("foobar", "hi"); + /// jail.set_env("foo_bar", "hey"); /// /// // This is like `prefixed("foo_")` without the filtering. - /// let env = Env::raw().map(|k| match k.starts_with("foo_") { - /// true => k["foo_".len()..].into(), - /// false => k.into() - /// }); - /// - /// // We now have `FOO=100`, `BAR_FOO=hi`, and `bar=hi`. - /// assert_eq!(env.clone().filter(|k| k == "foo").iter().count(), 1); + /// let env = Env::raw() + /// .map(|k| match k.starts_with("foo_") { + /// true => k["foo_".len()..].into(), + /// false => k.into() + /// }); + /// + /// // We now have `FOO=100`, `BAR_FOO=hi`, and `bar=hey`. + /// # let mut env_vars = env.iter() + /// # .filter(|(k, _)| k.starts_with("foo") || k.starts_with("bar")) + /// # .collect::>(); + /// # env_vars.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then(v1.cmp(v2))); + /// assert_eq!(env_vars, [ + /// ("bar".into(), "hey".into()), + /// ("BAR_FOO".into(), "hi".into()), + /// ("FOO".into(), "100".into()), + /// ]); /// /// // Mappings chain, like iterator adapters. /// let env = env.map(|k| match k.starts_with("bar_") { @@ -260,8 +271,17 @@ impl Env { /// false => k.into() /// }); /// - /// // We now have `FOO=100`, `FOO=hi`, and `bar=hi`. - /// assert_eq!(env.filter(|k| k == "foo").iter().count(), 2); + /// // We now have `FOO=100`, `FOO=hi`, and `bar=hey`. + /// # let mut env_vars = env.iter() + /// # .filter(|(k, _)| k.starts_with("foo") || k.starts_with("bar")) + /// # .collect::>(); + /// # env_vars.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then(v1.cmp(v2))); + /// assert_eq!(env_vars, [ + /// ("bar".into(), "hey".into()), + /// ("FOO".into(), "100".into()), + /// ("FOO".into(), "hi".into()), + /// ]); + /// /// Ok(()) /// }); /// ```