--- a/crates/jrsonnet-cli/src/stdlib.rs +++ b/crates/jrsonnet-cli/src/stdlib.rs @@ -10,8 +10,33 @@ 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 { match s.find('=') { Some(idx) => Ok(ExtStr {