ltext!() { /* proc-macro */ }Expand description
swiftui! { … } — SwiftUI’s declarative syntax, lowered to this crate’s DSL.
The DSL is already chainable, so what the macro adds is the part Rust has no
syntax for: a body of children written one after another, with if,
match and ForEach inside it.
A string literal in the macro is a format string, so values go in the text
the way Swift’s \(…) puts them there — Text("{count} of {total}"). Field
access works too, Text("{item.name}"), which format! refuses on its own.
$name passes a binding the way Swift passes a projected value —
Toggle("Dark", isOn: $dark) — and a leading dot names a case anywhere an
argument is taken, VStack(alignment: .leading).
Swift’s further trailing closures carry a label, and work here too:
Section { … } header: { … } and Button { … } label: { … }.
A trailing brace is content for a modifier that takes a view
— .overlay { Text("over") } — and an action for one that takes a callback,
.on_tap { count += 1 }. Swift tells those apart by the parameter’s type;
with no type to ask, the name does: on_*, task and refreshable take
actions. Swift’s { value in … } names the callback’s argument.
let view = swiftui! {
VStack(spacing: 8.0) {
Text("Title").text_style(.large_title)
if show_detail {
Text("the details")
}
match status {
Status::Ready(name) => Text("ready: {name}"),
_ => Text("waiting"),
}
ForEach(0..3) { i in
Text("row {i}")
}
ForEach(items) { item in
Text(item)
}
}
};See swiftui_native_macros for what
it does and does not translate.
Text("Item \(i)") — a localisable Text, interpolated the way SwiftUI does.
text! formats in Rust and hands SwiftUI a
literal key: Text("Item 3"), hasFormatting: false, no arguments. This
builds the key Swift’s own interpolation builds — "Item %lld" with one
argument — which is the difference between a string a translation can
reorder and one it cannot.
ltext!("Item {}", i) // Text("Item \(i)")
ltext!("{} of {}", done, total) // Text("\(done) of \(total)")Only {} is understood. A format spec like {:.2} is a Rust idea — the
specifier here is Swift’s, chosen by the argument’s type — so it is
rejected rather than silently ignored. Use text!
where the formatting matters more than the localisation.