#serialization #deserialize #serde #env

serde-envfile

♻️ Deserialize and serialize environment variables

2 unstable releases

Uses new Rust 2024

0.2.0 Apr 4, 2025
0.1.0 Oct 27, 2023

#505 in Encoding

Download history 258/week @ 2025-01-22 172/week @ 2025-01-29 365/week @ 2025-02-05 141/week @ 2025-02-12 135/week @ 2025-02-19 261/week @ 2025-02-26 220/week @ 2025-03-05 117/week @ 2025-03-12 102/week @ 2025-03-19 197/week @ 2025-03-26 308/week @ 2025-04-02 95/week @ 2025-04-09 137/week @ 2025-04-16 65/week @ 2025-04-23 49/week @ 2025-04-30 95/week @ 2025-05-07

357 downloads per month
Used in 3 crates

MIT OR Apache-2.0 OR EUPL-1.2

34KB
912 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

Example

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

~0.4–1MB
~22K SLoC