difftreelog
feat std.extVar support
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth42 }42 }43}43}4445#[derive(Clone)]46struct ExtStr {47 name: String,48 value: String,49}50impl FromStr for ExtStr {51 type Err = &'static str;52 fn from_str(s: &str) -> Result<Self, Self::Err> {53 let out: Vec<_> = s.split('=').collect();54 match out.len() {55 1 => Ok(ExtStr {56 name: out[0].to_owned(),57 value: std::env::var(out[0]).or(Err("missing env var"))?,58 }),59 2 => Ok(ExtStr {60 name: out[0].to_owned(),61 value: out[1].to_owned(),62 }),63 _ => Err("bad ext-str syntax"),64 }65 }66}446745#[derive(Clap)]68#[derive(Clap)]46#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]69#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]47struct Opts {70struct Opts {48 #[clap(long, about = "Disable global std variable")]71 #[clap(long, about = "Disable global std variable")]49 no_stdlib: bool,72 no_stdlib: bool,50 #[clap(long, about = "Add external string")]73 #[clap(long, about = "Add external string")]51 ext_str: Option<Vec<String>>,74 ext_str: Vec<ExtStr>,52 #[clap(long, about = "Add external string from code")]75 #[clap(long, about = "Add external string from code")]53 ext_code: Option<Vec<String>>,76 ext_code: Vec<ExtStr>,54 #[clap(long, about = "Add TLA")]77 #[clap(long, about = "Add TLA")]55 tla_str: Option<Vec<String>>,78 tla_str: Vec<ExtStr>,56 #[clap(long, about = "Add TLA from code")]79 #[clap(long, about = "Add TLA from code")]57 tla_code: Option<Vec<String>>,80 tla_code: Vec<ExtStr>,58 #[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]81 #[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]59 format: Format,82 format: Format,60 #[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]83 #[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]92 if !opts.no_stdlib {115 if !opts.no_stdlib {93 evaluator.with_stdlib();116 evaluator.with_stdlib();94 }117 }118 for ExtStr { name, value } in opts.ext_str.iter().cloned() {119 evaluator.add_ext_var(name, Val::Str(value));120 }121 for ExtStr { name, value } in opts.ext_code.iter().cloned() {122 evaluator.add_ext_var(name, evaluator.parse_evaluate_raw(&value).unwrap());123 }95 let mut input = current_dir().unwrap();124 let mut input = current_dir().unwrap();96 input.push(opts.input.clone());125 input.push(opts.input.clone());97 let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();126 let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth12 TooManyArgsFunctionHas(usize),12 TooManyArgsFunctionHas(usize),13 FunctionParameterNotBoundInCall(String),13 FunctionParameterNotBoundInCall(String),1415 UndefinedExternalVariable(String),141615 RuntimeError(String),17 RuntimeError(String),16 StackOverflow,18 StackOverflow,crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth533 panic!("bad pow call");533 panic!("bad pow call");534 }534 }535 }535 }536 ("std", "extVar") => {537 assert_eq!(args.len(), 1);538 if let Val::Str(a) = evaluate(context, &args[0].1)? {539 with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(540 || {541 create_error::<()>(crate::Error::UndefinedExternalVariable(a))542 .err()543 .unwrap()544 },545 )?546 } else {547 panic!("bad extVar call");548 }549 }536 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),550 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),537 },551 },538 Val::Func(f) => {552 Val::Func(f) => {crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth72 files: RefCell<HashMap<PathBuf, FileData>>,72 files: RefCell<HashMap<PathBuf, FileData>>,73 globals: RefCell<HashMap<String, Val>>,73 globals: RefCell<HashMap<String, Val>>,7475 /// Values to use with std.extVar76 ext_vars: RefCell<HashMap<String, Val>>,747775 settings: EvaluationSettings,78 settings: EvaluationSettings,76}79}177 pub fn add_global(&self, name: String, value: Val) {180 pub fn add_global(&self, name: String, value: Val) {178 self.0.globals.borrow_mut().insert(name, value);181 self.0.globals.borrow_mut().insert(name, value);179 }182 }183 pub fn add_ext_var(&self, name: String, value: Val) {184 self.0.ext_vars.borrow_mut().insert(name, value);185 }180186181 pub fn with_stdlib(&self) -> &Self {187 pub fn with_stdlib(&self) -> &Self {182 self.begin_state();188 self.begin_state();