Skip to content

Commit

Permalink
Update documentation with option example
Browse files Browse the repository at this point in the history
  • Loading branch information
dariocurr committed Sep 3, 2023
1 parent ad4888a commit 76d695a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ When you add the `EnvSettings` derive to a `struct`, two methods are added to it

```sh
export name=paolo
export age=42
export favourite_number=42
```

```rs
Expand All @@ -41,25 +41,25 @@ use env_settings_derive::EnvSettings;
struct MyStruct {
name: String,

age: u8,
favourite_number: u8,
}

fn main() {
let my_struct = MyStruct::from_env().unwrap();
assert_eq!(my_struct.name, "paolo".to_string());
assert_eq!(my_struct.age, 42);
assert_eq!(my_struct.favourite_number, 42);

let name = "luca";
let my_struct = MyStruct::new(Some(name.to_string()), None).unwrap();
assert_eq!(my_struct.name, name);
assert_eq!(my_struct.age, 42);
assert_eq!(my_struct.favourite_number, 42);
}
```

### Advanced

```sh
echo "MY_STRUCT_AGE=42\n" > .env
echo "MY_STRUCT_FAVOURITE_NUMBER=42\n" > .env
export MY_BIRTH_DATE=01/01/1970
```

Expand All @@ -72,23 +72,32 @@ struct MyStruct {
#[env_settings(default = "paolo")]
name: String,

age: u8,
favourite_number: u8,

#[env_settings(variable = "MY_BIRTH_DATE")]
birth_date: String,

birth_place: Option<String>
}

fn main() {
let my_struct = MyStruct::from_env().unwrap();
assert_eq!(my_struct.name, "paolo".to_string());
assert_eq!(my_struct.age, 42);
assert_eq!(my_struct.favourite_number, 42);
assert_eq!(my_struct.birth_date, "01/01/1970");
assert_eq!(my_struct.birth_place, None);

let name = "luca";
let my_struct = MyStruct::new(Some(name.to_string()), None, None).unwrap();
let my_struct = MyStruct::new(
Some(name.to_string()),
None,
None,
Some("london".to_string())
).unwrap();
assert_eq!(my_struct.name, name);
assert_eq!(my_struct.age, 42);
assert_eq!(my_struct.favourite_number, 42);
assert_eq!(my_struct.birth_date, "01/01/1970");
assert_eq!(my_struct.birth_place, Some("london".to_string()));
}
```

Expand Down
32 changes: 21 additions & 11 deletions env-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
//! ```rust
//! // `export name=paolo` in shell or
//! std::env::set_var("name", "paolo");
//! // `export age=42` in shell or
//! std::env::set_var("age", "42");
//! // `export favourite_number=42` in shell or
//! std::env::set_var("favourite_number", "42");
//!
//!
//! use env_settings_derive::EnvSettings;
Expand All @@ -51,27 +51,27 @@
//! struct MyStruct {
//! name: String,
//!
//! age: u8,
//! favourite_number: u8,
//! }
//!
//! let my_struct = MyStruct::from_env().unwrap();
//! assert_eq!(my_struct.name, "paolo".to_string());
//! assert_eq!(my_struct.age, 42);
//! assert_eq!(my_struct.favourite_number, 42);
//!
//! let name = "luca";
//! let my_struct = MyStruct::new(Some(name.to_string()), None).unwrap();
//! assert_eq!(my_struct.name, name);
//! assert_eq!(my_struct.age, 42);
//! assert_eq!(my_struct.favourite_number, 42);
//! ```
//!
//! ### Advanced
//!
//! ```rust
//! use std::io::prelude::Write;
//!
//! // `echo "MY_STRUCT_AGE=42\n" > .env` in shell or
//! // `echo "MY_STRUCT_FAVOURITE_NUMBER=42\n" > .env` in shell or
//! let mut env_file = std::fs::File::create(".env").unwrap();
//! env_file.write_all("MY_STRUCT_AGE=42\n".as_bytes()).unwrap();
//! env_file.write_all("MY_STRUCT_FAVOURITE_NUMBER=42\n".as_bytes()).unwrap();
//! // `export MY_BIRTH_DATE=01/01/1970` in shell or
//! std::env::set_var("MY_BIRTH_DATE", "01/01/1970");
//!
Expand All @@ -85,22 +85,31 @@
//! #[env_settings(default = "paolo")]
//! name: String,
//!
//! age: u8,
//! favourite_number: u8,
//!
//! #[env_settings(variable = "MY_BIRTH_DATE")]
//! birth_date: String,
//!
//! birth_place: Option<String>
//! }
//!
//! let my_struct = MyStruct::from_env().unwrap();
//! assert_eq!(my_struct.name, "paolo".to_string());
//! assert_eq!(my_struct.age, 42);
//! assert_eq!(my_struct.favourite_number, 42);
//! assert_eq!(my_struct.birth_date, "01/01/1970");
//! assert_eq!(my_struct.birth_place, None);
//!
//! let name = "luca";
//! let my_struct = MyStruct::new(Some(name.to_string()), None, None).unwrap();
//! let my_struct = MyStruct::new(
//! Some(name.to_string()),
//! None,
//! None,
//! Some("london".to_string())
//! ).unwrap();
//! assert_eq!(my_struct.name, name);
//! assert_eq!(my_struct.age, 42);
//! assert_eq!(my_struct.favourite_number, 42);
//! assert_eq!(my_struct.birth_date, "01/01/1970");
//! assert_eq!(my_struct.birth_place, Some("london".to_string()));
//! ```
//!
//! ### Parameters
Expand Down Expand Up @@ -128,5 +137,6 @@
//! 3. Variables loaded from a file (e.g. `.env`)
//! 4. Default values
//!
/// The trait to add to the derive
pub trait EnvSettings {}

0 comments on commit 76d695a

Please sign in to comment.