Code Monkey home page Code Monkey logo

envmnt's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar indietyp avatar joseluisq avatar sagiegurari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

envmnt's Issues

bulk apis

support operations on vec of env vars.

`.expand()` not working with `"$VAR1:$VAR2"`

Describe The Bug

.expand() doesn't work if variables wrap :.

To Reproduce

Run code below.

Code Sample

use envmnt::{ExpandOptions, ExpansionType};
use std::env;

#[test]
fn not_expanded_with_var_colon_var() {
    env::set_var("ASDF", "asdf");
    env::set_var("QWER", "qwer");

    let options = ExpandOptions {
        expansion_type: Some(ExpansionType::Unix),
        default_to_empty: true,
    };
    let expanded = envmnt::expand("$ASDF:$QWER", Some(options));

    assert_eq!(expanded, "asdf:qwer");
}

Error Stack

$ cargo test

running 1 test
test test::not_expanded_with_var_colon_var ... FAILED

failures:

---- test::not_expanded_with_var_colon_var stdout ----
thread 'test::not_expanded_with_var_colon_var' panicked at 'assertion failed: `(left == right)`
  left: `""`,
 right: `"asdf:qwer"`', src/lib.rs:13:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Thank you for your hard work!!!

Flatten and impl std::error::Error for EnvmntError

Feature Description

Have EnvmntError be more docile when matching or chaining against it from downstream. For chaining implementing std::error::Error is necessary.

Describe The Solution You'd Like

Move ErrorKind member into EnvmntError, making the latter an enumeration. Implement std::error::Error for it.

Side Effects

Reduction of code for anything depending on matching against EnvmntError.kind.

get_or_panic

get the env var value or panic if value doesn't exist.

Get and parse to a generic T inferred from return T

Feature Description

A function to access and parse into any type T or error out.

Describe The Solution You'd Like

A function envmnt::get<T>() -> Result<T, ?> where T: FromStr<T>. This way a environment variable can be gotten and parsed by its return type, or gracefully error out.

Code Sample

pub fn get<T, E>(key: &'static str) -> EnvmtResult<T>
    where T: std::str::FromStr + std::str::FromStr<Err=E>,
          E: std::fmt::Display
{
    let var = std::env::var(key)
        .map_err(|e|
            EnvmtError::Missing(format!("Failed to load environment variable: '{}' ({})", name, e))
        )?;

    let val = var.parse::<T>()
        .map_err(|e|
            EnvmtError::InvalidType(
                format!("Failed to parse environment variable: '{}' to type '{}' ({})", name, stringify!(T), e)
            )
        )?;

    Ok(val)
}

Collateral Effects

For instance numeric.rs can be simplified by this, since this generic specializes amongst other things into all its variants.

Motivation

I'm currently carrying this boiler plate around project after project, so I figured it might be useful to upstream it into an existing env utilities crate like yours. Would you accept a pull request for something like this?

Env expansion function

Features Description
Allow for manual env expansion

Describe the solution you'd like
A new function that would get the env value and expansion type enum (OS default, windows, unix or unix subset) and return the updated value after expansion.

Code Sample

let expanded_value = envmnt::expand("${MY_ENV}_123", ExpansionType::OS);

Allow for default values in expansion syntax

Features Description
The exposure I have to this crate leads me to believe that either the default syntax isn't documented, or it doesn't support default values.

Describe the solution you'd like
Today, I can expand things like ${MY_VAR}, but I would like to be able to supply defaults, e.g. ${MY_VAR:default_value} so that if the environment variable isn't present, it uses a default value that's supplied within the text being expanded.

Environment expansion working differently in 0.10.0 and 0.10.2

Describe The Bug

In the version 0.10.0 if I use ${ADDRESS:http://127.0.0.1:1234} and the env variable is not defined it was returning http://127.0.0.1:1234 in the version 0.10.2 this doesn't work and it returns the same ${ADDRESS:http://127.0.0.1:1234}

`.expand()` not working with `/`

Describe The Bug

.expand() not working with /.

To Reproduce

Run code below.

Code Sample

#[test]
fn not_expanded_with_slash() {
    env::set_var("EDITOR", "nvim");

    let options = ExpandOptions {
        expansion_type: Some(ExpansionType::Unix),
        default_to_empty: true,
    };
    let expanded = envmnt::expand("$EDITOR/asdf", Some(options));

    assert_eq!("nvim/asdf", expanded);
}

Error Stack

$ cargo test

running 1 test
test not_expanded_with_slash ... FAILED

failures:

---- not_expanded_with_slash stdout ----
thread 'not_expanded_with_slash' panicked at 'assertion failed: `(left == right)`
  left: `"nvim/asdf"`,
 right: `""`', src/main.rs:15:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Add support for environment variables with double quotes wrapper

Describe the bug

In v0.8.2 when an environment variable value contains double quotes those are not evaluated by the library, instead the whole value is returned including the double quotes wrapper.

To Reproduce
Just create an environment variable and wrap its value with double quotes like LOG_LEVEL="info" and just run the code example below.

Code Sample

#[test]
fn env_file_quoted() {
    let output = envmnt::load_file("load.env");

    assert!(output.is_ok());

    // Panic: `LOG_LEVEL` returns ""info"" and not "info"
    assert_eq!(envmnt::get_or_panic("LOG_LEVEL"), "info");
}

Error Stack

$ cargo test

running 1 test
test env_file_quoted ... FAILED

failures:

---- env_file_quoted stdout ----
thread 'env_file_quoted' panicked at 'assertion failed: `(left == right)`
  left: `"\"info\""`,
 right: `"info"`', src/main.rs:12:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.