use crate::Result;
type LValue<'a> = &'a str;
type RValue<'a> = &'a str;
#[derive(Debug)]
#[derive(PartialEq, Eq)]
pub(crate) struct Declaration<'a>(LValue<'a>, RValue<'a>);
impl<'a> Declaration<'a> {
pub(crate) fn parse<'b: 'a>(input: &'b str) -> Result<Self> {
input
.split_once('=')
.filter(|(key, _)| key.to_ascii_uppercase().eq(key))
.map(|(key, value)| Self(key, value))
.ok_or(crate::Error::ParseLineDeclaration)
}
pub(crate) fn set_env(&self) {
let Self(key, value) = self;
std::env::set_var(key, value);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_empty_value() {
let decl = Declaration::parse("A=").expect("?");
assert_eq!(decl.0, "A");
assert_eq!(decl.1, "");
}
#[test]
fn test_parse_key_value() {
let decl = Declaration::parse("A=B").expect("?");
assert_eq!(decl.0, "A");
assert_eq!(decl.1, "B");
}
#[test]
fn test_parse_multiple_eq_sign() {
let decl = Declaration::parse("A=B=C=D").expect("?");
assert_eq!(decl.0, "A");
assert_eq!(decl.1, "B=C=D");
}
}