Asteracea is a web application framework aiming to combine the strengths of Angular and React while fully supporting Rust's lifetime model.
Note: Asteracea is experimental software.
While it appears to work well so far, there likely will be breaking changes to the template syntax.
Please use cargo-edit to always add the latest version of this library:
cargo add asteracea
-
Little boilerplate / Useful defaults
Most generated boilerplate code is adjusted automatically to what is required. For example, the signature of a component's
.render
method changes if aNode
is generated.There is still room for improvement here without sacrificing readability.
-
Co-location / DRY
Intent shouldn't need to be reiterated in multiple places (split declaration, initialisation and usage).
For now, short form captures nested in the component templates provide a way to centralise some semantics (similarly to React's Hooks but without their control flow limitations).
-
Robust code
Element names are statically checked against
lignin-schema
by default, but other schemata can be defined similarly. Empty elements like<br>
cannot contain children.Similar checks for attributes and event names are planned.
-
No default runtime
Asteracea components compile to plain Rust code with few dependencies, which helps keep bundles small.
Use
lignin-dom
orlignin-html
to transform renderedNode
trees into live user interfaces.
Additional examples can be found in the examples directory.
The most simple (Node
-rendering) component can be written like this:
asteracea::component! {
pub Empty()() -> Sync
[] // Empty node sequence
}
// Render into a bump allocator:
// This is generally only this explicit at the application root.
let mut bump = bumpalo::Bump::new();
let root = {
struct Root;
rhizome::sync::Node::new(core::any::TypeId::of::<Root>())
};
assert!(matches!(
Box::pin(Empty::new(root.as_ref(), Empty::new_args_builder().build()).unwrap())
.as_ref()
.render(&mut bump, Empty::render_args_builder().build())
.unwrap(),
lignin::Node::Multi(&[]) // Empty node sequence
));
VDOM Sync
-ness can be inferred (even transitively) at zero runtime cost by omitting -> Sync
(or -> !Sync
), except for components visible outside their crate.
A return type other than Node
can be specified after the render argument list:
asteracea::component! {
Unit(/* ::new arguments */)(/* .render arguments */) -> ()
{} // Empty Rust block
}
asteracea::component! {
Offset(base: usize)(offset: usize) -> usize
let pub self.base: usize = base; // ²
{ self.base + offset }
}
// This is generally only this explicit at the application root.
let mut bump = bumpalo::Bump::new();
let root = {
struct Root;
rhizome::sync::Node::new(core::any::TypeId::of::<Root>())
};
assert_eq!(
Box::pin(Unit::new(root.as_ref(), Unit::new_args_builder().build()).unwrap())
.as_ref()
.render(&mut bump, Unit::render_args_builder().build())
.unwrap(),
(),
);
assert_eq!(
Box::pin(Offset::new(root.as_ref(), Offset::new_args_builder().base(2).build()).unwrap())
.as_ref()
.render(&mut bump, Offset::render_args_builder().offset(3).build())
.unwrap(),
5,
);
² #2
For a relatively complex example, see this parametrised counter:
use asteracea::component;
use lignin::web::Event;
use std::cell::Cell;
fn schedule_render() { /* ... */ }
component! {
pub Counter(
/// The counter's starting value.
initial: i32,
priv step: i32, // field from argument
pub enabled: bool = true, // default parameter
)(
// optional argument;
// `class` is `Option<&'bump str>` only inside this component, not its API.
class?: &'bump str,
) -> !Sync // visible across crate-boundaries, so use explicit `Sync`ness
// shorthand capture; Defines a struct field.
let self.value = Cell::<i32>::new(initial);
<div
// conditional attribute from `Option<&'bump str>`
."class"? = {class}
// Anything within curlies is plain Rust.
"The current value is: " !(self.value()) <br>
<button
."disabled"? = {!self.enabled} // boolean attribute from `bool`
"+" !(self.step) // shorthand `bump_format` call
on bubble click = Self::on_click_plus
>
>
}
// Counter is a plain struct, so `impl` works as expected!
impl Counter {
pub fn value(&self) -> i32 {
self.value.get()
}
pub fn set_value(&self, value: i32) {
self.value.set(value);
}
// This may alternatively take a `*const Self` or `Pin<&Self>`.
// Inline handlers are also possible, but not much less verbose.
fn on_click_plus(&self, _: Event) {
self.value.set(self.value() + self.step);
schedule_render();
}
}
asteracea::component! {
CounterUser()()
<*Counter
*initial = {0} // parameters by name
*step = {1}
.class = {"custom-counter"} // without Some(…)
>
}
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING for more information.
Asteracea strictly follows Semantic Versioning 2.0.0 with the following exceptions:
- The minor version will not reset to 0 on major version changes (except for v1).
Consider it the global feature level. - The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
Consider it the global patch level.
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.
Note that dependencies of this crate may have a more lenient MSRV policy!
Please use cargo +nightly update -Z minimal-versions
in your automation if you don't generate Cargo.lock manually (or as necessary) and require support for a compiler older than current stable.