-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
100 lines (87 loc) · 2.41 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! # serde-envfile
//! Built ontop the [`dotenvy`](https://github.com/allan2/dotenvy) and
//! [`envy`](https://github.com/softprops/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.
//! ```no_run
//! 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.
//! ```no_run
//! 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(())
//! }
//! ```
#[doc(hidden)]
pub mod de;
pub(crate) mod error;
pub(crate) mod log;
pub(crate) mod prefixed;
pub(crate) mod ser;
pub(crate) mod value;
pub use error::Error;
pub use de::from_env;
pub use de::from_file;
pub use de::from_str;
pub use ser::to_env;
pub use ser::to_file;
pub use ser::to_string;
pub use ser::Serializer;
pub use value::Value;
pub use prefixed::prefixed;
pub use prefixed::Prefixed;
#[cfg(test)]
mod tests {
use crate::{from_str, to_string, Value};
use serde::{Deserialize, Serialize};
#[test]
fn basic_main() {
#[derive(Debug, Deserialize, Serialize)]
struct Test {
hello: String,
}
let de = "HELLO=\"WORLD\"";
let test: Test = from_str(de).unwrap();
let ser = to_string(&test).unwrap();
assert_eq!(de, ser)
}
#[test]
fn value_main() {
let mut env = Value::new();
env.insert("hello".into(), "world".into());
let ser = to_string(&env).unwrap();
let de: Value = from_str(&ser).unwrap();
assert_eq!(env, de);
}
}