resolve remaining clap deprecations

This commit is contained in:
Connor Skees 2023-07-14 15:45:40 +00:00
parent 95d09192cd
commit 5b6a06d3c8
3 changed files with 69 additions and 12 deletions

View File

@ -646,9 +646,9 @@ impl<'a> Serializer<'a> {
// SAFETY: todo // SAFETY: todo
let mut as_string = unsafe { String::from_utf8_unchecked(self.buffer) }; let mut as_string = unsafe { String::from_utf8_unchecked(self.buffer) };
if is_not_ascii && self.options.is_compressed() { if is_not_ascii && self.options.is_compressed() && self.options.allows_charset {
as_string.insert(0, '\u{FEFF}'); as_string.insert(0, '\u{FEFF}');
} else if is_not_ascii { } else if is_not_ascii && self.options.allows_charset {
as_string.insert_str(0, "@charset \"UTF-8\";\n"); as_string.insert_str(0, "@charset \"UTF-8\";\n");
} }

View File

@ -4,7 +4,7 @@ use std::{
path::Path, path::Path,
}; };
use clap::{value_parser, Arg, ArgEnum, Command, PossibleValue}; use clap::{value_parser, Arg, ArgAction, ArgEnum, Command, PossibleValue};
use grass::{from_path, from_string, Options, OutputStyle}; use grass::{from_path, from_string, Options, OutputStyle};
@ -46,13 +46,14 @@ impl ArgEnum for SourceMapUrls {
} }
} }
fn main() -> std::io::Result<()> { fn cli() -> Command<'static> {
let matches = Command::new("grass") Command::new("grass")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("A near-feature-complete Sass compiler written purely in Rust") .about("A near-feature-complete Sass compiler written purely in Rust")
.mut_arg("version", |arg| arg.short('v')) .mut_arg("version", |arg| arg.short('v'))
.arg( .arg(
Arg::new("STDIN") Arg::new("STDIN")
.action(ArgAction::SetTrue)
.long("stdin") .long("stdin")
.help("Read the stylesheet from stdin"), .help("Read the stylesheet from stdin"),
) )
@ -67,7 +68,7 @@ fn main() -> std::io::Result<()> {
.short('I') .short('I')
.long("load-path") .long("load-path")
.help("A path to use when resolving imports. May be passed multiple times.") .help("A path to use when resolving imports. May be passed multiple times.")
.multiple_occurrences(true) .action(ArgAction::Append)
.takes_value(true) .takes_value(true)
.value_parser(value_parser!(String)) .value_parser(value_parser!(String))
.number_of_values(1) .number_of_values(1)
@ -86,6 +87,7 @@ fn main() -> std::io::Result<()> {
) )
.arg( .arg(
Arg::new("NO_CHARSET") Arg::new("NO_CHARSET")
.action(ArgAction::SetTrue)
.long("no-charset") .long("no-charset")
.help("Don't emit a @charset or BOM for CSS with non-ASCII characters."), .help("Don't emit a @charset or BOM for CSS with non-ASCII characters."),
) )
@ -172,11 +174,13 @@ fn main() -> std::io::Result<()> {
) )
.arg( .arg(
Arg::new("NO_UNICODE") Arg::new("NO_UNICODE")
.action(ArgAction::SetTrue)
.long("no-unicode") .long("no-unicode")
.help("Whether to use Unicode characters for messages.") .help("Whether to use Unicode characters for messages.")
) )
.arg( .arg(
Arg::new("QUIET") Arg::new("QUIET")
.action(ArgAction::SetTrue)
.short('q') .short('q')
.long("quiet") .long("quiet")
.help("Don't print warnings."), .help("Don't print warnings."),
@ -199,7 +203,10 @@ fn main() -> std::io::Result<()> {
.hide(true) .hide(true)
.takes_value(true) .takes_value(true)
) )
.get_matches(); }
fn main() -> std::io::Result<()> {
let matches = cli().get_matches();
let load_paths = matches let load_paths = matches
.get_many::<String>("LOAD_PATH") .get_many::<String>("LOAD_PATH")
@ -213,12 +220,12 @@ fn main() -> std::io::Result<()> {
let options = &Options::default() let options = &Options::default()
.load_paths(&load_paths) .load_paths(&load_paths)
.style(style) .style(style)
.quiet(matches.is_present("QUIET")) .quiet(matches.get_flag("QUIET"))
.unicode_error_messages(!matches.is_present("NO_UNICODE")) .unicode_error_messages(!matches.get_flag("NO_UNICODE"))
.allows_charset(!matches.is_present("NO_CHARSET")); .allows_charset(!matches.get_flag("NO_CHARSET"));
let (mut stdout_write, mut file_write); let (mut stdout_write, mut file_write);
let buf_out: &mut dyn Write = if let Some(path) = matches.get_one::<&str>("OUTPUT") { let buf_out: &mut dyn Write = if let Some(path) = matches.get_one::<String>("OUTPUT") {
file_write = OpenOptions::new() file_write = OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)
@ -233,7 +240,7 @@ fn main() -> std::io::Result<()> {
buf_out.write_all( buf_out.write_all(
if let Some(name) = matches.get_one::<String>("INPUT") { if let Some(name) = matches.get_one::<String>("INPUT") {
from_path(name, options) from_path(name, options)
} else if matches.is_present("STDIN") { } else if matches.get_flag("STDIN") {
from_string( from_string(
{ {
let mut buffer = String::new(); let mut buffer = String::new();
@ -253,3 +260,13 @@ fn main() -> std::io::Result<()> {
)?; )?;
Ok(()) Ok(())
} }
#[cfg(test)]
mod test {
use crate::cli;
#[test]
fn verify() {
cli().debug_assert();
}
}

View File

@ -1,3 +1,5 @@
use grass_compiler::OutputStyle;
#[macro_use] #[macro_use]
mod macros; mod macros;
@ -57,3 +59,41 @@ error!(
invalid_charset_value_unterminated_loud_comment, invalid_charset_value_unterminated_loud_comment,
"@charset /*", "Error: expected more input." "@charset /*", "Error: expected more input."
); );
#[test]
fn charset_not_allowed_expanded() {
let input = r#"
a {
color: 🦆;
}
"#;
assert_eq!(
"a {\n color: 🦆;\n}\n",
&grass::from_string(
input.to_string(),
&grass::Options::default().allows_charset(false)
)
.expect(input)
);
}
#[test]
fn charset_not_allowed_compressed() {
let input = r#"
a {
color: 🦆;
}
"#;
assert_eq!(
"a{color:🦆}",
&grass::from_string(
input.to_string(),
&grass::Options::default()
.allows_charset(false)
.style(OutputStyle::Compressed)
)
.expect(input)
);
}