git.delta.rocks / jrsonnet / refs/commits / 9efbcad25b9d

difftreelog

build upgrade clap to v3.1

Yaroslav Bolyukin2022-03-06parent: #4ec6326.patch.diff
in: master

8 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -18,11 +18,5 @@
 mimallocator = { version = "0.1.3", optional = true }
 thiserror = "1.0"
 gcmodule = { git = "https://github.com/CertainLach/gcmodule", branch = "jrsonnet" }
-
-[dependencies.clap]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
-
-[dependencies.clap_generate]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+clap = { version = "3.1", features = ["derive"] }
+clap_complete = { version = "3.1" }
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,4 +1,5 @@
-use clap::{AppSettings, Clap, IntoApp};
+use clap::{AppSettings, IntoApp, Parser};
+use clap_complete::Shell;
 use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
 use jrsonnet_evaluator::{error::LocError, EvaluationState};
 use std::{
@@ -6,51 +7,39 @@
 	io::Read,
 	io::Write,
 	path::PathBuf,
-	str::FromStr,
 };
 
 #[cfg(feature = "mimalloc")]
 #[global_allocator]
 static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;
 
-#[derive(Clap)]
-#[clap(help_heading = "DEBUG")]
+#[derive(Parser)]
+enum SubOpts {
+	/// Generate completions for specified shell
+	Generate {
+		/// Target shell name
+		shell: Shell,
+	},
+}
+
+#[derive(Parser)]
+#[clap(next_help_heading = "DEBUG")]
 struct DebugOpts {
 	/// Required OS stack size.
 	/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.
 	#[clap(long, name = "size")]
 	pub os_stack: Option<usize>,
-	/// Generate completions script
-	#[clap(long)]
-	generate: Option<GenerateTarget>,
 }
 
-enum GenerateTarget {
-	Bash,
-	Zsh,
-	Fish,
-	PowerShell,
-}
-impl FromStr for GenerateTarget {
-	type Err = &'static str;
-
-	fn from_str(s: &str) -> Result<Self, Self::Err> {
-		match s {
-			"bash" => Ok(Self::Bash),
-			"zsh" => Ok(Self::Zsh),
-			"fish" => Ok(Self::Fish),
-			"powershell" => Ok(Self::PowerShell),
-			_ => Err("unknown target"),
-		}
-	}
-}
-
-#[derive(Clap)]
+#[derive(Parser)]
 #[clap(
-	global_setting = AppSettings::ColoredHelp,
 	global_setting = AppSettings::DeriveDisplayOrder,
