Skip to content

Commit

Permalink
Fix 'Env::raw()' doc example comments.
Browse files Browse the repository at this point in the history
Resolves #118.
  • Loading branch information
SergioBenitez committed Aug 17, 2024
1 parent c730bdf commit 2889580
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/providers/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,30 +238,50 @@ 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::<Vec<_>>();
/// # 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_") {
/// true => k["bar_".len()..].into(),
/// 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::<Vec<_>>();
/// # 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(())
/// });
/// ```
Expand Down

0 comments on commit 2889580

Please sign in to comment.