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

difftreelog

refactor(cli) move to split stdlib

Yaroslav Bolyukin2022-07-23parent: #06fa714.patch.diff
in: master

5 files changed

modifiedcmds/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" }
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -7,7 +7,14 @@
 edition = "2021"
 
 [features]
-exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]
+exp-preserve-order = [
+    "jrsonnet-evaluator/exp-preserve-order",
+    "jrsonnet-stdlib/exp-preserve-order",
+]
+exp-serde-preserve-order = [
+    "jrsonnet-evaluator/exp-serde-preserve-order",
+    "jrsonnet-stdlib/exp-serde-preserve-order",
+]
 
 [dependencies]
 jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [
@@ -15,5 +22,6 @@
 ] }
 jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.4.2" }
 jrsonnet-gcmodule = { version = "0.3.4" }
+jrsonnet-stdlib = { path = "../../crates/jrsonnet-stdlib", version = "0.4.2" }
 
-clap = { version = "3.1", features = ["derive"] }
+clap = { version = "3.2", features = ["derive"] }
deletedcrates/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(())
-	}
-}
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
1mod ext;1mod manifest;
2mod manifest;2mod stdlib;
3mod tla;3mod tla;
4mod trace;4mod trace;
55
6use std::{env, path::PathBuf};6use std::{env, path::PathBuf};
77
8use clap::Parser;8use clap::Parser;
9pub use ext::*;
10use jrsonnet_evaluator::{error::Result, FileImportResolver, State};9use jrsonnet_evaluator::{error::Result, FileImportResolver, State};
11use jrsonnet_gcmodule::with_thread_object_space;10use jrsonnet_gcmodule::with_thread_object_space;
12pub use manifest::*;11pub use manifest::*;
12pub use stdlib::*;
13pub use tla::*;13pub use tla::*;
14pub use trace::*;14pub use trace::*;
1515
31#[derive(Parser)]31#[derive(Parser)]
32#[clap(next_help_heading = "OPTIONS")]32#[clap(next_help_heading = "OPTIONS")]
33pub struct MiscOpts {33pub struct MiscOpts {
34 /// Disable standard library.
35 /// By default standard library will be available via global `std` variable.
36 /// Note that standard library will still be loaded
37 /// if chosen manifestification method is not `none`.
38 #[clap(long)]
39 no_stdlib: bool,
40
41 /// Maximal allowed number of stack frames,34 /// Maximal allowed number of stack frames,
42 /// stack overflow error will be raised if this number gets exceeded.35 /// stack overflow error will be raised if this number gets exceeded.
52}45}
53impl ConfigureState for MiscOpts {46impl ConfigureState for MiscOpts {
54 fn configure(&self, s: &State) -> Result<()> {47 fn configure(&self, s: &State) -> Result<()> {
55 if !self.no_stdlib {
56 s.with_stdlib();
57 }
58
59 let mut library_paths = self.jpath.clone();48 let mut library_paths = self.jpath.clone();
60 library_paths.reverse();49 library_paths.reverse();
79 #[clap(flatten)]68 #[clap(flatten)]
80 tla: TLAOpts,69 tla: TLAOpts,
81 #[clap(flatten)]70 #[clap(flatten)]
82 ext: ExtVarOpts,71 std: StdOpts,
8372
84 #[clap(flatten)]73 #[clap(flatten)]
85 trace: TraceOpts,74 trace: TraceOpts,
91 self.trace.configure(s)?;80 self.trace.configure(s)?;
92 self.misc.configure(s)?;81 self.misc.configure(s)?;
93 self.tla.configure(s)?;82 self.tla.configure(s)?;
94 self.ext.configure(s)?;83 self.std.configure(s)?;
95 Ok(())84 Ok(())
96 }85 }
97}86}
114impl GcOpts {103impl GcOpts {
115 pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {104 pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {
116 (105 (
117 self.gc_print_stats.then(|| GcStatsPrinter {106 self.gc_print_stats.then_some(GcStatsPrinter {
118 collect_before_printing_stats: self.gc_collect_before_printing_stats,107 collect_before_printing_stats: self.gc_collect_before_printing_stats,
119 }),108 }),
120 (!self.gc_collect_on_exit).then(|| LeakSpace {}),109 (!self.gc_collect_on_exit).then_some(LeakSpace {}),
121 )110 )
122 }111 }
123}112}
addedcrates/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(())
+	}
+}