Skip to content

Commit

Permalink
Refactor put the entire public API in the RustyWind struct
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Jan 31, 2025
1 parent fd48f2d commit 19d00c2
Show file tree
Hide file tree
Showing 10 changed files with 652 additions and 614 deletions.
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
15 changes: 7 additions & 8 deletions rustywind-cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 +55,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 +77,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 +86,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 +106,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
Loading

0 comments on commit 19d00c2

Please sign in to comment.