a rusty way to implement css in rust
rusty-css offers a solution to create and export css styles in a familiar way, but without leaving the rust syntax. You can access and manipulate every value you define on an individual basis.
The identifiers are taken as-is and converted into strings, with the exception of the underscore token (_), which is converted into the minus token (-) when the struct is to be converted into its corresponding css values.
Example:
will be converted into
"css-property: css value"
regardless of the property names' or values' validity. If you have an error in your css, it will still compile!
Roadmap
- simple conversion from rust structs to inline css code
- more reliable extraction of numeric values inside of a String
- exporting structs as classes inside a style sheet
- support for queries
- validating the written css code at compile time
- automated implementation according to the css spec
- second layer implementation of a system with strict typing (such as enums for all possible units for a given property)
- more abstraction for less boilderplate
How to use
As of now this crate uses the bevy_reflect crate to convert the structs into css "property: value" strings, so all structs that you wish to convert must derive Reflect
use ;
to convert this struct into an inline css string we have to first implement the struct and its initial state. For this we implement the Style Trait from this crate.
use *;
now we can create an instance of ExampleStruct and convert it into a css inline string.
let example_struct = create;
let inline_css: String = example_struct.inline;
// "width: 4em; height: 2rem; background: rgb(69,13,37); transform: skewX(20deg) skewY(30deg);"
Developer experiance improvements
since it ca be hard to access values of a property that can take multiple values such as transform, we can instead implement a nested struct into our original struct. By doing so, the fields of the struct in the second layer will no longer be treated as if they're css properties but rather css fuctions that take an argument.
// so the skewX field doesn't throw a warning for being in snake case, which css uses
let example_struct = create;
let inline_css: String = example_struct.inline;
let skewX: String = example_struct.transform.skewX; // can access this field, wuhu!
// "width: 4em; height: 2rem; background: rgb(69,13,37); transform: skewX(20deg) skewY(30deg);"
The output is equivalend but whe can access the elements skewX and skewY fields individually now. Following that logic, you should be able to write the background fields value similarly, so lets try it:
let example_struct = create;
let inline_css: String = example_struct.inline;
// "width: 4em; height: 2rem; background: rgb(69,13,37);"
Works just fine!
You might have noticed that we're appending a lot of .to_string() calls. At scale this can become quite cumbersome, so i created the append_to_string crate, which helps with that.
Complete Example
with all that out of the way, here's what your code might look like:
use *;
use *;
use ;
// define all the structs we want to be css-ified
let example_struct = create;
let inline_css: String = example_struct.inline;
// "width: 4em; height: 2rem; background: rgb(69,13,37); transform: skewX(20deg) skewY(30deg);"
Crate implements: