turf/
lib.rs

1//! # turf 🌱
2//!
3//! `turf` allows you to build SCSS to CSS during compile time and inject those styles into your binary.
4//!
5//! [![Crates.io][crates-badge]][crates-url]
6//! [![Docs.rs][docs-badge]][docs-url]
7//! [![MIT licensed][lic-badge]][lic-url]
8//!
9//! [crates-badge]: https://img.shields.io/crates/v/turf.svg
10//! [crates-url]: https://crates.io/crates/turf
11//! [docs-badge]: https://img.shields.io/docsrs/turf/latest.svg?logo=docsdotrs&label=docs.rs
12//! [docs-url]: https://docs.rs/turf
13//! [lic-url]: https://github.com/myFavShrimp/turf/blob/master/LICENSE
14//! [lic-badge]: https://img.shields.io/badge/license-MIT-blue.svg
15//!
16//! **turf will:**
17//!
18//! - 🌿 transform your SCSS files into CSS with [grass](https://github.com/connorskees/grass/), right at compilation time
19//! - 🪴 generate unique and dynamic class names for your CSS during compilation
20//! - 🔬 minify and optimize your CSS using [lightningcss](https://github.com/parcel-bundler/lightningcss), ensuring compatibility with various browser targets
21//! - 🎨 inject the generated CSS into your binary, guaranteeing quick access to your styles whenever you need them
22//!
23//! ## Usage
24//!
25//! For a complete runnable example project, you can check out one of the examples:
26//!
27//! | [leptos-example](https://github.com/myFavShrimp/turf/tree/main/examples/leptos-example) | [yew-example](https://github.com/myFavShrimp/turf/tree/main/examples/yew-example) | [dioxus-example](https://github.com/myFavShrimp/turf/tree/main/examples/dioxus-example) | [axum-askama-htmx](https://github.com/myFavShrimp/turf/tree/main/examples/axum-askama-htmx) |
28//! | --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
29//!
30//! **1. Create SCSS styles for your application**
31//!
32//! ```scss
33//! // file at scss/file/path.scss
34//!
35//! .TopLevelClass {
36//!     color: red;
37//!
38//!     .SomeClass {
39//!         color: blue;
40//!     }
41//! }
42//! ```
43//!
44//! **2. Use the `style_sheet` macro to include the resulting CSS in your code**
45//!
46//! ```rust,ignore
47//! turf::style_sheet!("scss/file/path.scss");
48//! ```
49//!
50//! The macro from the above example will expand to the following code:
51//!
52//! ```rust
53//! static STYLE_SHEET: &'static str = "<style_sheet>";
54//! struct ClassName;
55//! impl ClassName {
56//!     pub const TOP_LEVEL_CLASS: &'static str = "<unique_class_name>";
57//!     pub const SOME_CLASS: &'static str = "<another_unique_class_name>";
58//! }
59//! ```
60//!
61//! **3. Use the `ClassName` struct and its associated constants to access the generated class names**
62//!
63//! ```rust,ignore
64//! let top_level_class_name = ClassName::TOP_LEVEL_CLASS;
65//! let some_class_name = ClassName::SOME_CLASS;
66//! ```
67//!
68//! ### Configuration
69//!
70//! The configuration for turf can be specified in the Cargo.toml file using the `[package.metadata.turf]` and `[package.metadata.turf-dev]` keys. This allows you to conveniently manage your SCSS compilation settings for both development and production builds within your project's manifest.
71//!
72//! Both profiles offer the exact same configuration options. However, if you haven't specified a `[package.metadata.turf-dev]` profile, the `[package.metadata.turf]` settings will also be applied to debug builds. This ensures consistency in the compilation process across different build types unless you explicitly define a separate configuration for the development profile.
73//!
74//! Example configuration:
75//!
76//! ```toml
77//! [package.metadata.turf]
78//! minify = true
79//! load_paths = ["path/to/scss/files", "path/to/other/scss/files"]
80//!
81//! [package.metadata.turf.class_names]
82//! template = "custom-<id>-<original_name>"
83//! excludes = ["exclude-this-class-please", "^abc-[123]{4}"]
84//!
85//! [package.metadata.turf.browser_targets]
86//! chrome = [80, 1, 2]
87//! firefox = 65
88//! safari = [12, 3]
89//!
90//! [package.metadata.turf.file_output]
91//! global_css_file_path = "path/to/global.css"
92//! separate_css_files_path = "dir/for/separate/css/"
93//! ```
94//!
95//! The following configuration options are available:
96//!
97//! - `minify` (default: `true`): Specifies whether the generated CSS should be minified or not. If set to true, the CSS output will be compressed and optimized for reduced file size. If set to false, the CSS output will be formatted with indentation and line breaks for improved readability.
98//!
99//! - `load_paths`: Specifies additional paths to search for SCSS files to include during compilation. It accepts a list of string values, where each value represents a directory path to be included. This option allows you to import SCSS files from multiple directories.
100//!
101//! - `browser_targets`: Defines the target browser versions for compatibility when generating CSS. It expects a structure that contains specific versions for different browsers. Each browser can have its own version specified.
102//!
103//! - `class_names`: Allows configuration of the CSS class name generation. It expects a structure that contains two values for generating CSS class names and excluding class names from the uniquification process.
104//!
105//! - `debug` (default: `false`): When set to true, this option will enable debug output of the read configuration and the generated CSS class names. This can be helpful for troubleshooting and understanding how the CSS is being generated.
106//!
107//! - `file_output`: Enables output of compiled CSS. It expects a structure that contains two values for a single global CSS file or separate CSS files for each compiled SCSS file.
108//!
109//! #### The `class_names` Key
110//!
111//! - `template` (default: `"class-<id>"`): Specifies the template for generating randomized CSS class names. The template can include placeholders to customize the output:
112//!     - `<id>` will be replaced with a unique identifier for each CSS class name
113//!     - `<original_name>` will be replaced with the original class name from the SCSS file
114//!     - `<name_hash>` will be replaced with the hash of the original class name from the SCSS file
115//!     - `<name_hash_short>` will be replaced with the first 5 characters of the hash of the original class name from the SCSS file
116//!     - `<style_sheet_hash>` will be replaced with the hash of the SCSS file
117//!     - `<style_sheet_hash_short>` will be replaced with the first 8 characters of the hash of the SCSS file
118//!
119//! - `excludes`: An array of regex patterns that exclude class names in your SCSS files from the class name uniquification process.
120//!
121//! #### The `file_output` Key
122//!
123//! - `global_css_file_path`: Specifies the file path for a global CSS file. If set, a CSS file will be created at the provided path, and all compiled styles will be written to this file. This allows you to have a single CSS file containing all the compiled styles.
124//!
125//! - `separate_css_files_path`: Specifies the directory path for separate CSS files. If set, all compiled CSS files will be saved in the specified directory. Each compiled SCSS file will have its corresponding CSS file in this directory, allowing for modular CSS management. The file name for inline SCSS style definitions will be a 64 bit hash that is computed from the original SCSS style.
126//!
127//! #### Browser Versions
128//!
129//! The available browsers are as follows:
130//!
131//! - android
132//! - chrome
133//! - edge
134//! - firefox
135//! - ie
136//! - ios_saf
137//! - opera
138//! - safari
139//! - samsung
140//!
141//! #### Browser Version Format
142//!
143//! Three formats are supported:
144//!
145//! | major | major.minor | major.minor.patch |
146//! | :---- | :---------- | :---------------- |
147//! | Use a single integer to specify the major version number. | Use an array `[major, minor]` to specify both the major and minor version numbers. | Use an array `[major, minor, patch]` to specify the major, minor, and patch version numbers. |
148//! | Example: `1` or `[1]` represent version `1.0.0` | Example: `[1, 2]` represents version `1.2.0` | Example: `[1, 2, 3]` represents version `1.2.3`. |
149//!
150//! ### Additional Macros
151//!
152//! turf provides a few additional macros for other use cases.
153//!
154//! #### The `style_sheet_values` Macro
155//!
156//! In some cases, it may be necessary to have a struct's instance to access the class names (for example when using turf in [askama](https://github.com/djc/askama) templates).
157//! The `turf::style_sheet_values` macro provides an alternative to directly including the resulting CSS and obtaining the associated class names. It returns a tuple of `(style_sheet: &'static str, class_names: struct)`.
158//!
159//! **Usage:**
160//!
161//! ```rust,ignore
162//! let (style_sheet, class_names) = turf::style_sheet_values!("path/to/style.scss");
163//! let some_class_name = class_names.some_class;
164//! ```
165//!
166//! #### The `inline_style_sheet` Macro
167//!
168//! If you don't want your style sheet to live in another file, you can use the `turf::inline_style_sheet` macro. It allows you to write inline SCSS which will then be compiled to CSS.
169//!     
170//! **Usage:**
171//!     
172//! ```rust,ignore
173//! turf::inline_style_sheet! {
174//!     .TopLevelClass {
175//!         color: red;
176//!         
177//!         .SomeClass {
178//!             color: blue;
179//!         }
180//!     }
181//! }
182//!
183//! // ...
184//!
185//! let some_class_name = ClassName::SOME_CLASS;
186//! ```
187//!
188//! #### The `inline_style_sheet_values` Macro
189//!
190//! This macro combines the functionality of both the `style_sheet_values` and `inline_style_sheet` macros. It allows you to write inline SCSS and returns an tuple of `(style_sheet: &'static str, class_names: struct)`.
191//!
192//! **Usage:**
193//!     
194//! ```rust,ignore
195//! let (style_sheet, class_names) = turf::inline_style_sheet_values! {
196//!     .TopLevelClass {
197//!         color: red;
198//!         
199//!         .SomeClass {
200//!             color: blue;
201//!         }
202//!     }
203//! };
204//! let some_class_name = class_names.some_class;
205//! ```
206
207/// Generates the static variable `STYLE_SHEET` and the `ClassName` struct with default settings or the settings specified in the `Cargo.toml`
208///
209/// **Usage:**
210///
211/// ```rust,ignore
212/// turf::style_sheet!("scss/file/path.scss");
213///
214/// let style_sheet_str = STYLE_SHEET;
215/// let some_class_name = ClassName::SOME_CLASS;
216/// ```
217pub use turf_macros::style_sheet;
218
219/// Returns a tuple of `(style_sheet: &'static str, class_names: struct)`
220///
221/// In some cases, it may be necessary to have a struct's instance (for example when using turf in [askama](https://github.com/djc/askama) templates).
222/// The `turf::style_sheet_values` macro simplifies the process of including the resulting CSS and obtaining the associated class names. It allows you to retrieve both the style sheet and the generated class names in a tuple.
223///
224/// **Usage:**
225///
226/// ```rust,ignore
227/// let (style_sheet, class_names) = turf::style_sheet_values!("path/to/style.scss");
228///
229/// let style_sheet_str = style_sheet;
230/// let some_class_name = class_names.some_class;
231/// ```
232pub use turf_macros::style_sheet_values;
233
234/// Generates the static variable `STYLE_SHEET` and the `ClassName` struct from inline SCSS styles with default settings or the settings specified in the `Cargo.toml`
235///
236/// If you don't want your style sheet to live in another file, you can use the `turf::inline_style_sheet` macro. It allows you to write inline SCSS which will then be compiled to CSS.
237///
238/// **Usage:**
239///
240/// ```rust,ignore
241/// turf::inline_style_sheet! {
242///     .TopLevelClass {
243///         color: red;
244///
245///         .SomeClass {
246///             color: blue;
247///         }
248///     }
249/// }
250///
251/// // ...
252///
253/// let some_class_name = ClassName::SOME_CLASS;
254/// ```
255pub use turf_macros::inline_style_sheet;
256
257/// Returns a tuple of `(style_sheet: &'static str, class_names: struct)` from inline SCSS styles
258///
259/// This macro combines the functionality of both the `style_sheet_values` and `inline_style_sheet` macros. It allows you to write inline SCSS and returns an tuple of `(style_sheet: &'static str, class_names: struct)`.
260///
261/// **Usage:**
262///     
263/// ```rust,ignore
264/// let (style_sheet, class_names) = turf::inline_style_sheet_values! {
265///     .TopLevelClass {
266///         color: red;
267///         
268///         .SomeClass {
269///             color: blue;
270///         }
271///     }
272/// };
273/// let some_class_name = class_names.some_class;
274/// ```
275pub use turf_macros::inline_style_sheet_values;