Crate serde_envfile

source ·
Expand description

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).

Examples

Note that keys are transformed to lowercase during deserialization. With serialization, the contrary is the case.

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, also 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(())
}

Structs

  • Helper structure to work with prefixes more efficiently. Instantiable through prefixed.
  • A serializer to transform Rust data into environment variables.
  • Flexible representation of environment variables.

Enums

Functions

  • Deserialize program-available environment variables into an instance of type T.
  • Deserialize an environment variable file into an instance of type T.
  • Deserialize environment variables from a string into an instance of type T.
  • Instantiates Prefixed from which values can be both serialized and deserialized.
  • Serialize data into the environment of the application running.
  • Serialize data into an environment variable file.
  • Serialize data into an environment variable string.