lightspeed/component/template_compiler
Compile-time-style template schema validation with deterministic diagnostics.
Types
Attribute value kind for schema and diagnostics.
pub type AttrKind {
AttrString
AttrInt
AttrBool
}
Constructors
-
AttrString -
AttrInt -
AttrBool
Attribute requirement contract.
pub type AttrRequirement {
RequiredAttr
OptionalAttr(default: option.Option(AttrValue))
}
Constructors
-
RequiredAttr -
OptionalAttr(default: option.Option(AttrValue))
One attribute schema rule.
pub type AttrSchema {
AttrSchema(
name: String,
kind: AttrKind,
requirement: AttrRequirement,
)
}
Constructors
-
AttrSchema( name: String, kind: AttrKind, requirement: AttrRequirement, )
One concrete attribute value.
pub type AttrValue {
StringAttr(String)
IntAttr(Int)
BoolAttr(Bool)
}
Constructors
-
StringAttr(String) -
IntAttr(Int) -
BoolAttr(Bool)
One compiled template artifact.
pub type CompiledTemplate(assigns, slots) {
CompiledTemplate(
name: String,
attrs: List(#(String, AttrValue)),
slots: List(#(String, component.Rendered)),
assigns: assigns,
slot_assigns: slots,
rendered: component.Rendered,
signature: String,
)
}
Constructors
-
CompiledTemplate( name: String, attrs: List(#(String, AttrValue)), slots: List(#(String, component.Rendered)), assigns: assigns, slot_assigns: slots, rendered: component.Rendered, signature: String, )
Deterministic validation/compile diagnostics.
pub type Diagnostic {
MissingAttribute(template: String, attribute: String)
DuplicateAttribute(template: String, attribute: String)
UnknownAttribute(template: String, attribute: String)
InvalidAttributeType(
template: String,
attribute: String,
expected: AttrKind,
actual: AttrKind,
)
MissingSlot(template: String, slot: String)
DuplicateSlot(template: String, slot: String)
UnknownSlot(template: String, slot: String)
AssignBuildFailed(template: String, reason: String)
SlotBuildFailed(template: String, reason: String)
}
Constructors
-
MissingAttribute(template: String, attribute: String) -
DuplicateAttribute(template: String, attribute: String) -
UnknownAttribute(template: String, attribute: String) -
-
MissingSlot(template: String, slot: String) -
DuplicateSlot(template: String, slot: String) -
UnknownSlot(template: String, slot: String) -
AssignBuildFailed(template: String, reason: String) -
SlotBuildFailed(template: String, reason: String)
Template compilation schema with typed builders.
pub type Schema(assigns, slots) {
Schema(
name: String,
attrs: List(AttrSchema),
slots: List(SlotSchema),
build_assigns: fn(List(#(String, AttrValue))) -> Result(
assigns,
String,
),
build_slots: fn(List(#(String, component.Rendered))) -> Result(
slots,
String,
),
render: fn(assigns, slots) -> component.Rendered,
)
}
Constructors
-
Schema( name: String, attrs: List(AttrSchema), slots: List(SlotSchema), build_assigns: fn(List(#(String, AttrValue))) -> Result( assigns, String, ), build_slots: fn(List(#(String, component.Rendered))) -> Result( slots, String, ), render: fn(assigns, slots) -> component.Rendered, )
Slot requirement contract.
pub type SlotRequirement {
RequiredSlot
OptionalSlot
}
Constructors
-
RequiredSlot -
OptionalSlot
One slot schema rule.
pub type SlotSchema {
SlotSchema(name: String, requirement: SlotRequirement)
}
Constructors
-
SlotSchema(name: String, requirement: SlotRequirement)
Values
pub fn compile(
schema: Schema(assigns, slots),
attrs: List(#(String, AttrValue)),
slots: List(#(String, component.Rendered)),
) -> Result(CompiledTemplate(assigns, slots), List(Diagnostic))
Validate and compile one template invocation.
pub fn diagnostics_signature(
diagnostics: List(Diagnostic),
) -> String
Stable diagnostics signature.
pub fn fingerprint(
compiled: CompiledTemplate(assigns, slots),
) -> String
Compiled template fingerprint.
pub fn optional_bool(
name: String,
default: option.Option(Bool),
) -> AttrSchema
Build one optional bool attribute with optional default.
pub fn optional_int(
name: String,
default: option.Option(Int),
) -> AttrSchema
Build one optional int attribute with optional default.
pub fn optional_string(
name: String,
default: option.Option(String),
) -> AttrSchema
Build one optional string attribute with optional default.
pub fn rendered(
compiled: CompiledTemplate(assigns, slots),
) -> component.Rendered
Compiled template rendered output.
pub fn require_bool(
attrs: List(#(String, AttrValue)),
name: String,
) -> Result(Bool, String)
Find one required bool attribute from normalized attrs.
pub fn require_int(
attrs: List(#(String, AttrValue)),
name: String,
) -> Result(Int, String)
Find one required int attribute from normalized attrs.
pub fn require_slot(
slots: List(#(String, component.Rendered)),
name: String,
) -> Result(component.Rendered, String)
Find required slot by name with deterministic errors.
pub fn require_string(
attrs: List(#(String, AttrValue)),
name: String,
) -> Result(String, String)
Find one required string attribute from normalized attrs.
pub fn required_bool(name: String) -> AttrSchema
Build one required bool attribute rule.
pub fn required_string(name: String) -> AttrSchema
Build one required string attribute rule.
pub fn schema(
name: String,
attrs: List(AttrSchema),
slots: List(SlotSchema),
build_assigns: fn(List(#(String, AttrValue))) -> Result(
assigns,
String,
),
build_slots: fn(List(#(String, component.Rendered))) -> Result(
slots,
String,
),
render: fn(assigns, slots) -> component.Rendered,
) -> Schema(assigns, slots)
Build one template compilation schema.
pub fn signature(
compiled: CompiledTemplate(assigns, slots),
) -> String
Compiled template stable signature.
pub fn slot_rendered(
slots: List(#(String, component.Rendered)),
name: String,
) -> option.Option(component.Rendered)
Find optional slot by name.