diff --git a/.github/workflows/rustfmt.yml b/.github/workflows/rustfmt.yml index 7b197c7..b2db840 100644 --- a/.github/workflows/rustfmt.yml +++ b/.github/workflows/rustfmt.yml @@ -1,4 +1,3 @@ -# When pushed to master, run `cargo +nightly fmt --all` and open a PR. name: rustfmt on: [push, pull_request] jobs: @@ -6,7 +5,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout sources - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install stable toolchain with rustfmt available uses: actions-rs/toolchain@v1 @@ -15,4 +14,4 @@ jobs: override: true components: rustfmt - - run: rustfmt --edition 2021 **/*.rs --check + - run: cargo fmt --check diff --git a/Cargo.lock b/Cargo.lock index 729d1fa..aa5212e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,9 +89,9 @@ dependencies = [ [[package]] name = "backtrace" -version = "0.3.69" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", @@ -120,9 +120,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.90" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +checksum = "1fd97381a8cc6493395a5afc4c691c1084b3768db713b73aa215217aa245d153" [[package]] name = "cfg-if" @@ -132,9 +132,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.3" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949626d00e063efc93b6dca932419ceb5432f99769911c0b995f7e884c778813" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", "clap_derive", @@ -154,9 +154,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.3" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90239a040c80f5e14809ca132ddc4176ab33d5e17e49691793296e3fcb34d72f" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" dependencies = [ "heck", "proc-macro2", @@ -313,9 +313,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "a06fddc2749e0528d2813f95e050e87e52c8cbbae56223b9babf73b3e53b0cc6" dependencies = [ "cfg-if", "libc", @@ -387,9 +387,9 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" [[package]] name = "indoc" -version = "2.0.4" +version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" [[package]] name = "itertools" @@ -402,9 +402,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "lazy_static" @@ -426,9 +426,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "miniz_oxide" @@ -468,9 +468,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pretty_assertions" @@ -502,9 +502,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4963ed1bc86e4f3ee217022bd855b297cef07fb9eac5dfa1f788b220b49b3bd" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" dependencies = [ "either", "rayon-core", @@ -522,9 +522,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -545,9 +545,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "ring" @@ -572,9 +572,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustls" -version = "0.22.2" +version = "0.22.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e87c9956bd9807afa1f77e0f7594af32566e830e088a5576d27c5b6f30f49d41" +checksum = "99008d7ad0bbbea527ec27bddbc0e432c5b87d8175178cee68d2eec9c4a1813c" dependencies = [ "log", "ring", @@ -586,9 +586,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.3.1" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ede67b28608b4c60685c7d54122d4400d90f62b40caee7700e700380a390fa8" +checksum = "ecd36cc4259e3e4514335c4a138c6b43171a8d61d8f5c9348f9fc7529416f247" [[package]] name = "rustls-webpki" @@ -606,7 +606,6 @@ name = "rustywind" version = "0.21.1" dependencies = [ "ahash", - "aho-corasick", "anstyle", "clap", "color-eyre", @@ -618,12 +617,37 @@ dependencies = [ "itertools", "log", "once_cell", - "pretty_assertions", "rayon", "regex", - "rustls", + "rustywind_core", + "rustywind_vite", "serde", "serde_json", +] + +[[package]] +name = "rustywind_core" +version = "0.1.0" +dependencies = [ + "ahash", + "aho-corasick", + "eyre", + "itertools", + "once_cell", + "pretty_assertions", + "regex", +] + +[[package]] +name = "rustywind_vite" +version = "0.1.0" +dependencies = [ + "color-eyre", + "eyre", + "once_cell", + "regex", + "rustls", + "rustywind_core", "ureq", ] @@ -664,9 +688,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" dependencies = [ "itoa", "ryu", @@ -690,9 +714,9 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "subtle" @@ -702,9 +726,9 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "syn" -version = "2.0.53" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 07d689d..ee8e9cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,63 +1,31 @@ -[package] +[workspace] +members = ["rustywind-cli", "rustywind-core", "rustywind-vite"] +default-members = ["rustywind-cli"] +resolver = "2" + +[workspace.package] authors = ["Praveen Perera "] edition = "2021" license = "Apache-2.0" -name = "rustywind" -version = "0.21.1" homepage = "https://github.com/avencera/rustywind" repository = "https://github.com/avencera/rustywind" -documentation = "https://docs.rs/rustywind" -description = "A CLI to sort tailwind CSS classes" - -[package.metadata.binstall] -pkg-url = "{ repo }/releases/download/v{ version }/{ name }-v{ version }-{ target }.tar.gz" -bin-dir = "{ bin }{ binary-ext }" -pkg-fmt = "tgz" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -# cli -clap = { version = "4.3", features = ["cargo", "derive", "unstable-styles"] } -indoc = "2.0" -colored = "2.1" - -# files -ignore = "0.4" - -# utils -itertools = "0.12" +[workspace.dependencies] once_cell = "1.18" - -# string matching -aho-corasick = "1.0" regex = "1.9" - -# logging -env_logger = "0.11" -log = "0.4" - -# faster hashmap +itertools = "0.12" +# Faster HashMap ahash = "0.8" - -# parallel -rayon = "1.5" - -# http -ureq = "2.9" - -# errors -color-eyre = "0.6" +# Errors eyre = "0.6" - -# json parsing -serde = {version = "1.0", features = ["derive"]} -serde_json = "1.0" +color-eyre = "0.6" +# Logging +env_logger = "0.11" +log = "0.4" +# TLS rustls = { version = "0.22" } -anstyle = "1.0.1" - -[dev-dependencies] -pretty_assertions = "1.4" +# HTTP +ureq = "2.9" [profile.release] codegen-units = 1 diff --git a/rustywind-cli/Cargo.toml b/rustywind-cli/Cargo.toml new file mode 100644 index 0000000..363a5d0 --- /dev/null +++ b/rustywind-cli/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "rustywind" +version = "0.21.1" +description = "A CLI to sort tailwind CSS classes" +documentation = "https://docs.rs/rustywind" +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true + +[package.metadata.binstall] +pkg-url = "{ repo }/releases/download/v{ version }/{ name }-v{ version }-{ target }.tar.gz" +bin-dir = "{ bin }{ binary-ext }" +pkg-fmt = "tgz" + +[dependencies] +rustywind_core = { path = "../rustywind-core" } +rustywind_vite = { path = "../rustywind-vite" } + +itertools = { workspace = true } +regex = { workspace = true } +once_cell = { workspace = true } +ahash = { workspace = true } +eyre = { workspace = true } +color-eyre = { workspace = true } +env_logger = { workspace = true } +log = { workspace = true } + +# CLI +clap = { version = "4.3", features = ["cargo", "derive", "unstable-styles"] } +indoc = "2.0" +colored = "2.1" +# Parallel +rayon = "1.5" +# Files +ignore = "0.4" +# JSON parsing +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +anstyle = "1.0.1" diff --git a/src/cli.rs b/rustywind-cli/src/cli.rs similarity index 100% rename from src/cli.rs rename to rustywind-cli/src/cli.rs diff --git a/src/main.rs b/rustywind-cli/src/main.rs similarity index 96% rename from src/main.rs rename to rustywind-cli/src/main.rs index b15c776..dbd13ad 100644 --- a/src/main.rs +++ b/rustywind-cli/src/main.rs @@ -1,18 +1,15 @@ mod cli; -pub mod consts; -pub mod defaults; -pub mod options; -pub mod parser; -pub mod sorter; -pub mod tls; +mod options; use ahash::AHashSet as HashSet; use clap::Parser; use eyre::Result; use indoc::indoc; use once_cell::sync::Lazy; -use options::{Options, WriteMode}; +use options::Options; +use options::WriteMode; use rayon::prelude::*; +use rustywind_core::sorter; use std::fs; use std::path::Path; use std::path::PathBuf; @@ -123,8 +120,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) { - let sorted_content = sorter::sort_file_contents(&contents, &options); + if sorter::has_classes(&contents, &options.sorter_options) { + let sorted_content = sorter::sort_file_contents(&contents, &options.sorter_options); print!("{sorted_content}"); } else { print!("{contents}"); @@ -153,8 +150,8 @@ fn run_on_file_paths(file_path: &Path, options: &Options) { match fs::read_to_string(file_path) { Ok(contents) => { - if sorter::has_classes(&contents, options) { - let sorted_content = sorter::sort_file_contents(&contents, options); + if sorter::has_classes(&contents, &options.sorter_options) { + let sorted_content = sorter::sort_file_contents(&contents, &options.sorter_options); let contents_changed = sorted_content != contents; match (contents_changed, &options.write_mode) { diff --git a/src/options.rs b/rustywind-cli/src/options.rs similarity index 92% rename from src/options.rs rename to rustywind-cli/src/options.rs index f6123bf..718b456 100644 --- a/src/options.rs +++ b/rustywind-cli/src/options.rs @@ -1,10 +1,10 @@ -pub mod vite; - use color_eyre::Help; use eyre::{Context, Result}; use ignore::WalkBuilder; use itertools::Itertools; use regex::Regex; +use rustywind_core::{parser, sorter}; +use rustywind_vite::create_vite_sorter; use serde::Deserialize; use std::fs; use std::io::Read; @@ -14,7 +14,7 @@ use std::str::FromStr; use ahash::AHashMap as HashMap; use ahash::AHashSet as HashSet; -use crate::parser; +use crate::sorter::{FinderRegex, Sorter}; use crate::Cli; #[derive(Debug)] @@ -26,18 +26,6 @@ pub enum WriteMode { CheckFormatted, } -#[derive(Debug)] -pub enum FinderRegex { - DefaultRegex, - CustomRegex(Regex), -} - -#[derive(Debug)] -pub enum Sorter { - DefaultSorter, - CustomSorter(HashMap), -} - #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct ConfigFileContents { @@ -47,11 +35,9 @@ struct ConfigFileContents { #[derive(Debug)] pub struct Options { pub stdin: Option, + pub sorter_options: sorter::Options, pub write_mode: WriteMode, - pub regex: FinderRegex, - pub sorter: Sorter, pub starting_paths: Vec, - pub allow_duplicates: bool, pub search_paths: Vec, pub ignored_files: HashSet, pub quiet: bool, @@ -71,14 +57,18 @@ 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 { + regex: get_custom_regex_from_cli(&cli)?, + sorter: get_sorter_from_cli(&cli)?, + allow_duplicates: cli.allow_duplicates, + }; + Ok(Options { stdin, + sorter_options, starting_paths, search_paths, write_mode: get_write_mode_from_cli(&cli), - regex: get_custom_regex_from_cli(&cli)?, - sorter: get_sorter_from_cli(&cli)?, - allow_duplicates: cli.allow_duplicates, ignored_files: get_ignored_files_from_cli(&cli), quiet: cli.quiet, }) @@ -87,7 +77,7 @@ impl Options { fn get_sorter_from_cli(cli: &Cli) -> Result { if let Some(vite_css_url) = &cli.vite_css { - return vite::create_sorter(vite_css_url, cli.skip_ssl_verification); + return create_vite_sorter(vite_css_url, cli.skip_ssl_verification); } if let Some(css_file) = &cli.output_css_file { @@ -152,7 +142,7 @@ fn get_write_mode_from_cli(cli: &Cli) -> WriteMode { } else if cli.stdin { WriteMode::ToStdOut } else { - WriteMode::DryRun + WriteMode::ToConsole } } diff --git a/rustywind-core/Cargo.toml b/rustywind-core/Cargo.toml new file mode 100644 index 0000000..f873659 --- /dev/null +++ b/rustywind-core/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "rustywind_core" +version = "0.1.0" +description = "A library for sorting tailwind CSS classes" +documentation = "https://docs.rs/rustywind_core" +authors.workspace = true +edition.workspace = true +license.workspace = true +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 +aho-corasick = "1.0" + +[dev-dependencies] +pretty_assertions = "1.4" diff --git a/src/consts.rs b/rustywind-core/src/consts.rs similarity index 100% rename from src/consts.rs rename to rustywind-core/src/consts.rs diff --git a/src/defaults.rs b/rustywind-core/src/defaults.rs similarity index 100% rename from src/defaults.rs rename to rustywind-core/src/defaults.rs diff --git a/rustywind-core/src/lib.rs b/rustywind-core/src/lib.rs new file mode 100644 index 0000000..701dcef --- /dev/null +++ b/rustywind-core/src/lib.rs @@ -0,0 +1,4 @@ +pub mod consts; +pub mod defaults; +pub mod parser; +pub mod sorter; diff --git a/src/parser.rs b/rustywind-core/src/parser.rs similarity index 100% rename from src/parser.rs rename to rustywind-core/src/parser.rs diff --git a/src/sorter.rs b/rustywind-core/src/sorter.rs similarity index 93% rename from src/sorter.rs rename to rustywind-core/src/sorter.rs index 0632eea..dc33352 100644 --- a/src/sorter.rs +++ b/rustywind-core/src/sorter.rs @@ -4,11 +4,29 @@ use ahash::AHashMap as HashMap; use aho_corasick::{Anchored, Input}; use itertools::Itertools; -use regex::Captures; +use regex::{Captures, Regex}; use crate::consts::{VARIANTS, VARIANT_SEARCHER}; use crate::defaults::{RE, SORTER}; -use crate::options::{FinderRegex, Options, Sorter}; + +#[derive(Debug)] +pub enum FinderRegex { + DefaultRegex, + CustomRegex(Regex), +} + +#[derive(Debug)] +pub enum Sorter { + DefaultSorter, + CustomSorter(HashMap), +} + +#[derive(Debug)] +pub struct Options { + pub regex: FinderRegex, + pub sorter: Sorter, + pub allow_duplicates: bool, +} pub fn has_classes(file_contents: &str, options: &Options) -> bool { let regex = match &options.regex { diff --git a/src/tests.rs b/rustywind-core/src/tests.rs similarity index 100% rename from src/tests.rs rename to rustywind-core/src/tests.rs diff --git a/tests/fixtures/tailwind.css b/rustywind-core/tests/fixtures/tailwind.css similarity index 100% rename from tests/fixtures/tailwind.css rename to rustywind-core/tests/fixtures/tailwind.css diff --git a/rustywind-vite/Cargo.toml b/rustywind-vite/Cargo.toml new file mode 100644 index 0000000..86fcc58 --- /dev/null +++ b/rustywind-vite/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "rustywind_vite" +version = "0.1.0" +description = "Additional functionality for rustywind_core specific to Vite" +documentation = "https://docs.rs/rustywind_vite" +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true + +[dependencies] +rustywind_core = { path = "../rustywind-core" } + +once_cell = { workspace = true } +regex = { workspace = true } +eyre = { workspace = true } +color-eyre = { workspace = true } +ureq = { workspace = true } +rustls = { workspace = true } diff --git a/rustywind-vite/src/lib.rs b/rustywind-vite/src/lib.rs new file mode 100644 index 0000000..ddee360 --- /dev/null +++ b/rustywind-vite/src/lib.rs @@ -0,0 +1,4 @@ +mod sorter; +mod tls; + +pub use sorter::create_sorter as create_vite_sorter; diff --git a/src/options/vite.rs b/rustywind-vite/src/sorter.rs similarity index 95% rename from src/options/vite.rs rename to rustywind-vite/src/sorter.rs index 9040f20..8610637 100644 --- a/src/options/vite.rs +++ b/rustywind-vite/src/sorter.rs @@ -4,11 +4,10 @@ use color_eyre::Help; use eyre::{Context, Result}; use once_cell::sync::Lazy; use regex::Regex; +use rustywind_core::{parser, sorter::Sorter}; use ureq::AgentBuilder; -use crate::{parser, tls::NoCertificateVerification}; - -use super::Sorter; +use crate::tls::NoCertificateVerification; static VITE_CSS_RE: Lazy = Lazy::new(|| Regex::new(r#"const __vite__css = "(.*)""#).unwrap()); diff --git a/src/tls.rs b/rustywind-vite/src/tls.rs similarity index 100% rename from src/tls.rs rename to rustywind-vite/src/tls.rs