+	// args_conflicts_with_subcommands = true,
 )]
 struct Opts {
+	#[clap(subcommand)]
+	sub: Option<SubOpts>,
+
 	#[clap(flatten)]
 	input: InputOpts,
 	#[clap(flatten)]
@@ -68,20 +57,17 @@
 fn main() {
 	let opts: Opts = Opts::parse();
 
-	if let Some(target) = opts.debug.generate {
-		use clap_generate::{generate, generators};
-		use GenerateTarget::*;
-		let app = &mut Opts::into_app();
-		let buf = &mut std::io::stdout();
-		let bin = "jrsonnet";
-		match target {
-			Bash => generate::<generators::Bash, _>(app, bin, buf),
-			Zsh => generate::<generators::Zsh, _>(app, bin, buf),
-			Fish => generate::<generators::Fish, _>(app, bin, buf),
-			PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),
+	if let Some(sub) = opts.sub {
+		match sub {
+			SubOpts::Generate { shell } => {
+				use clap_complete::generate;
+				let app = &mut Opts::command();
+				let buf = &mut std::io::stdout();
+				generate(shell, app, "jrsonnet", buf);
+				std::process::exit(0)
+			}
 		}
-		std::process::exit(0);
-	};
+	}
 
 	let success = if let Some(size) = opts.debug.os_stack {
 		std::thread::Builder::new()
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -14,6 +14,4 @@
 jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.4.2" }
 gcmodule = { git = "https://github.com/CertainLach/gcmodule", branch = "jrsonnet" }
 
-[dependencies.clap]
-git = "https://github.com/clap-rs/clap"
-rev = "f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+clap = { version = "3.1", features = ["derive"] }
modifiedcrates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,5 +1,5 @@
 use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
 use std::{fs::read_to_string, str::FromStr};
 
@@ -53,8 +53,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "EXTERNAL VARIABLES")]
+#[derive(Parser)]
+#[clap(next_help_heading = "EXTERNAL VARIABLES")]
 pub struct ExtVarOpts {
 	/// Add string external variable.
 	/// External variables are globally available so it is preferred
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-cli/src/lib.rs
1mod ext;2mod manifest;3mod tla;4mod trace;56pub use ext::*;7pub use manifest::*;8pub use tla::*;9pub use trace::*;1011use clap::Clap;12use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};13use std::{env, path::PathBuf};1415pub trait ConfigureState {16	fn configure(&self, state: &EvaluationState) -> Result<()>;17}1819#[derive(Clap)]20#[clap(help_heading = "INPUT")]21pub struct InputOpts {22	#[clap(23		long,24		short = 'e',25		about = "Treat input as code, evaluate them instead of reading file"26	)]27	pub exec: bool,2829	#[clap(30		about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"31	)]32	pub input: String,33}3435#[derive(Clap)]36#[clap(help_heading = "OPTIONS")]37pub struct MiscOpts {38	/// Disable standard library.39	/// By default standard library will be available via global `std` variable.40	/// Note that standard library will still be loaded41	/// if chosen manifestification method is not `none`.42	#[clap(long)]43	no_stdlib: bool,4445	/// Maximal allowed number of stack frames,46	/// stack overflow error will be raised if this number gets exceeded.47	#[clap(long, short = 's', default_value = "200")]48	max_stack: usize,4950	/// Library search dirs. (right-most wins)51	/// Any not found `imported` file will be searched in these.52	/// This can also be specified via `JSONNET_PATH` variable,53	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.54	#[clap(long, short = 'J', multiple_occurrences = true)]55	jpath: Vec<PathBuf>,56}57impl ConfigureState for MiscOpts {58	fn configure(&self, state: &EvaluationState) -> Result<()> {59		if !self.no_stdlib {60			state.with_stdlib();61		}6263		let mut library_paths = self.jpath.clone();64		library_paths.reverse();65		if let Some(path) = env::var_os("JSONNET_PATH") {66			library_paths.extend(env::split_paths(path.as_os_str()));67		}6869		state.set_import_resolver(Box::new(FileImportResolver { library_paths }));7071		state.set_max_stack(self.max_stack);72		Ok(())73	}74}7576/// General configuration of jsonnet77#[derive(Clap)]78#[clap(name = "jrsonnet", version, author)]79pub struct GeneralOpts {80	#[clap(flatten)]81	misc: MiscOpts,8283	#[clap(flatten)]84	tla: TLAOpts,85	#[clap(flatten)]86	ext: ExtVarOpts,8788	#[clap(flatten)]89	trace: TraceOpts,90}9192impl ConfigureState for GeneralOpts {93	fn configure(&self, state: &EvaluationState) -> Result<()> {94		// Configure trace first, because tla-code/ext-code can throw95		self.trace.configure(state)?;96		self.misc.configure(state)?;97		self.tla.configure(state)?;98		self.ext.configure(state)?;99		Ok(())100	}101}102103#[derive(Clap)]104#[clap(help_heading = "GARBAGE COLLECTION")]105pub struct GcOpts {106	/// Do not skip gc on exit107	#[clap(long)]108	gc_collect_on_exit: bool,109	/// Print gc stats before exit110	#[clap(long)]111	gc_print_stats: bool,112	/// Force garbage collection before printing stats113	/// Useful for checking for memory leaks114	/// Does nothing useless --gc-print-stats is specified115	#[clap(long)]116	gc_collect_before_printing_stats: bool,117}118impl GcOpts {119	pub fn configure_global(&self) {120		if !self.gc_collect_on_exit {121			gcmodule::set_thread_collect_on_drop(false)122		}123	}124	pub fn stats_printer(&self) -> Option<GcStatsPrinter> {125		self.gc_print_stats.then(|| GcStatsPrinter {126			collect_before_printing_stats: self.gc_collect_before_printing_stats,127		})128	}129}130131pub struct GcStatsPrinter {132	collect_before_printing_stats: bool,133}134impl Drop for GcStatsPrinter {135	fn drop(&mut self) {136		eprintln!("=== GC STATS ===");137		if self.collect_before_printing_stats {138			let collected = gcmodule::collect_thread_cycles();139			eprintln!("Collected: {}", collected);140		}141		eprintln!("Tracked: {}", gcmodule::count_thread_tracked())142	}143}
after · crates/jrsonnet-cli/src/lib.rs
1mod ext;2mod manifest;3mod tla;4mod trace;56pub use ext::*;7pub use manifest::*;8pub use tla::*;9pub use trace::*;1011use clap::Parser;12use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};13use std::{env, path::PathBuf};1415pub trait ConfigureState {16	fn configure(&self, state: &EvaluationState) -> Result<()>;17}1819#[derive(Parser)]20#[clap(next_help_heading = "INPUT")]21pub struct InputOpts {22	/// Treat input as code, evaluate them instead of reading file23	#[clap(long, short = 'e')]24	pub exec: bool,2526	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself27	pub input: String,28}2930#[derive(Parser)]31#[clap(next_help_heading = "OPTIONS")]32pub struct MiscOpts {33	/// Disable standard library.34	/// By default standard library will be available via global `std` variable.35	/// Note that standard library will still be loaded36	/// if chosen manifestification method is not `none`.37	#[clap(long)]38	no_stdlib: bool,3940	/// Maximal allowed number of stack frames,41	/// stack overflow error will be raised if this number gets exceeded.42	#[clap(long, short = 's', default_value = "200")]43	max_stack: usize,4445	/// Library search dirs. (right-most wins)46	/// Any not found `imported` file will be searched in these.47	/// This can also be specified via `JSONNET_PATH` variable,48	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.49	#[clap(long, short = 'J', multiple_occurrences = true)]50	jpath: Vec<PathBuf>,51}52impl ConfigureState for MiscOpts {53	fn configure(&self, state: &EvaluationState) -> Result<()> {54		if !self.no_stdlib {55			state.with_stdlib();56		}5758		let mut library_paths = self.jpath.clone();59		library_paths.reverse();60		if let Some(path) = env::var_os("JSONNET_PATH") {61			library_paths.extend(env::split_paths(path.as_os_str()));62		}6364		state.set_import_resolver(Box::new(FileImportResolver { library_paths }));6566		state.set_max_stack(self.max_stack);67		Ok(())68	}69}7071/// General configuration of jsonnet72#[derive(Parser)]73#[clap(name = "jrsonnet", version, author)]74pub struct GeneralOpts {75	#[clap(flatten)]76	misc: MiscOpts,7778	#[clap(flatten)]79	tla: TLAOpts,80	#[clap(flatten)]81	ext: ExtVarOpts,8283	#[clap(flatten)]84	trace: TraceOpts,85}8687impl ConfigureState for GeneralOpts {88	fn configure(&self, state: &EvaluationState) -> Result<()> {89		// Configure trace first, because tla-code/ext-code can throw90		self.trace.configure(state)?;91		self.misc.configure(state)?;92		self.tla.configure(state)?;93		self.ext.configure(state)?;94		Ok(())95	}96}9798#[derive(Parser)]99#[clap(next_help_heading = "GARBAGE COLLECTION")]100pub struct GcOpts {101	/// Do not skip gc on exit102	#[clap(long)]103	gc_collect_on_exit: bool,104	/// Print gc stats before exit105	#[clap(long)]106	gc_print_stats: bool,107	/// Force garbage collection before printing stats108	/// Useful for checking for memory leaks109	/// Does nothing useless --gc-print-stats is specified110	#[clap(long)]111	gc_collect_before_printing_stats: bool,112}113impl GcOpts {114	pub fn configure_global(&self) {115		if !self.gc_collect_on_exit {116			gcmodule::set_thread_collect_on_drop(false)117		}118	}119	pub fn stats_printer(&self) -> Option<GcStatsPrinter> {120		self.gc_print_stats.then(|| GcStatsPrinter {121			collect_before_printing_stats: self.gc_collect_before_printing_stats,122		})123	}124}125126pub struct GcStatsPrinter {127	collect_before_printing_stats: bool,128}129impl Drop for GcStatsPrinter {130	fn drop(&mut self) {131		eprintln!("=== GC STATS ===");132		if self.collect_before_printing_stats {133			let collected = gcmodule::collect_thread_cycles();134			eprintln!("Collected: {}", collected);135		}136		eprintln!("Tracked: {}", gcmodule::count_thread_tracked())137	}138}
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,5 +1,5 @@
 use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};
 use std::{path::PathBuf, str::FromStr};
 
