#env-var #serialize-deserialize #serialization #deserialize #env #serde

serde-envfile

♻️ Deserialize and serialize environment variables

1 unstable release

0.1.0 Oct 27, 2023

#1480 in Encoding

Download history 561/week @ 2024-07-19 361/week @ 2024-07-26 317/week @ 2024-08-02 444/week @ 2024-08-09 337/week @ 2024-08-16 256/week @ 2024-08-23 356/week @ 2024-08-30 427/week @ 2024-09-06 144/week @ 2024-09-13 355/week @ 2024-09-20 223/week @ 2024-09-27 358/week @ 2024-10-04 387/week @ 2024-10-11 360/week @ 2024-10-18 248/week @ 2024-10-25 247/week @ 2024-11-01

1,418 downloads per month
Used in 2 crates

MIT OR Apache-2.0 OR EUPL-1.2

35KB
952 lines

serde-envfile

Built ontop the dotenvy and envy crates, serde-envfile supports both the serialization and the deserialization of environment variables from or to files (from_file, to_file), strings (from_str, to_string), or the environment of the application (from_env, to_env).

Install ☁️

Extend your Cargo.toml configuration file to include serde-envfile as a dependency or install the package with the Cargo package manager.

cargo add serde-envfile

Use 🔨

use serde::{Deserialize, Serialize};
use serde_envfile::{Error, from_str, to_string};

#[derive(Debug, Deserialize, Serialize)]
struct Test {
    hello: String,
}

fn main() -> Result<(), Error> {
    let env = "HELLO=\"WORLD\"";
    let test: Test = from_str(env)?;
    let env = to_string(&test)?;

    println!("{}", env);

    Ok(())
}

Introducing the Value type, serde-envfile, provides a more flexible approach to working with environment variables.

use serde_envfile::{to_string, Error, Value};

fn main() -> Result<(), Error> {
    let mut env = Value::new();
    env.insert("hello".into(), "world".into());
    let env = to_string(&env)?;

    println!("{}", env);

    Ok(())
}

Dependencies

~2–2.8MB
~47K SLoC