Giter Club home page Giter Club logo

stupid_simple_dotenv's Introduction

Stupid simple dotenv

A simple dotenv parser for Rust with no dependencies.

Reads key value pairs from an .env or any other file and stores them as easily available environment variables. Since dotenv is no longer maintained, this is an simpler smaller alternative.

Usage

use stupid_simple_dotenv;
fn main() {
    stupid_simple_dotenv::to_env().ok();
    println!("Hello, {}!", std::env::var("myuser").unwrap()); // Hello, world!

    stupid_simple_dotenv::file_to_env("other.env").ok();
    println!(
        "Hello, {}!",
        stupid_simple_dotenv::get_or("other_user", "Not set")
    ); // Hello, other user name!

    println!(
        "Hello, {}!",
        stupid_simple_dotenv::get_or("other_user_not_set", "not set")
    ); // Hello, not set!

    let list = stupid_simple_dotenv::file_to_vec("other.env").unwrap();
    let other_user_name = list.iter().find(|(key, _value)| key == "other_user");
    if let Some((_, value)) = other_user_name {
        println!("Hello, {}!", value); // Hello, other user name!
    }
}

Valid .env file

.env files are simply configuration files in which a key-value pair separated by a = character is specified per line.
The keys are to be specified without quotes.
The values can be specified also without, however, are permissible also: ", ' and ` as quotes.
Comments are to be introduced with # and must stand in a new line. Comments after values are not recognized and interpreted as values. Spaces before or after keys and values are ignored. If they are intended, quotes must be used.
Valid lines of an .env file for this parser are:

myuser = world
other_user = other user name
key0=value
 key1= value
key2=value
key3='value'
key4=`value`
key5=` value with spaces `
 #comment
key6 = value with spaces inside
key 7= value:with#special&😀UTF-8 Chars🦀
key-8="value:with#special&😀UTF-8 Chars🦀and_quote"
key9="comments now enabled in the same Line" #this is a comment and will be ignored

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

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.