• Stars
    star
    320
  • Rank 131,126 (Top 3 %)
  • Language
    Rust
  • Created about 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Advanced JSX-like templating for Rust

Under heavy research and development, please don't use this yet!

rsx

License: MPL 2.0 Build Status

A compiler plugin for using RSX (JSX-like syntax) as advanced templating and metaprogramming in Rust.

Made possible by the Self Tokenize library, a trait derive for transferring data structures outside of procedural macros from compile-time to run-time.

Take a look at the RSX DOM and RSX Stylesheet crates for the underlying types and implementations, or the RSX parser and Servo CSS parser parsing backends for the parser combinators. To convert these data structures into lower level rendering primitives, see RSX Layout and RSX Primitives, which integrate with Facebook's YOGA library and Servo's Graphics component for building a Servo WebRender-powered gfx::display_list::DisplayList. Finally, rendering to pixels is done via the RSX Renderers crate.

For quick and easy example demos, simply check out here.

Purpose

Documentation

This compiler plugin allows you to freely intertwine JSX-like syntax anywhere into your Rust code.

RSX implements all of the JSX grammar. The purpose and benefits of JSX and RSX are equivalent.

How to use

To get access to the rsx!, css! macros, add this to your Cargo.toml file:

[dependencies]
rsx = { git = "https://github.com/victorporof/rsx.git" }
rsx-primitives = { git = "https://github.com/victorporof/rsx-primitives.git" }

Then, simply import the library into your code and use the rsx!, css! macros to parse RSX and CSS into rsx_dom::DOMNode, or rsx_dom::Stylesheet data structures respectively.

For example:

#![feature(proc_macro)]

extern crate rsx;
extern crate rsx_primitives;

use rsx::{rsx, css};
use rsx_primitives::rsx_stylesheet::types::*;
use rsx_primitives::rsx_dom::types::*;

let stylesheet: Stylesheet = css! { .foo { padding: 1px; } };
let node: DOMNode = rsx! { <div>Hello world!</div> };

Here's some code rendering the first example from Facebook's YOGA library:

let stylesheet: Stylesheet = css! {
  .root {
    width: 500px;
    height: 120px;
    flex-direction: row;
    padding: 20px;
  }
  .image {
    width: 80px;
    margin-right: 20px;
  }
  .text {
    height: 25px;
    align-self: center;
    flex-grow: 1;
  }
};

let node: DOMNode = rsx! {
  <view style={stylesheet.take(".root")}>
    <image style={stylesheet.take(".image")} src="..." />
    <text style={stylesheet.take(".text")}>
      Hello world!
    </text>
  </view>
};

Composability

  • Mixing Rust and RSX is possible
  • Stylesheets can be included as separate CSS files.
  • Composing components is achieved through simple function calls (for now).

example.css

.root {
  width: 500px;
  height: 120px;
  flex-direction: row;
  padding: 20px;
}
.image {
  width: 80px;
  margin-right: 20px;
}
.text {
  height: 25px;
  align-self: center;
  flex-grow: 1;
}

example.rs

fn greeting_str(name: &str) -> String {
  format!("Hello {}!", name)
}

fn render_greeting(name: &str) -> DOMNode {
  let stylesheet = css!("example.css");

  rsx! {
    <text style={stylesheet.take(".text")}>
      { greeting_str(name) }
    </text>
  }
}

fn render_children(name: Option<&str>, image: DOMNode) -> DOMNode {
  rsx! {
    <view>
      { image }
      {
        match name {
          Some(ref n) => render_greeting(n),
          None => <text>No greetings!</text>
        }
      }
    </view>
  }
}

fn render_root() -> DOMNode {
  let stylesheet = css!("example.css");

  rsx! {
    <view style={stylesheet.take(".root")}>
      {
        let name = Some("world");
        let image = <image style={stylesheet.take(".image")} src="..." />;
        render_children(name, image)
      }
    </view>
  }
}

let node = render_root();

The css! macro returns a rsx_dom::Stylesheet instance (coming from the RSX Stylesheet library re-exported through the RSX DOM library), because parsing CSS happens at compile-time.

let styles: rsx_stylesheet::Stylesheet = css! { ... }

The rsx! macro returns a rsx_dom::DOMNode instance (coming from the RSX DOM library). The convertion is automatic between rsx_parser::RSXElement abstract syntax trees to the more convenient rsx_dom::DOMNode elements, because the AST is directly tokenized into a DOM tree to avoid any runtime work! Templating is thus a zero cost abstraction.

let node: rsx_dom::DOMNode = rsx! { ... }

More Repositories

1

Sublime-HTMLPrettify

HTML, CSS, JavaScript, JSON, React/JSX and Vue code formatter for Sublime Text 2 and 3 via node.js
JavaScript
1,924
star
2

Sublime-JSHint

JSHint Gutter for Sublime Text 2 and 3 via node.js
Python
688
star
3

Tilt

Tilt: a WebGL-based 3D visualization of a Webpage
JavaScript
488
star
4

firefox-restartless-template

Simple template project for developing restartless Firefox Developer Tools addons.
JavaScript
75
star
5

atom

Atom | Piano Roll 2: The scripting/styling documentation.
JavaScript
25
star
6

rsx-demo

Several demos for embedding RSX code
Swift
25
star
7

rsx-parser

JSX-like parser combinator for Rust
Rust
23
star
8

BeagleBone-SPI-UART

Enables SPI and UART (serial tty*) hardware ports on the BeagleBone Black
JavaScript
20
star
9

BDF.js

Simple library for reading Adobe Glyph Bitmap Distribution font files
JavaScript
15
star
10

MAX7219.js

Abstraction for the MAX7219 display driver controller
JavaScript
13
star
11

rsx-dom

Simple node tree abstraction layer over RSX syntax trees
Rust
13
star
12

rsx-layout

Flexbox layout engine for RSX DOM trees using YOGA
Rust
11
star
13

rsx-primitives

Basic component primitives necessary to render any RSX elements
Rust
10
star
14

servo-css-parser

Servo's CSS parser as a usable standalone crate
Rust
7
star
15

rsx-renderers

Several renderers for DisplayLists generated by the RSX Primitives library
JavaScript
7
star
16

rust-enum-str-derive

Self stringifying enum variants
Rust
5
star
17

reactenstein

Custom React reconciler using RSX as a backend
JavaScript
5
star
18

demo-angular

TypeScript
5
star
19

rsx-stylesheet

Simple style tree abstraction layer for RSX over Servo CSS stylesheets
Rust
4
star
20

javascript-AES

AES cipher implementation in JavaScript
JavaScript
3
star
21

rust-self-tokenize

Self tokenizing Rust data structures
Rust
2
star
22

noop-react-reconciler

A react reconciler that does absolutely nothing
JavaScript
2
star
23

rsx-shared

Traits and types shared between all RSX modules
Rust
2
star
24

rsx-resources

Resources cache for RSX
Rust
1
star
25

rsx-event-manager

Event manager for RSX
Rust
1
star
26

YouTube-Animated-Thumbnails

Firefox extension that animates the video thumbnails on YouTube
Shell
1
star
27

rsx-embedding

Several macros and helpers for embedding RSX Renderers on various platforms
Rust
1
star