Skip to main content

Crate swiftui_native_macros

Crate swiftui_native_macros 

Source
Expand description

swiftui! { … } — SwiftUI’s declarative syntax, lowered to this crate’s DSL.

The DSL underneath is already chainable, so what a macro adds is the part Rust has no syntax for: a body of children written one after another, with if and for inside it. Written by hand that is a vec![] of views with conditional(…) and for_each_items(…) spliced in, and the shape of the tree stops being visible.

ⓘ
swiftui! {
    VStack(spacing: 8) {
        Text("Title").font(.large_title)
        if show_detail {
            Text(&detail)
        }
        ForEach(items) { item in
            Text(item)
        }
    }
}

§What it does not do

It is a lowering, not a Swift parser. Names go through camelCase to snake_case and then have to exist in the DSL — Text becomes text, .foregroundStyle becomes .foreground_style.

That is deliberate rather than a shortcut: because the spans of the original tokens are kept, a name that does not exist gets rustc’s own diagnostic underlining the SwiftUI spelling and naming the Rust function it looked for — and for a near miss, rustc’s “there is a method padding with a similar name” does the rest. A macro that tried to validate names itself would have to carry a copy of the DSL’s surface and would go stale.

Containers are a table: the stacks know spacing: and alignment:, Section knows header: and footer:, and the rest take a body. A name not in the table falls through to snake_case(body), which is right for a container taking one view and reports a missing function otherwise.

Leading-dot syntax works: .large_title becomes swiftui_native::dot::LargeTitle.into(), and type checking picks the enum from the parameter’s type — the same inference Swift does, reached a different way. A proc macro sees tokens and not types, so it cannot resolve this itself, but it does not have to. See swiftui_native::dot.

Macros§

ltext
Text("Item \(i)") — a localisable Text, interpolated the way SwiftUI does.
stateful
Declare a view with property wrappers, by name.
swiftui
Lower SwiftUI-shaped syntax to the swiftui-native DSL.