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
after · cmds/jrsonnet/src/main.rs
1use clap::{AppSettings, IntoApp, Parser};2use clap_complete::Shell;3use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};4use jrsonnet_evaluator::{error::LocError, EvaluationState};5use std::{6	fs::{create_dir_all, File},7	io::Read,8	io::Write,9	path::PathBuf,10};1112#[cfg(feature = "mimalloc")]13#[global_allocator]14static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1516#[derive(Parser)]17enum SubOpts {18	/// Generate completions for specified shell19	Generate {20		/// Target shell name21		shell: Shell,22	},23}2425#[derive(Parser)]26#[clap(next_help_heading = "DEBUG")]27struct DebugOpts {28	/// Required OS stack size.29	/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.30	#[clap(long, name = "size")]31	pub os_stack: Option<usize>,32}3334#[derive(Parser)]35#[clap(36	global_setting = AppSettings::DeriveDisplayOrder,37	// args_conflicts_with_subcommands = true,38)]39struct Opts {40	#[clap(subcommand)]41	sub: Option<SubOpts>,4243	#[clap(flatten)]44	input: InputOpts,45	#[clap(flatten)]46	general: GeneralOpts,47	#[clap(flatten)]48	manifest: ManifestOpts,49	#[clap(flatten)]50	output: OutputOpts,51	#[clap(flatten)]52	debug: DebugOpts,53	#[clap(flatten)]54	gc: GcOpts,55}5657fn main() {58	let opts: Opts = Opts::parse();5960	if let Some(sub) = opts.sub {61		match sub {62			SubOpts::Generate { shell } => {63				use clap_complete::generate;64				let app = &mut Opts::command();65				let buf = &mut std::io::stdout();66				generate(shell, app, "jrsonnet", buf);67				std::process::exit(0)68			}69		}70	}7172	let success = if let Some(size) = opts.debug.os_stack {73		std::thread::Builder::new()74			.stack_size(size * 1024 * 1024)75			.spawn(|| main_catch(opts))76			.expect("new thread spawned")77			.join()78			.expect("thread finished successfully")79	} else {80		main_catch(opts)81	};82	if !success {83		std::process::exit(1);84	}85}8687#[derive(thiserror::Error, Debug)]88enum Error {89	// Handled differently90	#[error("evaluation error")]91	Evaluation(jrsonnet_evaluator::error::LocError),92	#[error("io error")]93	Io(#[from] std::io::Error),94	#[error("input is not utf8 encoded")]95	Utf8(#[from] std::str::Utf8Error),96}97impl From<LocError> for Error {98	fn from(e: LocError) -> Self {99		Self::Evaluation(e)100	}101}102103fn main_catch(opts: Opts) -> bool {104	let _printer = opts.gc.stats_printer();105	let state = EvaluationState::default();106	if let Err(e) = main_real(&state, opts) {107		if let Error::Evaluation(e) = e {108			eprintln!("{}", state.stringify_err(&e));109		} else {110			eprintln!("{}", e);111		}112		return false;113	}114	true115}116117fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> {118	opts.gc.configure_global();119	opts.general.configure(state)?;120	opts.manifest.configure(state)?;121122	let val = if opts.input.exec {123		state.evaluate_snippet_raw(124			PathBuf::from("<cmdline>").into(),125			(&opts.input.input as &str).into(),126		)?127	} else if opts.input.input == "-" {128		let mut input = Vec::new();129		std::io::stdin().read_to_end(&mut input)?;130		let input_str = std::str::from_utf8(&input)?.into();131		state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?132	} else {133		state.evaluate_file_raw(&PathBuf::from(opts.input.input))?134	};135136	let val = state.with_tla(val)?;137138	if let Some(multi) = opts.output.multi {139		if opts.output.create_output_dirs {140			let mut dir = multi.clone();141			dir.pop();142			create_dir_all(dir)?;143		}144		for (file, data) in state.manifest_multi(val)?.iter() {145			let mut path = multi.clone();146			path.push(file as &str);147			if opts.output.create_output_dirs {148				let mut dir = path.clone();149				dir.pop();150				create_dir_all(dir)?;151			}152			println!("{}", path.to_str().expect("path"));153			let mut file = File::create(path)?;154			writeln!(file, "{}", data)?;155		}156	} else if let Some(path) = opts.output.output_file {157		if opts.output.create_output_dirs {158			let mut dir = path.clone();159			dir.pop();160			create_dir_all(dir)?;161		}162		let mut file = File::create(path)?;163		writeln!(file, "{}", state.manifest(val)?)?;164	} else {165		let output = state.manifest(val)?;166		if !output.is_empty() {167			println!("{}", output);168		}169	}170171	Ok(())172}
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
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -8,7 +8,7 @@
 pub use tla::*;
 pub use trace::*;
 
-use clap::Clap;
+use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
 use std::{env, path::PathBuf};
 
@@ -16,24 +16,19 @@
 	fn configure(&self, state: &EvaluationState) -> Result<()>;
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "INPUT")]
+#[derive(Parser)]
+#[clap(next_help_heading = "INPUT")]
 pub struct InputOpts {
-	#[clap(
-		long,
-		short = 'e',
-		about = "Treat input as code, evaluate them instead of reading file"
-	)]
+	/// Treat input as code, evaluate them instead of reading file
+	#[clap(long, short = 'e')]
 	pub exec: bool,
 
-	#[clap(
-		about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"
-	)]
+	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself
 	pub input: String,
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "OPTIONS")]
+#[derive(Parser)]
+#[clap(next_help_heading = "OPTIONS")]
 pub struct MiscOpts {
 	/// Disable standard library.
 	/// By default standard library will be available via global `std` variable.
@@ -74,7 +69,7 @@
 }
 
 /// General configuration of jsonnet
-#[derive(Clap)]
+#[derive(Parser)]
 #[clap(name = "jrsonnet", version, author)]
 pub struct GeneralOpts {
 	#[clap(flatten)]
@@ -100,8 +95,8 @@
 	}
 }
 
-#[derive(Clap)]
-#[clap(help_heading = "GARBAGE COLLECTION")]
+#[derive(Parser)]
+#[clap(next_help_heading = "GARBAGE COLLECTION")]
 pub struct GcOpts {
 	/// Do not skip gc on exit
 	#[clap(long)]
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