A command-line program that writes the XAML codecs of MoonBit types from their attributes
// models.mbt
///|
#xaml.type(namespace="urn:tasks")
pub(all) struct Task {
#xaml.member(is_name=true)
id : String
title : String
#xaml.member(default=false)
done : Bool
after : Task?
#xaml.member(content=true)
tasks : Array[Task]
}<Task xmlns="urn:tasks" Title="Release">
<Task Done="True" Title="Publish"/>
</Task>import {
"moonrockz/xaml",
}
rule(
name: "xaml",
command: "moonx moonrockz/xaml-codegen@<version> $input -o $output",
)
dev_build(rule: "xaml", input: ["models.mbt"], output: "models.g.mbt")// Generated by xaml-codegen from models.mbt. Do not edit.
///|
let xaml_key_Task : @xaml.XamlKey[Task] = @xaml.XamlKey::new(
"urn:tasks", "Task",
)
///|
pub impl @xaml.Xaml for Task with fn xaml_codec() {
xaml_key_Task.object(object => {
let m_id = object.add_name("Id", it => it.id)
let m_title = object.add_member("Title", it => it.title)
let m_done = object.add_member("Done", it => it.done, default=false)
let m_after = object.add_member("After", it => it.after)
let m_tasks = object.add_list("Tasks", it => it.tasks, content=true)
members => Task::{
id: members.get(m_id),
title: members.get(m_title),
done: members.get(m_done),
after: members.get(m_after),
tasks: members.get(m_tasks),
}
})
}| Attribute | On | Arguments |
|---|---|---|
| #xaml.type | A struct or an enum | namespace, name |
| #xaml.member | A field, or a value of a case | name, default, content, is_name, of |
| #xaml.case | A case of an enum | name, inline |
| Type | Example |
|---|---|
| Bool | default=false |
| An enum | default=Low |
| A number | default="5", default="-1.5" |
| String, Char | default="none" |
| An optional one | default=Some("5"), default=None |
| Any of them, and all others | default=usual_limit, the name of a constant or a let |
// shapes.mbt
///|
#xaml.type(namespace="urn:shapes")
pub(all) struct Image {
source : String
}
///|
#xaml.type(namespace="urn:shapes")
pub(all) enum Shape {
Circle(radius~ : Double)
#xaml.member(of=height, default="1")
Rectangle(width~ : Double, height~ : Double)
Line(Double, Double)
#xaml.case(name="Text")
Label(String)
Group(Array[Shape])
#xaml.case(inline=true)
Picture(Image)
Empty
}<Group xmlns="urn:shapes">
<Circle Radius="1.5"/>
<Rectangle Width="2"/>
<Line Item1="3" Item2="4"/>
<Text>hi</Text>
<Image Source="logo.png"/>
<Empty/>
</Group>models.mbt:8:3: #xaml.member: there is no argument defualt, only name, default, content and is_nameUsage: xaml-codegen [--check] <input.mbt>... -o <output.mbt>moonx moonrockz/xaml-codegen@<version> --check models.mbt -o models.g.mbtformatter(ignore: [ "models.g.mbt" ])Install
Download zipA command-line program that writes the XAML codecs of MoonBit types from their attributes