Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace get_value functions with implementing Deref #105

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions rustywind-core/src/sorter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The module that sorts the classes in the file contents.
use std::borrow::Cow;
use std::ops::Deref;

use ahash::AHashMap as HashMap;

Expand All @@ -16,8 +17,11 @@ pub enum FinderRegex {
DefaultRegex,
CustomRegex(Regex),
}
impl FinderRegex {
fn get_value(&self) -> &Regex {

impl Deref for FinderRegex {
type Target = Regex;

fn deref(&self) -> &Self::Target {
match &self {
Self::DefaultRegex => &RE,
Self::CustomRegex(re) => re,
Expand All @@ -31,11 +35,14 @@ pub enum Sorter {
DefaultSorter,
CustomSorter(HashMap<String, usize>),
}
impl Sorter {
fn get_value(&self) -> &HashMap<String, usize> {

impl Deref for Sorter {
type Target = HashMap<String, usize>;

fn deref(&self) -> &Self::Target {
match &self {
Self::DefaultSorter => &SORTER,
Self::CustomSorter(custom_sorter) => custom_sorter,
Self::CustomSorter(sorter) => sorter,
}
}
}
Expand All @@ -50,26 +57,23 @@ pub struct Options {

/// Checks if the file contents have any classes.
pub fn has_classes(file_contents: &str, options: &Options) -> bool {
options.regex.get_value().is_match(file_contents)
options.regex.is_match(file_contents)
}

/// Sorts the classes in the file contents.
pub fn sort_file_contents<'a>(file_contents: &'a str, options: &Options) -> Cow<'a, str> {
options
.regex
.get_value()
.replace_all(file_contents, |caps: &Captures| {
let classes = &caps[1];
let sorted_classes = sort_classes(classes, options);

caps[0].replace(classes, &sorted_classes)
})
options.regex.replace_all(file_contents, |caps: &Captures| {
let classes = &caps[1];
let sorted_classes = sort_classes(classes, options);

caps[0].replace(classes, &sorted_classes)
})
}

/// Given a [&str] of whitespace-separated classes, returns a [String] of sorted classes.
/// Does not preserve whitespace.
pub fn sort_classes(class_string: &str, options: &Options) -> String {
let sorter = options.sorter.get_value();
let sorter = &options.sorter;

if options.allow_duplicates {
sort_classes_vec(class_string.split_ascii_whitespace(), sorter)
Expand Down
Loading