Skip to content

Commit 5017043

Browse files
feat(dotenv): Adding support for dotenv files
1 parent bf6e256 commit 5017043

File tree

8 files changed

+377
-0
lines changed

8 files changed

+377
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ yaml = ["yaml-rust2"]
127127
ini = ["rust-ini"]
128128
json5 = ["json5_rs", "dep:serde-untagged"]
129129
corn = ["dep:corn"]
130+
dotenv = ["dep:dotenvy"]
130131
convert-case = ["convert_case"]
131132
preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"]
132133
async = ["async-trait"]
@@ -143,6 +144,7 @@ rust-ini = { version = "0.21.3", optional = true }
143144
ron = { version = "0.8.1", optional = true }
144145
json5_rs = { version = "0.4.1", optional = true, package = "json5" }
145146
corn = { version = "0.10.0", optional = true, package = "libcorn" }
147+
dotenvy = { version = "0.15.7", optional = true }
146148
indexmap = { version = "2.11.4", features = ["serde"], optional = true }
147149
convert_case = { version = "0.6.0", optional = true }
148150
pathdiff = "0.2.3"

src/file/format/dotenv.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::error::Error;
2+
use std::io::Cursor;
3+
4+
use crate::map::Map;
5+
use crate::value::{Value, ValueKind};
6+
7+
pub(crate) fn parse(
8+
uri: Option<&String>,
9+
text: &str,
10+
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
11+
let mut map: Map<String, Value> = Map::new();
12+
let str_iter = dotenvy::Iter::new(Cursor::new(text));
13+
for item in str_iter {
14+
let (key, value) = item?;
15+
map.insert(key, Value::new(uri, ValueKind::String(value)));
16+
}
17+
18+
Ok(map)
19+
}

src/file/format/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ mod json5;
2424
#[cfg(feature = "corn")]
2525
mod corn;
2626

27+
#[cfg(feature = "dotenv")]
28+
mod dotenv;
29+
2730
/// File formats provided by the library.
2831
///
2932
/// Although it is possible to define custom formats using [`Format`] trait it is recommended to use `FileFormat` if possible.
@@ -57,6 +60,10 @@ pub enum FileFormat {
5760
/// Corn (parsed with `libcorn`)
5861
#[cfg(feature = "corn")]
5962
Corn,
63+
64+
/// Dotenv (parsed with `dotenvy`)
65+
#[cfg(feature = "dotenv")]
66+
Dotenv,
6067
}
6168

6269
impl FileFormat {
@@ -76,6 +83,8 @@ impl FileFormat {
7683
FileFormat::Json5,
7784
#[cfg(feature = "corn")]
7885
FileFormat::Corn,
86+
#[cfg(feature = "dotenv")]
87+
FileFormat::Dotenv,
7988
]
8089
}
8190

@@ -102,13 +111,17 @@ impl FileFormat {
102111
#[cfg(feature = "corn")]
103112
FileFormat::Corn => &["corn"],
104113

114+
#[cfg(feature = "dotenv")]
115+
FileFormat::Dotenv => &["dotenv"],
116+
105117
#[cfg(all(
106118
not(feature = "toml"),
107119
not(feature = "json"),
108120
not(feature = "yaml"),
109121
not(feature = "ini"),
110122
not(feature = "ron"),
111123
not(feature = "json5"),
124+
not(feature = "dotenv"),
112125
))]
113126
_ => unreachable!("No features are enabled, this library won't work without features"),
114127
}
@@ -141,13 +154,17 @@ impl FileFormat {
141154
#[cfg(feature = "corn")]
142155
FileFormat::Corn => corn::parse(uri, text),
143156

157+
#[cfg(feature = "dotenv")]
158+
FileFormat::Dotenv => dotenv::parse(uri, text),
159+
144160
#[cfg(all(
145161
not(feature = "toml"),
146162
not(feature = "json"),
147163
not(feature = "yaml"),
148164
not(feature = "ini"),
149165
not(feature = "ron"),
150166
not(feature = "json5"),
167+
not(feature = "dotenv"),
151168
))]
152169
_ => unreachable!("No features are enabled, this library won't work without features"),
153170
}

tests/testsuite/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foobar="I am FOOBAR envfile"
2+
foo="I am foo envfile"
3+
bar="I am BAR envfile"

tests/testsuite/.env.local

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foobar="I am FOOBAR envfile local"
2+
foo="I am foo envfile local"
3+
bar="I am BAR envfile local"

0 commit comments

Comments
 (0)