use std::collections::BTreeMap;
pub fn parse_dotenv(text: &str) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
for line in text.lines() {
let t = line.trim();
if t.is_empty() || t.starts_with('#') {
continue;
}
let Some(eq) = t.find('=') else { continue };
let key = t[..eq].trim().to_string();
let mut value = t[eq + 1..].trim().to_string();
let quoted = (value.starts_with('"') && value.ends_with('"') && value.len() >= 2)
|| (value.starts_with('\'') && value.ends_with('\'') && value.len() >= 2);
if quoted {
value = value[1..value.len() - 1].to_string();
}
out.insert(key, value);
}
out
}
pub fn render_dotenv(entries: &BTreeMap<String, String>) -> String {
let mut text = entries
.iter()
.map(|(k, v)| format!("{k}={}", serde_json::to_string(v).unwrap()))
.collect::<Vec<_>>()
.join("\n");
if !entries.is_empty() {
text.push('\n');
}
text
}