| Crates.io | beet_rsx_combinator |
| lib.rs | beet_rsx_combinator |
| version | 0.0.6 |
| created_at | 2025-04-26 00:57:57.892241+00 |
| updated_at | 2025-07-25 22:51:55.504122+00 |
| description | JSX-like parser combinator for Rust |
| homepage | https://beetstack.dev |
| repository | https://github.com/mrchantey/beet |
| max_upload_size | |
| id | 1649733 |
| size | 192,241 |
JSX-like parser combinator for Rust, forked from Victor Porof's rsx-parser.
Where macro parsers like rstml work with rust tokens, It works on the level of strings, not rust tokens,
TokenStream parsers like rstml?TokenStream, ie padding: 2em;This is an experimental parser for JSX-like code in Rust. The long term goal might be to build "something like React" in Rust, but this can mean a number of things, from a direct port with 100% API compatibility to a completely different product. A JSX-like parser is a good and simple place to start experimenting from.
The JSX spec is, although a draft, presumably stable. Syntax extension equivalents can be found for Rust, which is the main scope of this experiment.
Example, inspired from the JSX spec website linked above:
const FunDropdown = (props) =>
<Dropdown show={props.visible}>
A dropdown list
<Menu
icon={props.menu.icon}
onHide={(e) => console.log(e)}
onShow={(e) => console.log(e)}
>
<MenuItem>Do Something</MenuItem>
{
shouldDoSomethingFun()
? <MenuItem>Do Something Fun!</MenuItem>
: <MenuItem>Do Something Else</MenuItem>
}
</Menu>
</Dropdown>;
An equivalent interpretation of JSX in Rust, using compiler plugins, looks this:
fn fun_dropdown(props: Props) -> RSXElement {
rsx! {
<Dropdown show={props.visible}>
A dropdown list
<Menu
icon={props.menu.icon}
onHide={|e: Event| println!("{:?}", e)}
onShow={|e: Event| println!("{:?}", e)}
>
<MenuItem>Do Something</MenuItem>
{
if should_do_something_fun() {
<MenuItem>Do Something Fun!</MenuItem>
} else {
<MenuItem>Do Something Else</MenuItem>
}
}
</Menu>
</Dropdown>
}
}
All of the JSX official grammar is supported. In the case of handling arbitrary Rust code inside RSX, the treatment is similar: JSX can contain JavaScript "code blocks" delimited by curly braces (specifically "assignment expressions"), in clearly defined locations such as attribute values, children contents etc. Rust expressions provide sufficient equivalence.
< JSXElementName JSXAttributes? / >< JSXElementName JSXAttributes? >< / JSXElementName >-: JSXIdentifier. JSXIdentifier. JSXIdentifier{ ... AssignmentExpression }= JSXAttributeValue" JSXDoubleStringCharacters? "' JSXSingleStringCharacters? '{ AssignmentExpression }"'{ AssignmentExpression? } SourceCharacter but not one of {, <, > or }
Copyright 2016 Mozilla Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.