lexa-env 0.1.2

Récupère tes variables d'environnement simplement.
Documentation
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX)         ┃
// ┃ SPDX-License-Identifier: MPL-2.0                                          ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃                                                                           ┃
// ┃  This Source Code Form is subject to the terms of the Mozilla Public      ┃
// ┃  License, v. 2.0. If a copy of the MPL was not distributed with this      ┃
// ┃  file, You can obtain one at https://mozilla.org/MPL/2.0/.                ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use crate::{declaration, Error, Result};

// -------- //
// Fonction //
// -------- //

/// Dé-sérialise une chaîne vers un type.
///
/// Cette fonction va tenter d'interpréter `s` comme le contenu d'un document
/// .env et de désérialiser `T` à partir de cette chaîne.
pub fn from_str<T>(s: &str) -> Result<T>
where
	T: serde::de::DeserializeOwned,
{
	let decls = parse(s);
	decls.for_each(|decl| decl.set_env());
	// NOTE: La crate `serde_env` n'a pas exporté son type d'erreur 🤦‍♂️.
	serde_env::from_env().map_err(|err| Error::Internal(err.to_string()))
}

#[inline]
fn parse(input: &str) -> impl Iterator<Item = declaration::Declaration> + '_ {
	input
		.lines()
		.filter_map(|line| declaration::Declaration::parse(line).ok())
}