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

difftreelog

source

crates/jrsonnet-cli/src/stdlib.rs3.6 KiBsourcehistory
1use std::str::FromStr;23use clap::Parser;4use jrsonnet_evaluator::{Result, tla::TlaArg, trace::PathResolver};5use jrsonnet_stdlib::ContextInitializer;67#[derive(Clone)]8pub struct ExtStr {9	pub name: String,10	pub value: String,11}1213/// 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///37impl FromStr for ExtStr {38	type Err = &'static str;3940	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {41		match s.find('=') {42			Some(idx) => Ok(Self {43				name: s[..idx].to_owned(),44				value: s[idx + 1..].to_owned(),45			}),46			None => Ok(Self {47				name: s.to_owned(),48				value: std::env::var(s).or(Err("missing env var"))?,49			}),50		}51	}52}5354#[derive(Clone)]55pub struct ExtFile {56	pub name: String,57	pub path: String,58}5960impl FromStr for ExtFile {61	type Err = String;6263	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {64		let Some((name, path)) = s.split_once('=') else {65			return Err("bad ext-file syntax".to_owned());66		};67		Ok(Self {68			name: name.into(),69			path: path.into(),70		})71	}72}7374#[derive(Parser)]75#[clap(next_help_heading = "STANDARD LIBRARY")]76pub struct StdOpts {77	/// Disable standard library.78	/// By default standard library will be available via global `std` variable.79	#[clap(long)]80	no_stdlib: bool,81	/// Add string external variable.82	/// External variables are globally available so it is preferred83	/// to use top level arguments whenever it's possible.84	/// If [=data] is not set then it will be read from `name` env variable.85	/// Can be accessed from code via `std.extVar("name")`.86	#[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]87	ext_str: Vec<ExtStr>,88	/// Read string external variable from file.89	/// See also `--ext-str`90	#[clap(long, name = "name=var path", number_of_values = 1)]91	ext_str_file: Vec<ExtFile>,92	/// Add external variable from code.93	/// See also `--ext-str`94	#[clap(long, name = "name[=var source]", number_of_values = 1)]95	ext_code: Vec<ExtStr>,96	/// Read string external variable from file.97	/// See also `--ext-str`98	#[clap(long, name = "name=var code path", number_of_values = 1)]99	ext_code_file: Vec<ExtFile>,100}101impl StdOpts {102	pub fn context_initializer(&self) -> Result<Option<ContextInitializer>> {103		if self.no_stdlib {104			return Ok(None);105		}106		let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());107		for ext in &self.ext_str {108			ctx.settings_mut().ext_vars.insert(109				ext.name.as_str().into(),110				TlaArg::String(ext.value.as_str().into()),111			);112		}113		for ext in &self.ext_str_file {114			ctx.settings_mut().ext_vars.insert(115				ext.name.as_str().into(),116				TlaArg::ImportStr(ext.path.clone()),117			);118		}119		for ext in &self.ext_code {120			ctx.settings_mut().ext_vars.insert(121				ext.name.as_str().into(),122				TlaArg::InlineCode(ext.value.clone()),123			);124		}125		for ext in &self.ext_code_file {126			ctx.settings_mut()127				.ext_vars128				.insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));129		}130		Ok(Some(ctx))131	}132}