Crates.io | serde-envfile |
lib.rs | serde-envfile |
version | 0.1.0 |
source | src |
created_at | 2023-10-27 19:10:36.934713 |
updated_at | 2023-10-27 19:10:36.934713 |
description | ♻️ Deserialize and serialize environment variables. |
homepage | |
repository | https://github.com/lucagoslar/serde-envfile |
max_upload_size | |
id | 1016366 |
size | 62,472 |
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
).
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 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(())
}