environment-0.1.1/.gitignore01006440000765000002400000000037131676514250014276 0ustar0000000000000000/target/ **/*.rs.bk Cargo.lock environment-0.1.1/.travis.yml01006440000765000002400000002121131676514250014413 0ustar0000000000000000sudo: false language: rust cache: cargo rust: - beta - stable - nightly matrix: include: - rust: nightly-2017-10-09 env: CLIPPY_VERS="0.0.165" before_script: | [[ "$(cargo +nightly-2017-10-09 clippy --version)" != "$CLIPPY_VERS" ]] && \ cargo +nightly-2017-10-09 install clippy --vers "$CLIPPY_VERS" --force || true script: | cargo +nightly-2017-10-09 clippy -- -D warnings - rust: nightly-2017-10-09 env: RUSTFMT_VERS="0.2.8" before_script: | [[ "$(cargo +nightly-2017-10-09 fmt -- --version)" != "$RUSTFMT_VERS"* ]] && \ cargo +nightly-2017-10-09 install rustfmt-nightly --vers "$RUSTFMT_VERS" --force || true script: | cargo +nightly-2017-10-09 fmt --all -- --write-mode=diff before_script: - | pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH script: - | travis-cargo build && travis-cargo clean && travis-cargo test && travis-cargo bench notifications: email: on_success: never branches: only: - master - staging - trying env: global: - TRAVIS_CARGO_NIGHTLY_FEATURE="" environment-0.1.1/Cargo.toml.orig01006440000765000002400000000645131676532200015175 0ustar0000000000000000[package] name = "environment" version = "0.1.1" authors = ["Freyskeyd "] description = "Helper to deal with environment variables." license = "MIT OR Apache-2.0" repository = "https://github.com/Freyskeyd/environment.git" homepage = "https://github.com/Freyskeyd/environment" documentation = "http://docs.rs/environment/" readme = "README.md" keywords = ["environment", "env"] [dependencies] environment-0.1.1/Cargo.toml0000644000000016610010420 0ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "environment" version = "0.1.1" authors = ["Freyskeyd "] description = "Helper to deal with environment variables." homepage = "https://github.com/Freyskeyd/environment" documentation = "http://docs.rs/environment/" readme = "README.md" keywords = ["environment", "env"] license = "MIT OR Apache-2.0" repository = "https://github.com/Freyskeyd/environment.git" [dependencies] environment-0.1.1/README.md01006440000765000002400000002575131676530050013572 0ustar0000000000000000# Environment > **Handle environment variable context** - This crate helps you to deal with environment variables. [![Build Status](https://travis-ci.org/Freyskeyd/environment.svg)](https://travis-ci.org/Freyskeyd/environment) [![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation] ## Install Just add it to your `Cargo.toml`: ```toml [dependencies] environment = "0.1" ``` ## Example Here's a trivial example: ```rust extern crate environment; use std::process::Command; fn main() { let env = Environment::inherit().insert("foo", "bar"); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(env.compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert!(output.contains("foo=bar")); } ``` ## License Licensed under either of * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. [Documentation]: https://docs.rs/environment environment-0.1.1/rustfmt.toml01006440000765000002400000000056131676514250014710 0ustar0000000000000000format_strings = false reorder_imports = true environment-0.1.1/src/lib.rs01006440000765000002400000013562131676525540014224 0ustar0000000000000000use std::ffi::OsString; /// Structure to deal with environment variables #[derive(Clone, Debug, PartialEq, Eq)] pub struct Environment { /// Customized environment variables vars: Vec<(OsString, OsString)>, /// Define if the structure must inherit inherit: bool, } impl Default for Environment { fn default() -> Self { Self { vars: vec![], inherit: false, } } } impl Environment { /// Create a new Environment that inherits this process' environment. /// /// # Examples /// /// ```rust /// extern crate environment; /// use std::ffi::OsString; /// /// let e = environment::Environment::inherit().compile(); /// let e_: Vec<(OsString, OsString)> = ::std::env::vars_os().collect(); /// /// assert_eq!(e, e_); /// ``` pub fn inherit() -> Self { Self { vars: vec![], inherit: true, } } /// Create a new Environment independent of the current process's Environment /// /// # Examples /// /// ```rust /// extern crate environment; /// /// let e = environment::Environment::empty().compile(); /// assert_eq!(e, Vec::new()); /// ``` pub fn empty() -> Self { Self::default() } /// Insert a new entry into the custom variables for this environment object /// /// # Examples /// /// ```rust /// extern crate environment; /// /// use std::ffi::OsString; /// /// let e = environment::Environment::empty().insert("foo", "bar").compile(); /// assert_eq!(e, vec![(OsString::from("foo"), OsString::from("bar"))]); /// ``` pub fn insert, S2: Into>(mut self, key: S1, val: S2) -> Self { self.vars.push((key.into(), val.into())); self } /// Compile Environment object pub fn compile(self) -> Vec<(OsString, OsString)> { if self.inherit { ::std::env::vars_os().chain(self.vars).collect() } else { self.vars } } } /// Implicit clone for ergonomics impl<'a> From<&'a Environment> for Environment { fn from(v: &'a Environment) -> Self { v.clone() } } pub trait EnvironmentItem { fn to_environment_tuple(&self) -> (OsString, OsString); } impl EnvironmentItem for (T, Z) { fn to_environment_tuple(&self) -> (OsString, OsString) { ( OsString::from(self.0.to_string()), OsString::from(self.1.to_string()), ) } } impl<'s, T: ToString, Z: ToString> EnvironmentItem for &'s (T, Z) { fn to_environment_tuple(&self) -> (OsString, OsString) { ( OsString::from(self.0.to_string()), OsString::from(self.1.to_string()), ) } } impl<'s, T> From for Environment where T: IntoIterator, T::Item: EnvironmentItem, { fn from(v: T) -> Self { Self { vars: v.into_iter().map(|k| k.to_environment_tuple()).collect(), inherit: false, } } } #[cfg(test)] mod test { use super::*; use std::process::Command; #[test] fn take_ownership() { let x = Environment::inherit(); let mut c = Command::new("printenv"); c.env_clear().envs(x.clone().compile()).envs(x.compile()); } #[test] fn in_place_mod() { let y = Environment::empty(); let y = y.insert("key", "value"); assert_eq!( y.compile(), vec![(OsString::from("key"), OsString::from("value"))] ); } #[test] fn in_place_mod2() { let x = Environment::inherit(); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(x.insert("key", "value").insert("key", "vv").compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert!(output.contains("key=vv")); // Granted, `insert` moved `x`, so we can no longer reference it, even // though only a reference was passed to `envs` } #[test] fn in_place_mod3() { // In-place modification while allowing later accesses to the `Environment` let y = Environment::empty(); assert_eq!( y.clone().insert("key", "value").compile(), vec![(OsString::from("key"), OsString::from("value"))] ); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(y.compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert_eq!(output, ""); } #[test] fn empty_env() { // In-place modification while allowing later accesses to the `Environment` let y = Environment::empty(); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(y.compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert!(output.is_empty()); } #[test] fn take_vec() { let v = vec![("bar", "baz")]; let e: Environment = v.into(); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(e.clone().compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert!(output.contains("bar=baz")); let mut c = Command::new("printenv"); let output = c.env_clear() .envs(e.clone().insert("bar", "vv").compile()) .output() .expect("failed to execute command"); let output = String::from_utf8_lossy(&output.stdout); assert!(output.contains("bar=vv")); assert!(!output.contains("bar=baz")); } }