Skip to content

Commit

Permalink
Rename really long enum names
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Oct 23, 2024
1 parent c49ffa9 commit 6e97088
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 84 deletions.
35 changes: 3 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions rustywind-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ pkg-fmt = "tgz"

[dependencies]
# rustywind
rustywind_core = { version = "0.2.1" }
rustywind_vite = { version = "0.2.1" }
rustywind_core = { path = "../rustywind-core" }
rustywind_vite = { path = "../rustywind-vite" }

# rustywind_core = { version = "0.3" }
# rustywind_vite = { version = "0.3" }

# iter
itertools = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion rustywind-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub struct Cli {
custom_regex: Option<String>,
/// Specify how individual classes are wrapped.
#[arg(long)]
class_wrapping: Option<options::CliHowClassesAreWrapped>,
class_wrapping: Option<options::CliClassWrapping>,
/// Do not print log messages
#[arg(long, default_value = "false", conflicts_with_all = &["dry_run"])]
quiet: bool,
Expand Down
16 changes: 8 additions & 8 deletions rustywind-cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eyre::{Context, Result};
use ignore::WalkBuilder;
use itertools::Itertools;
use regex::Regex;
use rustywind_core::sorter::HowClassesAreWrapped;
use rustywind_core::sorter::ClassWrapping;
use rustywind_core::{parser, sorter};
use rustywind_vite::create_vite_sorter;
use serde::Deserialize;
Expand Down Expand Up @@ -36,14 +36,14 @@ struct ConfigFileContents {

// Wrapper to be able to use the `ValueEnum` trait without adding clap to the core crate
#[derive(Clone, Copy, Debug)]
pub struct CliHowClassesAreWrapped(HowClassesAreWrapped);
pub struct CliClassWrapping(ClassWrapping);

impl ValueEnum for CliHowClassesAreWrapped {
impl ValueEnum for CliClassWrapping {
fn value_variants<'a>() -> &'a [Self] {
&[
CliHowClassesAreWrapped(HowClassesAreWrapped::NoWrapping),
CliHowClassesAreWrapped(HowClassesAreWrapped::CommaSingleQuotes),
CliHowClassesAreWrapped(HowClassesAreWrapped::CommaDoubleQuotes),
CliClassWrapping(ClassWrapping::NoWrapping),
CliClassWrapping(ClassWrapping::CommaSingleQuotes),
CliClassWrapping(ClassWrapping::CommaDoubleQuotes),
]
}

Expand Down Expand Up @@ -146,10 +146,10 @@ fn get_custom_regex_from_cli(cli: &Cli) -> Result<FinderRegex> {
}
}

fn get_class_wrapping_from_cli(cli: &Cli) -> HowClassesAreWrapped {
fn get_class_wrapping_from_cli(cli: &Cli) -> ClassWrapping {
match &cli.class_wrapping {
Some(class_wrapping) => class_wrapping.0,
None => HowClassesAreWrapped::NoWrapping,
None => ClassWrapping::NoWrapping,
}
}

Expand Down
3 changes: 3 additions & 0 deletions rustywind-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

- Changed `HowClassesAreWrapped` to `ClassWrapping`
- Fixed some clippy warnings

## [0.2.0] - 2024-10-21

- Add options to handle wrapped classes to extend the set of use cases [#109](https://github.com/avencera/rustywind/pull/109), thanks [@dikkadev]](https://github.com/dikkadev])
Expand Down
69 changes: 29 additions & 40 deletions rustywind-core/src/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ impl Deref for FinderRegex {

/// How individual classes are wrapped.
#[derive(Debug, Clone, Copy)]
pub enum HowClassesAreWrapped {
pub enum ClassWrapping {
NoWrapping,
CommaSingleQuotes,
CommaDoubleQuotes,
}

impl Default for HowClassesAreWrapped {
impl Default for ClassWrapping {
fn default() -> Self {
Self::NoWrapping
}
}

impl HowClassesAreWrapped {
impl ClassWrapping {
pub fn as_str(&self) -> &'static str {
match self {
HowClassesAreWrapped::NoWrapping => "no-wrapping",
HowClassesAreWrapped::CommaSingleQuotes => "comma-single-quotes",
HowClassesAreWrapped::CommaDoubleQuotes => "comma-double-quotes",
ClassWrapping::NoWrapping => "no-wrapping",
ClassWrapping::CommaSingleQuotes => "comma-single-quotes",
ClassWrapping::CommaDoubleQuotes => "comma-double-quotes",
}
}
}

impl<T: AsRef<str>> From<T> for HowClassesAreWrapped {
impl<T: AsRef<str>> From<T> for ClassWrapping {
fn from(s: T) -> Self {
match s.as_ref() {
"no-wrapping" => Self::NoWrapping,
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct Options {
pub regex: FinderRegex,
pub sorter: Sorter,
pub allow_duplicates: bool,
pub class_wrapping: HowClassesAreWrapped,
pub class_wrapping: ClassWrapping,
}

/// Checks if the file contents have any classes.
Expand Down Expand Up @@ -121,33 +121,30 @@ pub fn sort_classes(class_string: &str, options: &Options) -> String {
rewrap_wrapped_classes(sorted, options.class_wrapping)
}

fn unwrap_wrapped_classes<'a>(
class_string: &'a str,
wrapping: HowClassesAreWrapped,
) -> Vec<&'a str> {
fn unwrap_wrapped_classes(class_string: &str, wrapping: ClassWrapping) -> Vec<&str> {
match wrapping {
HowClassesAreWrapped::NoWrapping => class_string.split_ascii_whitespace().collect(),
HowClassesAreWrapped::CommaSingleQuotes => class_string
ClassWrapping::NoWrapping => class_string.split_ascii_whitespace().collect(),
ClassWrapping::CommaSingleQuotes => class_string
.split(',')
.flat_map(|class| class.split_ascii_whitespace())
.map(|class| class.trim_matches('\''))
.collect(),
HowClassesAreWrapped::CommaDoubleQuotes => class_string
ClassWrapping::CommaDoubleQuotes => class_string
.split(',')
.flat_map(|class| class.split_ascii_whitespace())
.map(|class| class.trim_matches('"'))
.collect(),
}
}

fn rewrap_wrapped_classes<'a>(classes: Vec<&'a str>, wrapping: HowClassesAreWrapped) -> String {
fn rewrap_wrapped_classes(classes: Vec<&str>, wrapping: ClassWrapping) -> String {
match wrapping {
HowClassesAreWrapped::NoWrapping => classes.join(" "),
HowClassesAreWrapped::CommaSingleQuotes => classes
ClassWrapping::NoWrapping => classes.join(" "),
ClassWrapping::CommaSingleQuotes => classes
.iter()
.map(|class| format!("'{}'", class))
.join(", "),
HowClassesAreWrapped::CommaDoubleQuotes => classes
ClassWrapping::CommaDoubleQuotes => classes
.iter()
.map(|class| format!("\"{}\"", class))
.join(", "),
Expand Down Expand Up @@ -244,7 +241,7 @@ mod tests {
regex: FinderRegex::DefaultRegex,
sorter: Sorter::DefaultSorter,
allow_duplicates: false,
class_wrapping: HowClassesAreWrapped::NoWrapping,
class_wrapping: ClassWrapping::NoWrapping,
};

// HAS_CLASSES --------------------------------------------------------------------------------
Expand Down Expand Up @@ -514,80 +511,72 @@ mod tests {
// CLASS WRAPPING
#[test_case(
r#"flex-col inline flex"#,
HowClassesAreWrapped::NoWrapping,
ClassWrapping::NoWrapping,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "no wrapping"
)]
#[test_case(
r#"'flex-col', 'inline', 'flex'"#,
HowClassesAreWrapped::CommaSingleQuotes,
ClassWrapping::CommaSingleQuotes,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "comma single quotes"
)]
#[test_case(
r#""flex-col", "inline", "flex""#,
HowClassesAreWrapped::CommaDoubleQuotes,
ClassWrapping::CommaDoubleQuotes,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "comma double quotes"
)]
fn test_unwrap_wrapped_classes<'a>(
input: &str,
wrapping: HowClassesAreWrapped,
output: Vec<&str>,
) {
fn test_unwrap_wrapped_classes<'a>(input: &str, wrapping: ClassWrapping, output: Vec<&str>) {
assert_eq!(unwrap_wrapped_classes(input, wrapping), output)
}

#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::NoWrapping,
ClassWrapping::NoWrapping,
r#"flex-col inline flex"#
; "no wrapping"
)]
#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::CommaSingleQuotes,
ClassWrapping::CommaSingleQuotes,
r#"'flex-col', 'inline', 'flex'"#
; "comma single quotes"
)]
#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::CommaDoubleQuotes,
ClassWrapping::CommaDoubleQuotes,
r#""flex-col", "inline", "flex""#
; "comma double quotes"
)]
fn test_rewrap_wrapped_classes<'a>(
input: Vec<&'a str>,
wrapping: HowClassesAreWrapped,
output: &str,
) {
fn test_rewrap_wrapped_classes<'a>(input: Vec<&'a str>, wrapping: ClassWrapping, output: &str) {
assert_eq!(rewrap_wrapped_classes(input, wrapping), output)
}

