lexa_env/from_str.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX) ┃
3// ┃ SPDX-License-Identifier: MPL-2.0 ┃
4// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
5// ┃ ┃
6// ┃ This Source Code Form is subject to the terms of the Mozilla Public ┃
7// ┃ License, v. 2.0. If a copy of the MPL was not distributed with this ┃
8// ┃ file, You can obtain one at https://mozilla.org/MPL/2.0/. ┃
9// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
10
11use crate::{declaration, Error, Result};
12
13// -------- //
14// Fonction //
15// -------- //
16
17/// Dé-sérialise une chaîne vers un type.
18///
19/// Cette fonction va tenter d'interpréter `s` comme le contenu d'un document
20/// .env et de désérialiser `T` à partir de cette chaîne.
21pub fn from_str<T>(s: &str) -> Result<T>
22where
23 T: serde::de::DeserializeOwned,
24{
25 let decls = parse(s);
26 decls.for_each(|decl| decl.set_env());
27 // NOTE: La crate `serde_env` n'a pas exporté son type d'erreur 🤦♂️.
28 serde_env::from_env().map_err(|err| Error::Internal(err.to_string()))
29}
30
31#[inline]
32fn parse(input: &str) -> impl Iterator<Item = declaration::Declaration> + '_ {
33 input
34 .lines()
35 .filter_map(|line| declaration::Declaration::parse(line).ok())
36}