hyper_scripter/
env_pair.rs1use crate::error::{DisplayError, DisplayResult, FormatCode::EnvPair as EnvPairCode};
2use crate::util::{impl_de_by_from_str, impl_ser_by_to_string};
3use std::str::FromStr;
4
5#[derive(Display, Debug, Clone, Eq, PartialEq)]
6#[display(fmt = "{}={}", key, val)]
7pub struct EnvPair {
8 pub key: String,
9 pub val: String,
10}
11impl_ser_by_to_string!(EnvPair);
12impl_de_by_from_str!(EnvPair);
13
14impl EnvPair {
15 pub fn process_line(line: &str, env_vec: &mut Vec<Self>) {
17 let env = line.split_whitespace().next().unwrap();
18 if env_vec.iter().find(|p| env == p.key).is_some() {
19 } else if let Ok(val) = std::env::var(env) {
21 env_vec.push(EnvPair {
22 key: env.to_owned(),
23 val,
24 });
25 }
26 }
27 pub fn sort(v: &mut Vec<Self>) {
28 v.sort_by(|a, b| a.key.cmp(&b.key));
29 }
30}
31impl FromStr for EnvPair {
32 type Err = DisplayError;
33 fn from_str(s: &str) -> DisplayResult<Self> {
34 if let Some((key, val)) = s.split_once('=') {
35 Ok(EnvPair {
36 key: key.to_owned(),
37 val: val.to_owned(),
38 })
39 } else {
40 EnvPairCode.to_display_res(s.to_owned())
41 }
42 }
43}