#[test_case(
None,
HowClassesAreWrapped::NoWrapping,
ClassWrapping::NoWrapping,
r#"<div class="flex-col inline flex"></div>"#,
r#"<div class="inline flex flex-col"></div>"#
; "normal HTML use case"
)]
#[test_case(
Some(r#"(?:\[)([_a-zA-Z0-9\.,\-'"\s]+)(?:\])"#),
HowClassesAreWrapped::CommaSingleQuotes,
ClassWrapping::CommaSingleQuotes,
r#"classes = ['flex-col', 'inline', 'flex']"#,
r#"classes = ['inline', 'flex', 'flex-col']"#
; "array with single quotes"
)]
#[test_case(
Some(r#"(?:\[)([_a-zA-Z0-9\.,\-'"\s]+)(?:\])"#),
HowClassesAreWrapped::CommaDoubleQuotes,
ClassWrapping::CommaDoubleQuotes,
r#"classes = ["flex-col", "inline", "flex"]"#,
r#"classes = ["inline", "flex", "flex-col"]"#
; "array with double quotes"
)]
fn test_unusual_use_cases(
regex_overwrite: Option<&str>,
class_wrapping: HowClassesAreWrapped,
class_wrapping: ClassWrapping,
input: &str,
output: &str,
) {
Expand Down
3 changes: 2 additions & 1 deletion rustywind-vite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
rustywind_core = { version = "0.2.1" }
rustywind_core = { path = "../rustywind-core" }
# rustywind_core = { version = "0.3" }

# tls
rustls = { version = "0.23", features = ["ring"], default-features = false }
Expand Down

0 comments on commit 6e97088

Please sign in to comment.