From 5b6a06d3c8ef155d9bbbe8bb40973e981ac98450 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Fri, 14 Jul 2023 15:45:40 +0000 Subject: [PATCH] resolve remaining clap deprecations --- crates/compiler/src/serializer.rs | 4 ++-- crates/lib/src/main.rs | 37 ++++++++++++++++++++-------- crates/lib/tests/charset.rs | 40 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/crates/compiler/src/serializer.rs b/crates/compiler/src/serializer.rs index d2d28fc..7593e94 100644 --- a/crates/compiler/src/serializer.rs +++ b/crates/compiler/src/serializer.rs @@ -646,9 +646,9 @@ impl<'a> Serializer<'a> { // SAFETY: todo 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}'); - } else if is_not_ascii { + } else if is_not_ascii && self.options.allows_charset { as_string.insert_str(0, "@charset \"UTF-8\";\n"); } diff --git a/crates/lib/src/main.rs b/crates/lib/src/main.rs index 49a6a6b..fdd58e8 100644 --- a/crates/lib/src/main.rs +++ b/crates/lib/src/main.rs @@ -4,7 +4,7 @@ use std::{ 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}; @@ -46,13 +46,14 @@ impl ArgEnum for SourceMapUrls { } } -fn main() -> std::io::Result<()> { - let matches = Command::new("grass") +fn cli() -> Command<'static> { + Command::new("grass") .version(env!("CARGO_PKG_VERSION")) .about("A near-feature-complete Sass compiler written purely in Rust") .mut_arg("version", |arg| arg.short('v')) .arg( Arg::new("STDIN") + .action(ArgAction::SetTrue) .long("stdin") .help("Read the stylesheet from stdin"), ) @@ -67,7 +68,7 @@ fn main() -> std::io::Result<()> { .short('I') .long("load-path") .help("A path to use when resolving imports. May be passed multiple times.") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .value_parser(value_parser!(String)) .number_of_values(1) @@ -86,6 +87,7 @@ fn main() -> std::io::Result<()> { ) .arg( Arg::new("NO_CHARSET") + .action(ArgAction::SetTrue) .long("no-charset") .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::new("NO_UNICODE") + .action(ArgAction::SetTrue) .long("no-unicode") .help("Whether to use Unicode characters for messages.") ) .arg( Arg::new("QUIET") + .action(ArgAction::SetTrue) .short('q') .long("quiet") .help("Don't print warnings."), @@ -199,7 +203,10 @@ fn main() -> std::io::Result<()> { .hide(true) .takes_value(true) ) - .get_matches(); +} + +fn main() -> std::io::Result<()> { + let matches = cli().get_matches(); let load_paths = matches .get_many::("LOAD_PATH") @@ -213,12 +220,12 @@ fn main() -> std::io::Result<()> { let options = &Options::default() .load_paths(&load_paths) .style(style) - .quiet(matches.is_present("QUIET")) - .unicode_error_messages(!matches.is_present("NO_UNICODE")) - .allows_charset(!matches.is_present("NO_CHARSET")); + .quiet(matches.get_flag("QUIET")) + .unicode_error_messages(!matches.get_flag("NO_UNICODE")) + .allows_charset(!matches.get_flag("NO_CHARSET")); 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::("OUTPUT") { file_write = OpenOptions::new() .create(true) .write(true) @@ -233,7 +240,7 @@ fn main() -> std::io::Result<()> { buf_out.write_all( if let Some(name) = matches.get_one::("INPUT") { from_path(name, options) - } else if matches.is_present("STDIN") { + } else if matches.get_flag("STDIN") { from_string( { let mut buffer = String::new(); @@ -253,3 +260,13 @@ fn main() -> std::io::Result<()> { )?; Ok(()) } + +#[cfg(test)] +mod test { + use crate::cli; + + #[test] + fn verify() { + cli().debug_assert(); + } +} diff --git a/crates/lib/tests/charset.rs b/crates/lib/tests/charset.rs index 568ca28..1fdb3ac 100644 --- a/crates/lib/tests/charset.rs +++ b/crates/lib/tests/charset.rs @@ -1,3 +1,5 @@ +use grass_compiler::OutputStyle; + #[macro_use] mod macros; @@ -57,3 +59,41 @@ error!( invalid_charset_value_unterminated_loud_comment, "@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) + ); +}