difftreelog
refactor(cli) move to split stdlib
in: master
5 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -15,6 +15,7 @@
"jrsonnet-evaluator/exp-preserve-order",
"jrsonnet-evaluator/exp-serde-preserve-order",
"jrsonnet-cli/exp-preserve-order",
+ "jrsonnet-cli/exp-serde-preserve-order",
]
# Destructuring of locals
exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
@@ -27,5 +28,5 @@
mimallocator = { version = "0.1.3", optional = true }
thiserror = "1.0"
-clap = { version = "3.1", features = ["derive"] }
-clap_complete = { version = "3.1" }
+clap = { version = "3.2", features = ["derive"] }
+clap_complete = { version = "3.2" }
crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth889[features]9[features]10exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]10exp-preserve-order = [11 "jrsonnet-evaluator/exp-preserve-order",12 "jrsonnet-stdlib/exp-preserve-order",13]14exp-serde-preserve-order = [15 "jrsonnet-evaluator/exp-serde-preserve-order",16 "jrsonnet-stdlib/exp-serde-preserve-order",17]111812[dependencies]19[dependencies]13jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [20jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [14 "explaining-traces",21 "explaining-traces",15] }22] }16jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.4.2" }23jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.4.2" }17jrsonnet-gcmodule = { version = "0.3.4" }24jrsonnet-gcmodule = { version = "0.3.4" }25jrsonnet-stdlib = { path = "../../crates/jrsonnet-stdlib", version = "0.4.2" }182619clap = { version = "3.1", features = ["derive"] }27clap = { version = "3.2", features = ["derive"] }2028crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/ext.rs
+++ /dev/null
@@ -1,118 +0,0 @@
-use std::{fs::read_to_string, str::FromStr};
-
-use clap::Parser;
-use jrsonnet_evaluator::{error::Result, State};
-
-use crate::ConfigureState;
-
-#[derive(Clone)]
-pub struct ExtStr {
- pub name: String,
- pub value: String,
-}
-
-impl FromStr for ExtStr {
- type Err = &'static str;
- fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let out: Vec<_> = s.split('=').collect();
- match out.len() {
- 1 => Ok(ExtStr {
- name: out[0].to_owned(),
- value: std::env::var(out[0]).or(Err("missing env var"))?,
- }),
- 2 => Ok(ExtStr {
- name: out[0].to_owned(),
- value: out[1].to_owned(),
- }),
-
- _ => Err("bad ext-str syntax"),
- }
- }
-}
-
-#[derive(Clone)]
-pub struct ExtFile {
- pub name: String,
- pub value: String,
-}
-
-impl FromStr for ExtFile {
- type Err = String;
-
- fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let out: Vec<&str> = s.split('=').collect();
- if out.len() != 2 {
- return Err("bad ext-file syntax".to_owned());
- }
- let file = read_to_string(&out[1]);
- match file {
- Ok(content) => Ok(Self {
- name: out[0].into(),
- value: content,
- }),
- Err(e) => Err(format!("{}", e)),
- }
- }
-}
-
-#[derive(Parser)]
-#[clap(next_help_heading = "EXTERNAL VARIABLES")]
-pub struct ExtVarOpts {
- /// Add string external variable.
- /// External variables are globally available so it is preferred
- /// to use top level arguments whenever it's possible.
- /// If [=data] is not set then it will be read from `name` env variable.
- /// Can be accessed from code via `std.extVar("name")`.
- #[clap(
- long,
- short = 'V',
- name = "name[=var data]",
- number_of_values = 1,
- multiple_occurrences = true
- )]
- ext_str: Vec<ExtStr>,
- /// Read string external variable from file.
- /// See also `--ext-str`
- #[clap(
- long,
- name = "name=var path",
- number_of_values = 1,
- multiple_occurrences = true
- )]
- ext_str_file: Vec<ExtFile>,
- /// Add external variable from code.
- /// See also `--ext-str`
- #[clap(
- long,
- name = "name[=var source]",
- number_of_values = 1,
- multiple_occurrences = true
- )]
- ext_code: Vec<ExtStr>,
- /// Read string external variable from file.
- /// See also `--ext-str`
- #[clap(
- long,
- name = "name=var code path",
- number_of_values = 1,
- multiple_occurrences = true
- )]
- ext_code_file: Vec<ExtFile>,
-}
-impl ConfigureState for ExtVarOpts {
- fn configure(&self, s: &State) -> Result<()> {
- for ext in self.ext_str.iter() {
- s.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
- }
- for ext in self.ext_str_file.iter() {
- s.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
- }
- for ext in self.ext_code.iter() {
- s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
- }
- for ext in self.ext_code_file.iter() {
- s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
- }
- Ok(())
- }
-}
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -1,15 +1,15 @@
-mod ext;
mod manifest;
+mod stdlib;
mod tla;
mod trace;
use std::{env, path::PathBuf};
use clap::Parser;
-pub use ext::*;
use jrsonnet_evaluator::{error::Result, FileImportResolver, State};
use jrsonnet_gcmodule::with_thread_object_space;
pub use manifest::*;
+pub use stdlib::*;
pub use tla::*;
pub use trace::*;
@@ -31,13 +31,6 @@
#[derive(Parser)]
#[clap(next_help_heading = "OPTIONS")]
pub struct MiscOpts {
- /// Disable standard library.
- /// By default standard library will be available via global `std` variable.
- /// Note that standard library will still be loaded
- /// if chosen manifestification method is not `none`.
- #[clap(long)]
- no_stdlib: bool,
-
/// Maximal allowed number of stack frames,
/// stack overflow error will be raised if this number gets exceeded.
#[clap(long, short = 's', default_value = "200")]
@@ -52,10 +45,6 @@
}
impl ConfigureState for MiscOpts {
fn configure(&self, s: &State) -> Result<()> {
- if !self.no_stdlib {
- s.with_stdlib();
- }
-
let mut library_paths = self.jpath.clone();
library_paths.reverse();
if let Some(path) = env::var_os("JSONNET_PATH") {
@@ -79,7 +68,7 @@
#[clap(flatten)]
tla: TLAOpts,
#[clap(flatten)]
- ext: ExtVarOpts,
+ std: StdOpts,
#[clap(flatten)]
trace: TraceOpts,
@@ -91,7 +80,7 @@
self.trace.configure(s)?;
self.misc.configure(s)?;
self.tla.configure(s)?;
- self.ext.configure(s)?;
+ self.std.configure(s)?;
Ok(())
}
}
@@ -114,10 +103,10 @@
impl GcOpts {
pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {
(
- self.gc_print_stats.then(|| GcStatsPrinter {
+ self.gc_print_stats.then_some(GcStatsPrinter {
collect_before_printing_stats: self.gc_collect_before_printing_stats,
}),
- (!self.gc_collect_on_exit).then(|| LeakSpace {}),
+ (!self.gc_collect_on_exit).then_some(LeakSpace {}),
)
}
}
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -0,0 +1,129 @@
+use std::{fs::read_to_string, str::FromStr};
+
+use clap::Parser;
+use jrsonnet_evaluator::{error::Result, State};
+
+use crate::ConfigureState;
+
+#[derive(Clone)]
+pub struct ExtStr {
+ pub name: String,
+ pub value: String,
+}
+
+impl FromStr for ExtStr {
+ type Err = &'static str;
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ let out: Vec<_> = s.split('=').collect();
+ match out.len() {
+ 1 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: std::env::var(out[0]).or(Err("missing env var"))?,
+ }),
+ 2 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: out[1].to_owned(),
+ }),
+
+ _ => Err("bad ext-str syntax"),
+ }
+ }
+}
+
+#[derive(Clone)]
+pub struct ExtFile {
+ pub name: String,
+ pub value: String,
+}
+
+impl FromStr for ExtFile {
+ type Err = String;
+
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ let out: Vec<&str> = s.split('=').collect();
+ if out.len() != 2 {
+ return Err("bad ext-file syntax".to_owned());
+ }
+ let file = read_to_string(&out[1]);
+ match file {
+ Ok(content) => Ok(Self {
+ name: out[0].into(),
+ value: content,
+ }),
+ Err(e) => Err(format!("{}", e)),
+ }
+ }
+}
+
+#[derive(Parser)]
+#[clap(next_help_heading = "STANDARD LIBRARY")]
+pub struct StdOpts {
+ /// Disable standard library.
+ /// By default standard library will be available via global `std` variable.
+ /// Note that standard library will still be loaded
+ /// if chosen manifestification method is not `none`.
+ #[clap(long)]
+ no_stdlib: bool,
+ /// Add string external variable.
+ /// External variables are globally available so it is preferred
+ /// to use top level arguments whenever it's possible.
+ /// If [=data] is not set then it will be read from `name` env variable.
+ /// Can be accessed from code via `std.extVar("name")`.
+ #[clap(
+ long,
+ short = 'V',
+ name = "name[=var data]",
+ number_of_values = 1,
+ multiple_occurrences = true
+ )]
+ ext_str: Vec<ExtStr>,
+ /// Read string external variable from file.
+ /// See also `--ext-str`
+ #[clap(
+ long,
+ name = "name=var path",
+ number_of_values = 1,
+ multiple_occurrences = true
+ )]
+ ext_str_file: Vec<ExtFile>,
+ /// Add external variable from code.
+ /// See also `--ext-str`
+ #[clap(
+ long,
+ name = "name[=var source]",
+ number_of_values = 1,
+ multiple_occurrences = true
+ )]
+ ext_code: Vec<ExtStr>,
+ /// Read string external variable from file.
+ /// See also `--ext-str`
+ #[clap(
+ long,
+ name = "name=var code path",
+ number_of_values = 1,
+ multiple_occurrences = true
+ )]
+ ext_code_file: Vec<ExtFile>,
+}
+impl ConfigureState for StdOpts {
+ fn configure(&self, s: &State) -> Result<()> {
+ if self.no_stdlib {
+ return Ok(());
+ }
+ let ctx = jrsonnet_stdlib::ContextInitializer::new(s.clone());
+ for ext in self.ext_str.iter() {
+ ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ }
+ for ext in self.ext_str_file.iter() {
+ ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ }
+ for ext in self.ext_code.iter() {
+ ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ }
+ for ext in self.ext_code_file.iter() {
+ ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ }
+ s.settings_mut().context_initializer = Box::new(ctx);
+ Ok(())
+ }
+}