From e98be8b771cf81f770bff2f4a4c63e8eaba2ae10 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 13 Jul 2023 11:57:52 +0000 Subject: [PATCH] Merge pull request #122 from expelledboy/master Allow '=' within the body of --tla-code --- --- a/crates/jrsonnet-cli/src/stdlib.rs +++ b/crates/jrsonnet-cli/src/stdlib.rs @@ -10,21 +10,43 @@ pub value: String, } +/// Parses a string like `name=`, or `name` and reads value from env variable. +/// With no value it will be read from env variable. +/// If env variable is not found then it will be an error. +/// Value can contain `=` symbol. +/// +/// ``` +/// use std::str::FromStr; +/// use jrsonnet_cli::ExtStr; +/// +/// let ext = ExtStr::from_str("name=value").unwrap(); +/// assert_eq!(ext.name, "name"); +/// assert_eq!(ext.value, "value"); +/// +/// std::env::set_var("name", "value"); +/// +/// let ext = ExtStr::from_str("name").unwrap(); +/// assert_eq!(ext.name, "name"); +/// assert_eq!(ext.value, "value"); +/// +/// let ext = ExtStr::from_str("name=value=with=equals").unwrap(); +/// assert_eq!(ext.name, "name"); +/// assert_eq!(ext.value, "value=with=equals"); +/// ``` +/// impl FromStr for ExtStr { type Err = &'static str; + fn from_str(s: &str) -> std::result::Result { - let out: Vec<_> = s.split('=').collect(); - match out.len() { - 1 => Ok(ExtStr { - name: out[0].to_owned(), - value: std::env::var(out[0]).or(Err("missing env var"))?, + match s.find('=') { + Some(idx) => Ok(ExtStr { + name: s[..idx].to_owned(), + value: s[idx + 1..].to_owned(), }), - 2 => Ok(ExtStr { - name: out[0].to_owned(), - value: out[1].to_owned(), + None => Ok(ExtStr { + name: s.to_owned(), + value: std::env::var(s).or(Err("missing env var"))?, }), - - _ => Err("bad ext-str syntax"), } } } -- gitstuff