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

Refactor into RustyWind struct #116

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
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
25 changes: 12 additions & 13 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ repository = "https://github.com/avencera/rustywind"

[workspace.dependencies]
once_cell = "1.20"
regex = "1.11"
itertools = "0.13"

# hashmap
ahash = "0.8"
Expand All @@ -26,6 +24,10 @@ color-eyre = "0.6"
env_logger = "0.11"
log = "0.4"

# regex
regex = "1.11"


[profile.release]
codegen-units = 1
lto = "fat"
Expand Down
3 changes: 0 additions & 3 deletions rustywind-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ rustywind_vite = { path = "../rustywind-vite" }
# rustywind_core = { version = "0.3" }
# rustywind_vite = { version = "0.3" }

# iter
itertools = { workspace = true }

# utils
regex = { workspace = true }
once_cell = { workspace = true }
Expand Down
10 changes: 6 additions & 4 deletions rustywind-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ fn main() -> Result<()> {

let cli = Cli::parse();
let options = Options::new_from_cli(cli)?;
let rustywind = &options.rustywind;

match &options.write_mode {
WriteMode::ToStdOut => (),
Expand All @@ -124,8 +125,8 @@ fn main() -> Result<()> {
if let WriteMode::ToStdOut = &options.write_mode {
let contents = options.stdin.clone().unwrap_or_default();

if sorter::has_classes(&contents, &options.sorter_options) {
let sorted_content = sorter::sort_file_contents(&contents, &options.sorter_options);
if rustywind.has_classes(&contents) {
let sorted_content = rustywind.sort_file_contents(&contents);
print!("{sorted_content}");
} else {
print!("{contents}");
Expand All @@ -152,10 +153,11 @@ fn run_on_file_paths(file_path: &Path, options: &Options) {
return;
}

let rustywind = &options.rustywind;
match fs::read_to_string(file_path) {
Ok(contents) => {
if sorter::has_classes(&contents, &options.sorter_options) {
let sorted_content = sorter::sort_file_contents(&contents, &options.sorter_options);
if rustywind.has_classes(&contents) {
let sorted_content = rustywind.sort_file_contents(&contents);
let contents_changed = sorted_content != contents;

match (contents_changed, &options.write_mode) {
Expand Down
17 changes: 7 additions & 10 deletions rustywind-cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use clap::ValueEnum;
use color_eyre::Help;
use eyre::{Context, Result};
use ignore::WalkBuilder;
use itertools::Itertools;
use regex::Regex;
use rustywind_core::sorter::ClassWrapping;
use rustywind_core::{parser, sorter};
use rustywind_core::class_wrapping::ClassWrapping;
use rustywind_core::RustyWind;
use rustywind_vite::create_vite_sorter;
use serde::Deserialize;
use std::fs;
Expand Down Expand Up @@ -55,7 +54,7 @@ impl ValueEnum for CliClassWrapping {
#[derive(Debug)]
pub struct Options {
pub stdin: Option<String>,
pub sorter_options: sorter::Options,
pub rustywind: RustyWind,
pub write_mode: WriteMode,
pub starting_paths: Vec<PathBuf>,
pub search_paths: Vec<PathBuf>,
Expand All @@ -77,7 +76,7 @@ impl Options {
let starting_paths = get_starting_path_from_cli(&cli);
let search_paths = get_search_paths_from_starting_paths(&starting_paths);

let sorter_options = sorter::Options {
let rustywind = RustyWind {
regex: get_custom_regex_from_cli(&cli)?,
sorter: get_sorter_from_cli(&cli)?,
allow_duplicates: cli.allow_duplicates,
Expand All @@ -86,7 +85,7 @@ impl Options {

Ok(Options {
stdin,
sorter_options,
rustywind,
starting_paths,
search_paths,
write_mode: get_write_mode_from_cli(&cli),
Expand All @@ -106,10 +105,9 @@ fn get_sorter_from_cli(cli: &Cli) -> Result<Sorter> {
.wrap_err_with(|| format!("Error opening the css file {css_file}"))
.with_suggestion(|| format!("Make sure the file {css_file} exists"))?;

let sorter =
parser::parse_classes_from_file(css_file).wrap_err("Error parsing the css file")?;
let sorter = Sorter::new_from_file(css_file)?;

return Ok(Sorter::CustomSorter(sorter));
return Ok(sorter);
}

if let Some(config_file) = &cli.config_file {
Expand Down Expand Up @@ -184,7 +182,6 @@ fn get_search_paths_from_starting_paths(starting_paths: &[PathBuf]) -> Vec<PathB
.filter(|f| f.path().is_file())
.map(|file| file.path().to_owned())
})
.unique()
.collect()
}

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

## [Unreleased]

### Refactor

- Completely refactored the public API, now all the functionality is in the `RustyWind` struct

### Changed

- Changed `HowClassesAreWrapped` to `ClassWrapping`
- Fixed some clippy warnings
- Implemented `Default` for `Options`
Expand Down
6 changes: 3 additions & 3 deletions rustywind-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustywind_core"
version = "0.2.1"
version = "0.3.0"
description = "A library for sorting tailwind CSS classes"
documentation = "https://docs.rs/rustywind_core"
authors.workspace = true
Expand All @@ -10,14 +10,14 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
itertools = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
ahash = { workspace = true }
eyre = { workspace = true }

# string matching
# parsing
aho-corasick = "1.0"
winnow = { version = "0.6", features = ["simd"] }

[dev-dependencies]
pretty_assertions = "1.4"
Expand Down
Loading
Loading