lexa_fs/extension.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 std::{fmt, str};
12
13// ----------- //
14// Énumération //
15// ----------- //
16
17#[derive(Debug)]
18#[derive(Copy, Clone)]
19#[derive(PartialEq, Eq)]
20/// Extensions de fichiers supportées par les fonctions [crate::load()] et
21/// [crate::load_or_prompt()].
22pub enum Extension
23{
24 /// Correspond aux extensions de fichiers `.local`, `.development`, `.test`
25 ENV,
26 /// Correspond à l'extension de fichier `.json`
27 JSON,
28 /// Correspond à l'extension de fichier `.toml`
29 TOML,
30 /// Correspond aux extensions de fichiers `.yml`, `.yaml`
31 YAML,
32}
33
34// -------------- //
35// Implémentation // -> Interface
36// -------------- //
37
38impl str::FromStr for Extension
39{
40 type Err = String;
41
42 fn from_str(extension: &str) -> Result<Self, Self::Err>
43 {
44 Ok(match extension {
45 // NOTE: fichier .env (.env.local, .env.development, .env.test)
46 | "" | "local" | "development" | "test" => Self::ENV,
47 | "json" => Self::JSON,
48 | "toml" => Self::TOML,
49 | "yml" | "yaml" => Self::YAML,
50 | _ => return Err(format!("L'extension de fichier « {extension} » n'est pas valide")),
51 })
52 }
53}
54
55impl fmt::Display for Extension
56{
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
58 {
59 let extension = match self {
60 | Extension::ENV => "env",
61 | Extension::JSON => "json",
62 | Extension::TOML => "toml",
63 | Extension::YAML => "yml",
64 };
65 write!(f, "{}", extension)
66 }
67}