@@ -22,8 +22,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "MANIFESTIFICATION OUTPUT")]
+#[derive(Parser)]
+#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]
 pub struct ManifestOpts {
 	/// Output format, wraps resulting value to corresponding std.manifest call.
 	/// If set to `string` then plain string value is expected to be returned,
@@ -66,7 +66,7 @@
 	}
 }
 
-#[derive(Clap)]
+#[derive(Parser)]
 pub struct OutputOpts {
 	/// Write to the output file rather than stdout
 	#[clap(long, short = 'o')]
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,9 +1,9 @@
 use crate::{ConfigureState, ExtFile, ExtStr};
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
 
-#[derive(Clap)]
-#[clap(help_heading = "TOP LEVEL ARGUMENTS")]
+#[derive(Parser)]
+#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
 pub struct TLAOpts {
 	/// Add top level string argument.
 	/// Top level arguments will be passed to function before manifestification stage.
modifiedcrates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -1,5 +1,5 @@
 use crate::ConfigureState;
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{
 	error::Result,
 	trace::{CompactFormat, ExplainingFormat, PathResolver},
@@ -24,8 +24,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "STACK TRACE VISUAL")]
+#[derive(Parser)]
+#[clap(next_help_heading = "STACK TRACE VISUAL")]
 pub struct TraceOpts {
 	/// Format of stack traces' display in console.
 	/// `compact` format only shows `filename:line:column`s