nigeleke / style4rs

Scoped CSS for Rust web frameworks like Leptos.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

style4rs

MIT License Language Build Coverage Version

Site | GitHub | API | Coverage Report

Scoped CSS for Rust web frameworks like Leptos.

Macro Description
style! Enable writing css inside rust functions directly.
style_str! Same as style! macro but returns the tuple (class_name, style_val).
style_sheet! Extract css from an external css file and importing that into the rust functions.
style_sheet_str! Same as style_sheet! macro but returns the tuple (class_name, style_val).

Acknowledgement

This crate owes its existence to the stylers repository created by Abishek P, without which this crate would never have been possible.

Background

The reason for this crate's development was because stylers has a reliance on the Rust nightly build. This bugged me and I wanted to see if an alternate approach was feasible.

Alternatives

If you're choosing between style4rs & stylers, the following comparision may help. Also, an additional crate styled exists, which requires stylist. At the time of writing[0] stylist depends on the nightly build too; they haven't been analysed further.

style4rs stylers styled / stylist Comments
Rust build Stable ✓ Nightly ☹ Nightly ☹
Minified CSS x ?
macros
style!
style_sheet! ?
style_str! ?
style_sheet_str! ?
css validation ✓+ ? [1]
Misc
custom raw_str function x ? [2]
Specific CSS handling
::deep Passed-through Handled ? [3]
@document Passed-through Handled ? [3]
Released ?
Release version Not in crates.io 1.0.0-alpha 0.2.0 / 0.13.0
  1. 29-Dec-2023
  2. style4rs highlights syntactic errors around the entire CSS block (with an error message described by lightningcss).
    stylers highlights errors at their precise line and also provides semantic checks / hints.
  3. A consequence of not supporting a raw_str function is not all valid CSS content is parsable if it conflicts with the rust parsing. E.g. content: "\hello" results in compile error, whereas content: "\\hello" results in css with \\ rather than the rust escaped \. Unicode escape sequences, such as content: "\01F44D" appear ok.
  4. The best approach for handling these is to be determined. At this stage, my other projects are unlikely to require this CSS. Feel free to raise an issue / use-case if deemed required.

Development

Nix can be used to set up a development environment.

Running > nix develop --impure will set up Rust with Vscode.

The core code uses the LightningCSS Parser.

Tests in this crate are derived from stylers (and modified to reflect CSS constructs supported differently here). Note that the low coverage stats are misleading; much of the testing relies on compile time and build time invocation and is not instrumented.

Usage

Macros

See style4rs macro documentation.

Builder

Create a build.rs file to merge all style! and style_sheet! css to the target css file.

See style4rs builder documentation.

See Build Scripts - The Cargo Book for further information about build.rs.

Cargo.toml

[package]
name = "style4rs-test"
version = "0.1.0"
edition = "2021"

[dependencies]
style4rs = { version = "*" }

[build-dependencies]
style4rs = { version = "*" }

About

Scoped CSS for Rust web frameworks like Leptos.

License:MIT License


Languages

Language:Rust 90.4%Language:CSS 9.1%Language:Nix 0.6%