Skip to main content

itconfig/
lib.rs

1//! # itconfig
2//!
3//! Simple configuration with macro for rust application.
4//!
5//!
6//! ## Motivation
7//!
8//! I began to use rust with web programming experience where environment variables are widely used
9//! and often there are more then 50 of them. First I looked at already created libraries.
10//! But there it's necessary to initialise structure that needs to be moved to each function
11//! where you need variable. It uses little bit memory, but configuration lifetime is as long
12//! as application lifetime. Because of it I decided to create my own library.
13//!
14//!
15//! ## Installation
16//!
17//! These macros require a Rust compiler version 1.31 or newer.
18//!
19//! Add `itconfig = { version = "1.0", features = ["macro"] }` as a dependency in `Cargo.toml`.
20//!
21//!
22//! `Cargo.toml` example:
23//!
24//! ```toml
25//! [package]
26//! name = "my-crate"
27//! version = "0.1.0"
28//! authors = ["Me <user@rust-lang.org>"]
29//!
30//! [dependencies]
31//! itconfig = { version = "1.0", features = ["macro"] }
32//! ```
33//!
34//!
35//! ## Basic usage
36//!
37//! ```rust
38//! use itconfig::config;
39//! use std::env;
40//! //use dotenv::dotenv;
41//!
42//! config! {
43//!     DEBUG: bool => false,
44//!
45//!     #[env_name = "APP_HOST"]
46//!     HOST: String => "127.0.0.1",
47//!
48//!     database {
49//!         URL < (
50//!             "postgres://",
51//!             POSTGRES_USERNAME => "user",
52//!             ":",
53//!             POSTGRES_PASSWORD => "pass",
54//!             "@",
55//!             POSTGRES_HOST => "localhost:5432",
56//!             "/",
57//!             POSTGRES_DB => "test",
58//!         ),
59//!
60//!         pool {
61//!             MAX_SIZE: usize => 15,
62//!         },
63//!     },
64//!
65//!     sentry {
66//!         DSN: Option<&'static str>,
67//!     },
68//!
69//!     feature {
70//!         static CORS: bool => false,
71//!
72//!         static GRAPHQL_PLAYGROUND: bool => false,
73//!     },
74//! }
75//!
76//! fn main () {
77//!     // dotenv().expect("dotenv setup to be successful");
78//!     // or
79//!     env::set_var("FEATURE_CORS", "true");
80//!
81//!     config::init();
82//!     assert_eq!(config::HOST(), String::from("127.0.0.1"));
83//!     assert_eq!(config::database::URL(), String::from("postgres://user:pass@localhost:5432/test"));
84//!     assert_eq!(config::database::pool::MAX_SIZE(), 15);
85//!     assert_eq!(config::sentry::DSN(), None);
86//!     assert_eq!(config::feature::CORS(), true);
87//! }
88//! ```
89//!
90//! Macro is an optional feature, disabled by default. You can use this library without macro.
91//!
92//! ```rust
93//! use itconfig::*;
94//! use std::env;
95//! // use dotenv::dotenv;
96//!
97//! fn main() {
98//!     // dotenv().expect("dotenv setup to be successful");
99//!     // or
100//!     env::set_var("DATABASE_URL", "postgres://127.0.0.1:5432/test");
101//!
102//!     let database_url = get_env::<String>("DATABASE_URL").unwrap();
103//!     let new_profile: bool = get_env_or_default("FEATURE_NEW_PROFILE", false);
104//!     let articles_per_page: u32 = get_env_or_set_default("ARTICLES_PER_PAGE", 10);
105//! }
106//! ```
107//!
108//! ## Available features
109//!
110//! * **default** - ["primitives"]
111//! * **macro** - Activates `config!` macros for easy configure web application.
112//! * **primitives** - Group for features: `numbers` and `bool`.
113//! * **numbers** - Group for features: `int`, `uint` and `float`.
114//! * **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`.
115//! * **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`.
116//! * **float** - Group for features: `f32` and `f64`
117//! * **i8** - impl EnvString for `i8` type
118//! * **i16** - impl EnvString for `i16` type
119//! * **i32** - impl EnvString for `i32` type
120//! * **i64** - impl EnvString for `i64` type
121//! * **i128** - impl EnvString for `i128` type
122//! * **isize** - impl EnvString for `isize` type
123//! * **u8** - impl EnvString for `u8` type
124//! * **u16** - impl EnvString for `u16` type
125//! * **u32** - impl EnvString for `u32` type
126//! * **u64** - impl EnvString for `u64` type
127//! * **u128** - impl EnvString for `u128` type
128//! * **usize** - impl EnvString for `usize` type
129//! * **f32** - impl EnvString for `f32` type
130//! * **f64** - impl EnvString for `f64` type
131//! * **bool** - impl EnvString for `bool` type
132//! * **json_array** - Add EnvString impl for vector type (uses optional `serde_json` package). ⚠ **_DEPRECATED_**
133//!
134
135// Rustc lints.
136#![forbid(unsafe_code)]
137#![forbid(non_ascii_idents)]
138#![deny(
139    missing_debug_implementations,
140    missing_docs,
141    unstable_features,
142    unused_imports,
143    unused_qualifications
144)]
145// Clippy lints
146#![deny(clippy::all)]
147#![allow(clippy::needless_doctest_main)]
148
149/////////////////////////////////////////////////////////////////////////////
150
151mod envstr;
152mod error;
153mod get_env;
154mod get_vec_env;
155pub(crate) mod utils;
156
157pub use self::envstr::*;
158pub use self::error::*;
159pub use self::get_env::*;
160pub use self::get_vec_env::*;
161
162#[cfg(feature = "macro")]
163extern crate itconfig_macro;
164#[cfg(feature = "macro")]
165pub use itconfig_macro::*;