1use std::str::FromStr;23use clap::Parser;4use jrsonnet_evaluator::tla::TlaArg;5use jrsonnet_evaluator::{trace::PathResolver, Result};6use jrsonnet_stdlib::ContextInitializer;78#[derive(Clone)]9pub struct ExtStr {10 pub name: String,11 pub value: String,12}1314151617181920212223242526272829303132333435363738impl FromStr for ExtStr {39 type Err = &'static str;4041 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {42 match s.find('=') {43 Some(idx) => Ok(Self {44 name: s[..idx].to_owned(),45 value: s[idx + 1..].to_owned(),46 }),47 None => Ok(Self {48 name: s.to_owned(),49 value: std::env::var(s).or(Err("missing env var"))?,50 }),51 }52 }53}5455#[derive(Clone)]56pub struct ExtFile {57 pub name: String,58 pub path: String,59}6061impl FromStr for ExtFile {62 type Err = String;6364 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {65 let Some((name, path)) = s.split_once('=') else {66 return Err("bad ext-file syntax".to_owned());67 };68 Ok(Self {69 name: name.into(),70 path: path.into(),71 })72 }73}7475#[derive(Parser)]76#[clap(next_help_heading = "STANDARD LIBRARY")]77pub struct StdOpts {78 79 80 #[clap(long)]81 no_stdlib: bool,82 83 84 85 86 87 #[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]88 ext_str: Vec<ExtStr>,89 90 91 #[clap(long, name = "name=var path", number_of_values = 1)]92 ext_str_file: Vec<ExtFile>,93 94 95 #[clap(long, name = "name[=var source]", number_of_values = 1)]96 ext_code: Vec<ExtStr>,97 98 99 #[clap(long, name = "name=var code path", number_of_values = 1)]100 ext_code_file: Vec<ExtFile>,101}102impl StdOpts {103 pub fn context_initializer(&self) -> Result<Option<ContextInitializer>> {104 if self.no_stdlib {105 return Ok(None);106 }107 let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());108 for ext in &self.ext_str {109 ctx.settings_mut().ext_vars.insert(110 ext.name.as_str().into(),111 TlaArg::String(ext.value.as_str().into()),112 );113 }114 for ext in &self.ext_str_file {115 ctx.settings_mut().ext_vars.insert(116 ext.name.as_str().into(),117 TlaArg::ImportStr(ext.path.clone()),118 );119 }120 for ext in &self.ext_code {121 ctx.settings_mut().ext_vars.insert(122 ext.name.as_str().into(),123 TlaArg::InlineCode(ext.value.clone()),124 );125 }126 for ext in &self.ext_code_file {127 ctx.settings_mut()128 .ext_vars129 .insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));130 }131 Ok(Some(ctx))132 }133}