git.delta.rocks / jrsonnet / refs/commits / e98be8b771cf

difftreelog

Merge pull request #122 from expelledboy/master

Yaroslav Bolyukin2023-07-13parents: #777cdf5 #9872fc2.patch.diff
in: master
Allow '=' within the body of --tla-code

1 file changed

modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
10 pub value: String,10 pub value: String,
11}11}
1212
13/// Parses a string like `name=<value>`, or `name` and reads value from env variable.
14/// With no value it will be read from env variable.
15/// If env variable is not found then it will be an error.
16/// Value can contain `=` symbol.
17///
18/// ```
19/// use std::str::FromStr;
20/// use jrsonnet_cli::ExtStr;
21///
22/// let ext = ExtStr::from_str("name=value").unwrap();
23/// assert_eq!(ext.name, "name");
24/// assert_eq!(ext.value, "value");
25///
26/// std::env::set_var("name", "value");
27///
28/// let ext = ExtStr::from_str("name").unwrap();
29/// assert_eq!(ext.name, "name");
30/// assert_eq!(ext.value, "value");
31///
32/// let ext = ExtStr::from_str("name=value=with=equals").unwrap();
33/// assert_eq!(ext.name, "name");
34/// assert_eq!(ext.value, "value=with=equals");
35/// ```
36///
13impl FromStr for ExtStr {37impl FromStr for ExtStr {
14 type Err = &'static str;38 type Err = &'static str;
39
15 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {40 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
16 let out: Vec<_> = s.split('=').collect();
17 match out.len() {41 match s.find('=') {
18 1 => Ok(ExtStr {42 Some(idx) => Ok(ExtStr {
19 name: out[0].to_owned(),43 name: s[..idx].to_owned(),
20 value: std::env::var(out[0]).or(Err("missing env var"))?,44 value: s[idx + 1..].to_owned(),
21 }),45 }),
22 2 => Ok(ExtStr {46 None => Ok(ExtStr {
23 name: out[0].to_owned(),47 name: s.to_owned(),
24 value: out[1].to_owned(),48 value: std::env::var(s).or(Err("missing env var"))?,
25 }),49 }),
26
27 _ => Err("bad ext-str syntax"),
28 }50 }
29 }51 }
30}52}