@native-sdk/cli 0.0.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -0
- package/bin/native.js +156 -0
- package/build/app.zig +716 -0
- package/build.zig +2593 -0
- package/build.zig.zon +24 -0
- package/native-sdk.d.ts +615 -0
- package/package.json +44 -8
- package/skill-data/automation/SKILL.md +248 -0
- package/skill-data/core/SKILL.md +249 -0
- package/skill-data/core/references/app-model-runtime.md +141 -0
- package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
- package/skill-data/core/references/frontend-assets.md +124 -0
- package/skill-data/core/references/project-anatomy.md +108 -0
- package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
- package/skill-data/native-ui/SKILL.md +1080 -0
- package/skills/native-sdk/SKILL.md +33 -0
- package/src/app_runner/root.zig +740 -0
- package/src/assets/root.zig +20 -0
- package/src/automation/protocol.zig +285 -0
- package/src/automation/root.zig +13 -0
- package/src/automation/server.zig +506 -0
- package/src/automation/snapshot.zig +1202 -0
- package/src/automation/watcher.zig +165 -0
- package/src/bridge/root.zig +507 -0
- package/src/debug/root.zig +246 -0
- package/src/embed/app_exports.zig +11 -0
- package/src/embed/c_api.zig +793 -0
- package/src/embed/c_exports.zig +10 -0
- package/src/embed/chrome.zig +168 -0
- package/src/embed/conversions.zig +359 -0
- package/src/embed/host.zig +960 -0
- package/src/embed/root.zig +81 -0
- package/src/embed/tests.zig +2552 -0
- package/src/embed/types.zig +382 -0
- package/src/embed/ui_host.zig +289 -0
- package/src/extensions/root.zig +197 -0
- package/src/frontend/root.zig +48 -0
- package/src/js/root.zig +65 -0
- package/src/platform/android/NativeSdkActivity.java +1370 -0
- package/src/platform/android/android_host.c +696 -0
- package/src/platform/android/files.zig +9 -0
- package/src/platform/android/native_sdk_app.h +237 -0
- package/src/platform/ios/files.zig +8 -0
- package/src/platform/ios/native_sdk_app.h +289 -0
- package/src/platform/ios/uikit_host.m +2739 -0
- package/src/platform/linux/cef_host.cpp +599 -0
- package/src/platform/linux/gtk_host.c +5362 -0
- package/src/platform/linux/gtk_host.h +265 -0
- package/src/platform/linux/root.zig +1519 -0
- package/src/platform/macos/appkit_host.h +604 -0
- package/src/platform/macos/appkit_host.m +10392 -0
- package/src/platform/macos/automation_host.h +14 -0
- package/src/platform/macos/automation_host.m +12 -0
- package/src/platform/macos/cef_host.mm +3028 -0
- package/src/platform/macos/root.zig +2260 -0
- package/src/platform/null_platform.zig +2232 -0
- package/src/platform/null_platform_tests.zig +670 -0
- package/src/platform/policy_values.zig +14 -0
- package/src/platform/root.zig +181 -0
- package/src/platform/types.zig +2681 -0
- package/src/platform/windows/cef_host.cpp +3 -0
- package/src/platform/windows/root.zig +1521 -0
- package/src/platform/windows/webview2_host.cpp +6230 -0
- package/src/primitives/app_dirs/root.zig +440 -0
- package/src/primitives/app_manifest/root.zig +109 -0
- package/src/primitives/app_manifest/tests.zig +758 -0
- package/src/primitives/app_manifest/types.zig +587 -0
- package/src/primitives/app_manifest/validation.zig +828 -0
- package/src/primitives/assets/root.zig +520 -0
- package/src/primitives/canvas/a11y_audit.zig +390 -0
- package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
- package/src/primitives/canvas/app_icon.zig +1590 -0
- package/src/primitives/canvas/chart.zig +392 -0
- package/src/primitives/canvas/chart_tests.zig +751 -0
- package/src/primitives/canvas/commands.zig +446 -0
- package/src/primitives/canvas/drawing.zig +309 -0
- package/src/primitives/canvas/equality.zig +316 -0
- package/src/primitives/canvas/events.zig +696 -0
- package/src/primitives/canvas/font_coverage.zig +118 -0
- package/src/primitives/canvas/font_ttf.zig +510 -0
- package/src/primitives/canvas/font_ttf_tests.zig +280 -0
- package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
- package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
- package/src/primitives/canvas/frame.zig +679 -0
- package/src/primitives/canvas/frame_metrics.zig +462 -0
- package/src/primitives/canvas/gpu.zig +639 -0
- package/src/primitives/canvas/hash.zig +112 -0
- package/src/primitives/canvas/icons/alert.svg +1 -0
- package/src/primitives/canvas/icons/archive.svg +1 -0
- package/src/primitives/canvas/icons/arrow-down.svg +1 -0
- package/src/primitives/canvas/icons/arrow-right.svg +1 -0
- package/src/primitives/canvas/icons/arrow-up.svg +1 -0
- package/src/primitives/canvas/icons/check-circle.svg +1 -0
- package/src/primitives/canvas/icons/check.svg +1 -0
- package/src/primitives/canvas/icons/chevron-down.svg +1 -0
- package/src/primitives/canvas/icons/chevron-left.svg +1 -0
- package/src/primitives/canvas/icons/chevron-right.svg +1 -0
- package/src/primitives/canvas/icons/chevron-up.svg +1 -0
- package/src/primitives/canvas/icons/circle-dot.svg +1 -0
- package/src/primitives/canvas/icons/clock.svg +1 -0
- package/src/primitives/canvas/icons/copy.svg +1 -0
- package/src/primitives/canvas/icons/download.svg +1 -0
- package/src/primitives/canvas/icons/edit.svg +1 -0
- package/src/primitives/canvas/icons/ellipsis.svg +1 -0
- package/src/primitives/canvas/icons/external-link.svg +1 -0
- package/src/primitives/canvas/icons/eye.svg +1 -0
- package/src/primitives/canvas/icons/file-text.svg +1 -0
- package/src/primitives/canvas/icons/folder-open.svg +1 -0
- package/src/primitives/canvas/icons/folder.svg +1 -0
- package/src/primitives/canvas/icons/git-branch.svg +1 -0
- package/src/primitives/canvas/icons/git-merge.svg +1 -0
- package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
- package/src/primitives/canvas/icons/info.svg +1 -0
- package/src/primitives/canvas/icons/menu.svg +1 -0
- package/src/primitives/canvas/icons/missing.svg +1 -0
- package/src/primitives/canvas/icons/moon.svg +1 -0
- package/src/primitives/canvas/icons/music.svg +1 -0
- package/src/primitives/canvas/icons/panel-left.svg +1 -0
- package/src/primitives/canvas/icons/panel-right.svg +1 -0
- package/src/primitives/canvas/icons/pause.svg +1 -0
- package/src/primitives/canvas/icons/play.svg +1 -0
- package/src/primitives/canvas/icons/plus.svg +1 -0
- package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
- package/src/primitives/canvas/icons/repeat.svg +1 -0
- package/src/primitives/canvas/icons/save.svg +1 -0
- package/src/primitives/canvas/icons/search.svg +1 -0
- package/src/primitives/canvas/icons/send.svg +1 -0
- package/src/primitives/canvas/icons/settings.svg +1 -0
- package/src/primitives/canvas/icons/shuffle.svg +1 -0
- package/src/primitives/canvas/icons/skip-back.svg +1 -0
- package/src/primitives/canvas/icons/skip-forward.svg +1 -0
- package/src/primitives/canvas/icons/sun.svg +1 -0
- package/src/primitives/canvas/icons/terminal.svg +1 -0
- package/src/primitives/canvas/icons/trash.svg +1 -0
- package/src/primitives/canvas/icons/volume.svg +1 -0
- package/src/primitives/canvas/icons/wrench.svg +1 -0
- package/src/primitives/canvas/icons/x-circle.svg +1 -0
- package/src/primitives/canvas/icons/x.svg +1 -0
- package/src/primitives/canvas/icons.zig +249 -0
- package/src/primitives/canvas/layout_audit.zig +744 -0
- package/src/primitives/canvas/layout_audit_tests.zig +426 -0
- package/src/primitives/canvas/markdown.zig +1031 -0
- package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
- package/src/primitives/canvas/markdown_tests.zig +538 -0
- package/src/primitives/canvas/plan_key_index.zig +120 -0
- package/src/primitives/canvas/png.zig +355 -0
- package/src/primitives/canvas/reference.zig +1457 -0
- package/src/primitives/canvas/reference_blur.zig +104 -0
- package/src/primitives/canvas/reference_memo.zig +322 -0
- package/src/primitives/canvas/reference_paths.zig +184 -0
- package/src/primitives/canvas/reference_tests.zig +1650 -0
- package/src/primitives/canvas/render.zig +747 -0
- package/src/primitives/canvas/render_effects.zig +255 -0
- package/src/primitives/canvas/render_fingerprints.zig +256 -0
- package/src/primitives/canvas/render_generic_resources.zig +376 -0
- package/src/primitives/canvas/render_images.zig +247 -0
- package/src/primitives/canvas/render_layers.zig +260 -0
- package/src/primitives/canvas/render_paths.zig +347 -0
- package/src/primitives/canvas/render_resources.zig +51 -0
- package/src/primitives/canvas/render_tests.zig +3186 -0
- package/src/primitives/canvas/root.zig +744 -0
- package/src/primitives/canvas/serialization.zig +1528 -0
- package/src/primitives/canvas/svg_icon.zig +636 -0
- package/src/primitives/canvas/svg_icon_tests.zig +312 -0
- package/src/primitives/canvas/test_support.zig +422 -0
- package/src/primitives/canvas/testdata/markdown_document.md +83 -0
- package/src/primitives/canvas/tests.zig +30 -0
- package/src/primitives/canvas/text.zig +82 -0
- package/src/primitives/canvas/text_atlas.zig +408 -0
- package/src/primitives/canvas/text_batch_tests.zig +431 -0
- package/src/primitives/canvas/text_interaction.zig +615 -0
- package/src/primitives/canvas/text_layout.zig +1069 -0
- package/src/primitives/canvas/text_layout_cache.zig +281 -0
- package/src/primitives/canvas/text_layout_hash.zig +113 -0
- package/src/primitives/canvas/text_layout_types.zig +128 -0
- package/src/primitives/canvas/text_measure_cache.zig +279 -0
- package/src/primitives/canvas/text_metrics.zig +309 -0
- package/src/primitives/canvas/text_metrics_tests.zig +262 -0
- package/src/primitives/canvas/text_span_tests.zig +608 -0
- package/src/primitives/canvas/text_spans.zig +1010 -0
- package/src/primitives/canvas/text_tests.zig +1949 -0
- package/src/primitives/canvas/themes/geist.zig +470 -0
- package/src/primitives/canvas/tokens.zig +1522 -0
- package/src/primitives/canvas/ui.zig +2686 -0
- package/src/primitives/canvas/ui_markup.zig +3791 -0
- package/src/primitives/canvas/ui_markup_binary.zig +522 -0
- package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
- package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
- package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
- package/src/primitives/canvas/ui_markup_edit.zig +416 -0
- package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
- package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
- package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
- package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
- package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
- package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
- package/src/primitives/canvas/ui_markup_view.zig +2352 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
- package/src/primitives/canvas/ui_provenance.zig +96 -0
- package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
- package/src/primitives/canvas/ui_schema.zig +825 -0
- package/src/primitives/canvas/ui_schema_tests.zig +128 -0
- package/src/primitives/canvas/ui_tests.zig +1432 -0
- package/src/primitives/canvas/vector.zig +919 -0
- package/src/primitives/canvas/vector_tests.zig +547 -0
- package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
- package/src/primitives/canvas/virtual_extents.zig +540 -0
- package/src/primitives/canvas/widget_access.zig +208 -0
- package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
- package/src/primitives/canvas/widget_invalidation.zig +748 -0
- package/src/primitives/canvas/widget_layout.zig +2055 -0
- package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
- package/src/primitives/canvas/widget_metrics.zig +228 -0
- package/src/primitives/canvas/widget_render.zig +2534 -0
- package/src/primitives/canvas/widget_render_controls.zig +1563 -0
- package/src/primitives/canvas/widget_render_scroll.zig +136 -0
- package/src/primitives/canvas/widget_render_style.zig +626 -0
- package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
- package/src/primitives/canvas/widget_routing.zig +475 -0
- package/src/primitives/canvas/widget_runtime.zig +307 -0
- package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
- package/src/primitives/canvas/widget_semantics.zig +480 -0
- package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
- package/src/primitives/canvas/widget_tests.zig +8 -0
- package/src/primitives/canvas/widget_text_input.zig +351 -0
- package/src/primitives/canvas/widget_text_select.zig +147 -0
- package/src/primitives/canvas/widget_tree.zig +232 -0
- package/src/primitives/canvas/widgets.zig +1319 -0
- package/src/primitives/diagnostics/root.zig +445 -0
- package/src/primitives/geometry/root.zig +889 -0
- package/src/primitives/json/root.zig +272 -0
- package/src/primitives/platform_info/root.zig +450 -0
- package/src/primitives/trace/root.zig +569 -0
- package/src/root.zig +165 -0
- package/src/runtime/api.zig +431 -0
- package/src/runtime/async_bridge.zig +49 -0
- package/src/runtime/automation_commands.zig +686 -0
- package/src/runtime/automation_liveness_tests.zig +140 -0
- package/src/runtime/automation_snapshot.zig +246 -0
- package/src/runtime/automation_widget_dispatch.zig +617 -0
- package/src/runtime/bridge_dispatch_tests.zig +202 -0
- package/src/runtime/bridge_payload.zig +194 -0
- package/src/runtime/bridge_permission_tests.zig +551 -0
- package/src/runtime/bridge_responses.zig +414 -0
- package/src/runtime/builtin_bridge.zig +554 -0
- package/src/runtime/canvas_budget_edge_tests.zig +415 -0
- package/src/runtime/canvas_font_tests.zig +410 -0
- package/src/runtime/canvas_fonts.zig +229 -0
- package/src/runtime/canvas_frame.zig +1734 -0
- package/src/runtime/canvas_frame_animation_tests.zig +609 -0
- package/src/runtime/canvas_frame_cache_tests.zig +333 -0
- package/src/runtime/canvas_frame_helpers.zig +414 -0
- package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
- package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
- package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
- package/src/runtime/canvas_frame_retained_tests.zig +753 -0
- package/src/runtime/canvas_frame_tests.zig +9 -0
- package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
- package/src/runtime/canvas_image_tests.zig +362 -0
- package/src/runtime/canvas_images.zig +224 -0
- package/src/runtime/canvas_limits.zig +317 -0
- package/src/runtime/canvas_screenshot_tests.zig +289 -0
- package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
- package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
- package/src/runtime/canvas_widget_chart_tests.zig +249 -0
- package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
- package/src/runtime/canvas_widget_context_menu.zig +318 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
- package/src/runtime/canvas_widget_control_tests.zig +2164 -0
- package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
- package/src/runtime/canvas_widget_display.zig +677 -0
- package/src/runtime/canvas_widget_event_tests.zig +1779 -0
- package/src/runtime/canvas_widget_events.zig +1098 -0
- package/src/runtime/canvas_widget_floating_tests.zig +695 -0
- package/src/runtime/canvas_widget_layout_tests.zig +26 -0
- package/src/runtime/canvas_widget_runtime.zig +1335 -0
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
- package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
- package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
- package/src/runtime/canvas_widget_state.zig +798 -0
- package/src/runtime/canvas_widget_text_tests.zig +2104 -0
- package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
- package/src/runtime/clock.zig +231 -0
- package/src/runtime/core.zig +927 -0
- package/src/runtime/effects.zig +4197 -0
- package/src/runtime/effects_audio_tests.zig +690 -0
- package/src/runtime/effects_clipboard_tests.zig +317 -0
- package/src/runtime/effects_fetch_tests.zig +1031 -0
- package/src/runtime/effects_file_tests.zig +407 -0
- package/src/runtime/effects_tests.zig +1251 -0
- package/src/runtime/flow.zig +1175 -0
- package/src/runtime/frame_profile.zig +221 -0
- package/src/runtime/gpu_surface_events.zig +639 -0
- package/src/runtime/launch_timing.zig +59 -0
- package/src/runtime/markdown_app_tests.zig +224 -0
- package/src/runtime/native_command_event_tests.zig +431 -0
- package/src/runtime/platform_bridge_tests.zig +8 -0
- package/src/runtime/platform_config_tests.zig +392 -0
- package/src/runtime/root.zig +150 -0
- package/src/runtime/runtime_event_tests.zig +1079 -0
- package/src/runtime/session_journal.zig +1522 -0
- package/src/runtime/session_record.zig +332 -0
- package/src/runtime/session_replay.zig +274 -0
- package/src/runtime/session_state.zig +30 -0
- package/src/runtime/session_tests.zig +399 -0
- package/src/runtime/shell_layout.zig +472 -0
- package/src/runtime/shell_layout_tests.zig +1246 -0
- package/src/runtime/shell_view_tests.zig +604 -0
- package/src/runtime/state.zig +110 -0
- package/src/runtime/system_services.zig +392 -0
- package/src/runtime/test_support.zig +79 -0
- package/src/runtime/tests.zig +30 -0
- package/src/runtime/ui_app.zig +3290 -0
- package/src/runtime/ui_app_huge_model_tests.zig +351 -0
- package/src/runtime/ui_app_provenance.zig +370 -0
- package/src/runtime/ui_app_tests.zig +3254 -0
- package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
- package/src/runtime/ui_app_window_tests.zig +442 -0
- package/src/runtime/validation.zig +206 -0
- package/src/runtime/view.zig +908 -0
- package/src/runtime/view_canvas.zig +850 -0
- package/src/runtime/view_widget_control.zig +433 -0
- package/src/runtime/view_widget_scroll.zig +360 -0
- package/src/runtime/view_widget_text.zig +275 -0
- package/src/runtime/view_widget_tree.zig +843 -0
- package/src/runtime/webview_view_bridge_tests.zig +664 -0
- package/src/runtime/widget_bridge.zig +231 -0
- package/src/runtime/window_command_bridge_tests.zig +403 -0
- package/src/runtime/window_storage.zig +255 -0
- package/src/runtime/window_view_runtime.zig +727 -0
- package/src/runtime/window_views.zig +334 -0
- package/src/security/root.zig +118 -0
- package/src/tooling/android.zig +1172 -0
- package/src/tooling/assets.zig +128 -0
- package/src/tooling/buildgraph.zig +379 -0
- package/src/tooling/cef.zig +671 -0
- package/src/tooling/codesign.zig +172 -0
- package/src/tooling/components/identity_tests.zig +185 -0
- package/src/tooling/components/stepper.zig +113 -0
- package/src/tooling/components/timeline.native +27 -0
- package/src/tooling/components/timeline_item.zig +131 -0
- package/src/tooling/default_icon.icns +0 -0
- package/src/tooling/default_icon.png +0 -0
- package/src/tooling/dev.zig +172 -0
- package/src/tooling/doctor.zig +349 -0
- package/src/tooling/eject_components.zig +170 -0
- package/src/tooling/embedlib.zig +290 -0
- package/src/tooling/ios.zig +528 -0
- package/src/tooling/manifest.zig +1939 -0
- package/src/tooling/package.zig +2388 -0
- package/src/tooling/process_tree.zig +114 -0
- package/src/tooling/raw_manifest.zig +193 -0
- package/src/tooling/root.zig +22 -0
- package/src/tooling/templates.zig +3191 -0
- package/src/tooling/toolchain.zig +371 -0
- package/src/tooling/verbs.zig +204 -0
- package/src/tooling/web_engine.zig +135 -0
- package/src/tooling/xcodeproj.zig +462 -0
- package/src/window_state/root.zig +507 -0
- package/index.js +0 -2
|
@@ -0,0 +1,1620 @@
|
|
|
1
|
+
//! The model–view contract, checked in both directions at check time.
|
|
2
|
+
//!
|
|
3
|
+
//! The comptime engine already proves view→model at app build time and the
|
|
4
|
+
//! interpreter at view build time; this module makes the same contract
|
|
5
|
+
//! available to `native check` in milliseconds, without compiling the app:
|
|
6
|
+
//!
|
|
7
|
+
//! - `describe` reflects a concrete Model/Msg into a serializable
|
|
8
|
+
//! `Contract` — every scalar binding a view could resolve (fields,
|
|
9
|
+
//! public zero-arg methods, arena-taking scalar methods, nested struct
|
|
10
|
+
//! fields), every iterable `for each` could name, and every Msg tag with
|
|
11
|
+
//! its payload class. The reflection walks the SAME predicates the
|
|
12
|
+
//! engines resolve with (ui_markup_reflect.zig), so the three passes
|
|
13
|
+
//! cannot disagree about what markup can bind.
|
|
14
|
+
//! - `emitMain` is the body of the per-app `zig build model-contract`
|
|
15
|
+
//! step: it serializes the contract as ZON into
|
|
16
|
+
//! `zig-out/model-contract.zon` together with a hash over the app's Zig
|
|
17
|
+
//! sources, so a stale artifact is detectable instead of a false pass.
|
|
18
|
+
//! - `checkDocument` walks a RESOLVED markup document (imports merged,
|
|
19
|
+
//! `validate` already green) against a contract: binding paths,
|
|
20
|
+
//! iterables and their key fields, message tags and payload classes,
|
|
21
|
+
//! `app:` icon references against the registered icon table, and
|
|
22
|
+
//! the typed expression layer with real binding kinds — the same
|
|
23
|
+
//! accept/reject set as the engines, one grammar earlier in the loop.
|
|
24
|
+
//! Template args are part of a template's interface: use-site argument
|
|
25
|
+
//! kinds flow into the body, and slot content checks in the consumer's
|
|
26
|
+
//! scope, exactly like both engines build.
|
|
27
|
+
//! - `deadState` is the reverse direction: model state and Msg tags no
|
|
28
|
+
//! checked view binds or dispatches, reported as WARNINGS (fields that
|
|
29
|
+
//! only update/fx logic reads are legitimate — the opt-out is a
|
|
30
|
+
//! `pub const view_unbound = .{ "name", ... };` declaration on Model or
|
|
31
|
+
//! Msg). Warnings never fail a check unless the caller asks (the CLI's
|
|
32
|
+
//! --strict), which is the promotion hook.
|
|
33
|
+
//!
|
|
34
|
+
//! std-only by design: `native check` links this module standalone, and
|
|
35
|
+
//! the emit program needs nothing beyond the app's own types.
|
|
36
|
+
|
|
37
|
+
const std = @import("std");
|
|
38
|
+
const markup = @import("ui_markup.zig");
|
|
39
|
+
const schema = @import("ui_schema.zig");
|
|
40
|
+
const expr = @import("ui_markup_expr.zig");
|
|
41
|
+
const reflect = @import("ui_markup_reflect.zig");
|
|
42
|
+
|
|
43
|
+
pub const ValueKind = expr.ValueKind;
|
|
44
|
+
|
|
45
|
+
// ----------------------------------------------------------- the contract
|
|
46
|
+
|
|
47
|
+
/// Bumped when the artifact layout or its checking semantics change; a
|
|
48
|
+
/// reader refuses versions it does not know (loudly, degrading to
|
|
49
|
+
/// structural checks — never a false pass).
|
|
50
|
+
pub const format_version: u32 = 1;
|
|
51
|
+
|
|
52
|
+
/// Where the app's build step writes the artifact, relative to the app
|
|
53
|
+
/// directory (a build product lives under zig-out, not in durable state).
|
|
54
|
+
pub const default_artifact_path = "zig-out/model-contract.zon";
|
|
55
|
+
pub const default_source_root = "src";
|
|
56
|
+
|
|
57
|
+
/// The dead-state opt-out spelling: a public declaration listing names
|
|
58
|
+
/// that are intentionally unbound by views (update-only fields, Msg tags
|
|
59
|
+
/// only Zig code dispatches). A tuple of string literals keeps it
|
|
60
|
+
/// invisible to the binding engines.
|
|
61
|
+
pub const opt_out_decl = "view_unbound";
|
|
62
|
+
|
|
63
|
+
/// The app-icon table spelling: `pub const app_icons` on the app root —
|
|
64
|
+
/// the same static table `main` hands to `canvas.icons.registerAppIcons`
|
|
65
|
+
/// at boot, so the registered vocabulary and the contract's copy cannot
|
|
66
|
+
/// drift (one declaration feeds both). Entries only need a `name` field
|
|
67
|
+
/// here; this module never touches the parsed icon data.
|
|
68
|
+
pub const app_icons_decl = "app_icons";
|
|
69
|
+
|
|
70
|
+
/// The markup spelling of an app-icon reference (`app:<name>`); mirrors
|
|
71
|
+
/// `ui_markup.app_icon_prefix` (this layer stays std-only, and the
|
|
72
|
+
/// conformance suite holds the two equal).
|
|
73
|
+
pub const app_icon_prefix = "app:";
|
|
74
|
+
|
|
75
|
+
/// Reflect the app root's `pub const app_icons` table into the name list
|
|
76
|
+
/// the contract carries. Duck-typed on `.name` so the Entry type stays
|
|
77
|
+
/// the canvas layer's business; absent decl means "no registered icons".
|
|
78
|
+
pub fn appIconNames(comptime app: type) []const []const u8 {
|
|
79
|
+
comptime {
|
|
80
|
+
if (!@hasDecl(app, app_icons_decl)) return &.{};
|
|
81
|
+
var names: []const []const u8 = &.{};
|
|
82
|
+
for (@field(app, app_icons_decl)) |entry| {
|
|
83
|
+
names = names ++ &[_][]const u8{entry.name};
|
|
84
|
+
}
|
|
85
|
+
return names;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// One scalar binding leaf: a field, a public zero-arg method, or an
|
|
90
|
+
/// arena-taking scalar method.
|
|
91
|
+
pub const Scalar = struct {
|
|
92
|
+
name: []const u8,
|
|
93
|
+
/// Null when only runtime-known (an optional resolves to its inner
|
|
94
|
+
/// kind or boolean false).
|
|
95
|
+
kind: ?ValueKind = null,
|
|
96
|
+
type_name: []const u8 = "",
|
|
97
|
+
/// Arena-computed (`fn (*const T, std.mem.Allocator) V`): allowed
|
|
98
|
+
/// everywhere a scalar binding is, except as `==` comparison operands.
|
|
99
|
+
arena: bool = false,
|
|
100
|
+
fn_backed: bool = false,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
pub const NamedGroup = struct {
|
|
104
|
+
name: []const u8,
|
|
105
|
+
type_name: []const u8 = "",
|
|
106
|
+
group: Group = .{},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/// One struct type's binding surface: scalar leaves plus nested struct
|
|
110
|
+
/// fields a dotted path can traverse.
|
|
111
|
+
pub const Group = struct {
|
|
112
|
+
scalars: []const Scalar = &.{},
|
|
113
|
+
groups: []const NamedGroup = &.{},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/// One `for each` source: a slice/array field, a public slice/array
|
|
117
|
+
/// declaration, or a public fn returning a slice (with or without arena).
|
|
118
|
+
pub const Iterable = struct {
|
|
119
|
+
name: []const u8,
|
|
120
|
+
item_type: []const u8 = "",
|
|
121
|
+
/// Kind of the bare loop variable when the item type is itself a
|
|
122
|
+
/// scalar (a []const []const u8 iterates strings); null for structs.
|
|
123
|
+
item_kind: ?ValueKind = null,
|
|
124
|
+
item_scalar: bool = false,
|
|
125
|
+
item: Group = .{},
|
|
126
|
+
fn_backed: bool = false,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/// Payload classes a markup dispatch can (or cannot) construct. The
|
|
130
|
+
/// special classes match the engines exactly: text_input/scroll_state
|
|
131
|
+
/// tags bind through on-input/on-scroll only, and `unsupported` payloads
|
|
132
|
+
/// cannot be built from markup at all.
|
|
133
|
+
pub const PayloadClass = enum { none, string, integer, float, boolean, enum_tag, text_input, scroll_state, unsupported };
|
|
134
|
+
|
|
135
|
+
pub const MsgTag = struct {
|
|
136
|
+
name: []const u8,
|
|
137
|
+
payload: PayloadClass = .none,
|
|
138
|
+
payload_type: []const u8 = "",
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
pub const Contract = struct {
|
|
142
|
+
format: u32 = format_version,
|
|
143
|
+
/// Hash over the app's Zig sources at emit time (`hashSourceDir`); a
|
|
144
|
+
/// checker recomputes it and degrades to structural checking on any
|
|
145
|
+
/// mismatch. 0 for contracts derived in-process (tests).
|
|
146
|
+
source_hash: u64 = 0,
|
|
147
|
+
source_root: []const u8 = default_source_root,
|
|
148
|
+
model_type: []const u8 = "",
|
|
149
|
+
msg_type: []const u8 = "",
|
|
150
|
+
model: Group = .{},
|
|
151
|
+
iterables: []const Iterable = &.{},
|
|
152
|
+
msgs: []const MsgTag = &.{},
|
|
153
|
+
/// Names opted out of the dead-state lint via `pub const view_unbound`.
|
|
154
|
+
model_unbound: []const []const u8 = &.{},
|
|
155
|
+
msg_unbound: []const []const u8 = &.{},
|
|
156
|
+
/// The app's registered icon vocabulary (`pub const app_icons` on the
|
|
157
|
+
/// app root, the same table `canvas.icons.registerAppIcons` installs
|
|
158
|
+
/// at boot): what markup `app:<name>` references check against. The
|
|
159
|
+
/// engines cannot prove these at build time (registration is a
|
|
160
|
+
/// runtime act), so the contract is where `app:` names get their
|
|
161
|
+
/// typed check. Additive with a default: artifacts from before the
|
|
162
|
+
/// field parse as "no registered icons", never a false pass.
|
|
163
|
+
app_icons: []const []const u8 = &.{},
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// ------------------------------------------------------------ reflection
|
|
167
|
+
|
|
168
|
+
/// The payload types the engines special-case; passed in by the canvas
|
|
169
|
+
/// layer so this module stays std-only.
|
|
170
|
+
pub const Specials = struct {
|
|
171
|
+
TextInputEvent: type,
|
|
172
|
+
ScrollState: type,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/// Reflect a concrete Model/Msg pair into a contract, at comptime. The
|
|
176
|
+
/// walk mirrors the interpreter's binding resolution exactly: what this
|
|
177
|
+
/// includes is precisely what a view can bind, and nothing else.
|
|
178
|
+
pub fn describe(comptime Model: type, comptime Msg: type, comptime specials: Specials) Contract {
|
|
179
|
+
comptime {
|
|
180
|
+
@setEvalBranchQuota(4 * (reflect.typeScanQuota(Model) + reflect.typeScanQuota(Msg)));
|
|
181
|
+
return .{
|
|
182
|
+
.model_type = @typeName(Model),
|
|
183
|
+
.msg_type = @typeName(Msg),
|
|
184
|
+
.model = describeGroup(Model),
|
|
185
|
+
.iterables = describeIterables(Model),
|
|
186
|
+
.msgs = describeMsgs(Msg, specials),
|
|
187
|
+
.model_unbound = optOutNames(Model),
|
|
188
|
+
.msg_unbound = optOutNames(Msg),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
fn describeGroup(comptime T: type) Group {
|
|
194
|
+
comptime {
|
|
195
|
+
var scalars: []const Scalar = &.{};
|
|
196
|
+
var groups: []const NamedGroup = &.{};
|
|
197
|
+
for (@typeInfo(T).@"struct".fields) |field| {
|
|
198
|
+
if (reflect.supportedScalar(field.type)) {
|
|
199
|
+
scalars = scalars ++ &[_]Scalar{.{
|
|
200
|
+
.name = field.name,
|
|
201
|
+
.kind = reflect.scalarKindOf(field.type),
|
|
202
|
+
.type_name = @typeName(field.type),
|
|
203
|
+
}};
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (@typeInfo(field.type) == .@"struct") {
|
|
207
|
+
groups = groups ++ &[_]NamedGroup{.{
|
|
208
|
+
.name = field.name,
|
|
209
|
+
.type_name = @typeName(field.type),
|
|
210
|
+
.group = describeGroup(field.type),
|
|
211
|
+
}};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (@typeInfo(T).@"struct".decls) |decl| {
|
|
215
|
+
const DeclType = @TypeOf(@field(T, decl.name));
|
|
216
|
+
if (@typeInfo(DeclType) != .@"fn") continue;
|
|
217
|
+
const info = @typeInfo(DeclType).@"fn";
|
|
218
|
+
if (reflect.isZeroArgFn(T, DeclType) and reflect.supportedScalar(info.return_type.?)) {
|
|
219
|
+
scalars = scalars ++ &[_]Scalar{.{
|
|
220
|
+
.name = decl.name,
|
|
221
|
+
.kind = reflect.scalarKindOf(info.return_type.?),
|
|
222
|
+
.type_name = @typeName(info.return_type.?),
|
|
223
|
+
.fn_backed = true,
|
|
224
|
+
}};
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (reflect.isArenaScalarFn(T, DeclType) and reflect.supportedScalar(info.return_type.?)) {
|
|
228
|
+
scalars = scalars ++ &[_]Scalar{.{
|
|
229
|
+
.name = decl.name,
|
|
230
|
+
.kind = reflect.scalarKindOf(info.return_type.?),
|
|
231
|
+
.type_name = @typeName(info.return_type.?),
|
|
232
|
+
.arena = true,
|
|
233
|
+
.fn_backed = true,
|
|
234
|
+
}};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return .{ .scalars = scalars, .groups = groups };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
fn describeItem(comptime Item: type) Iterable {
|
|
242
|
+
comptime {
|
|
243
|
+
return .{
|
|
244
|
+
.name = "",
|
|
245
|
+
.item_type = @typeName(Item),
|
|
246
|
+
.item_kind = if (reflect.supportedScalar(Item)) reflect.scalarKindOf(Item) else null,
|
|
247
|
+
.item_scalar = reflect.supportedScalar(Item),
|
|
248
|
+
.item = if (@typeInfo(Item) == .@"struct") describeGroup(Item) else .{},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
fn describeIterables(comptime Model: type) []const Iterable {
|
|
254
|
+
comptime {
|
|
255
|
+
var iterables: []const Iterable = &.{};
|
|
256
|
+
for (@typeInfo(Model).@"struct".fields) |field| {
|
|
257
|
+
if (reflect.sliceElement(field.type)) |Item| {
|
|
258
|
+
var entry = describeItem(Item);
|
|
259
|
+
entry.name = field.name;
|
|
260
|
+
iterables = iterables ++ &[_]Iterable{entry};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
for (@typeInfo(Model).@"struct".decls) |decl| {
|
|
264
|
+
const DeclType = @TypeOf(@field(Model, decl.name));
|
|
265
|
+
if (reflect.sliceElement(DeclType)) |Item| {
|
|
266
|
+
var entry = describeItem(Item);
|
|
267
|
+
entry.name = decl.name;
|
|
268
|
+
iterables = iterables ++ &[_]Iterable{entry};
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (@typeInfo(DeclType) != .@"fn") continue;
|
|
272
|
+
const Return = @typeInfo(DeclType).@"fn".return_type orelse continue;
|
|
273
|
+
const Item = reflect.sliceElement(Return) orelse continue;
|
|
274
|
+
if (reflect.isItemFn(DeclType, Item, false) or reflect.isItemFn(DeclType, Item, true)) {
|
|
275
|
+
var entry = describeItem(Item);
|
|
276
|
+
entry.name = decl.name;
|
|
277
|
+
entry.fn_backed = true;
|
|
278
|
+
iterables = iterables ++ &[_]Iterable{entry};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return iterables;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
fn describeMsgs(comptime Msg: type, comptime specials: Specials) []const MsgTag {
|
|
286
|
+
comptime {
|
|
287
|
+
var tags: []const MsgTag = &.{};
|
|
288
|
+
for (@typeInfo(Msg).@"union".fields) |field| {
|
|
289
|
+
tags = tags ++ &[_]MsgTag{.{
|
|
290
|
+
.name = field.name,
|
|
291
|
+
.payload = payloadClassOf(field.type, specials),
|
|
292
|
+
.payload_type = if (field.type == void) "" else @typeName(field.type),
|
|
293
|
+
}};
|
|
294
|
+
}
|
|
295
|
+
return tags;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fn payloadClassOf(comptime T: type, comptime specials: Specials) PayloadClass {
|
|
300
|
+
if (T == void) return .none;
|
|
301
|
+
if (T == specials.TextInputEvent) return .text_input;
|
|
302
|
+
if (T == specials.ScrollState) return .scroll_state;
|
|
303
|
+
return switch (@typeInfo(T)) {
|
|
304
|
+
.int => .integer,
|
|
305
|
+
.float => .float,
|
|
306
|
+
.bool => .boolean,
|
|
307
|
+
.@"enum" => .enum_tag,
|
|
308
|
+
.pointer => |info| if (info.size == .slice and info.child == u8) .string else .unsupported,
|
|
309
|
+
else => .unsupported,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
fn optOutNames(comptime T: type) []const []const u8 {
|
|
314
|
+
comptime {
|
|
315
|
+
if (!@hasDecl(T, opt_out_decl)) return &.{};
|
|
316
|
+
const value = @field(T, opt_out_decl);
|
|
317
|
+
const V = @TypeOf(value);
|
|
318
|
+
const teaching = "pub const " ++ opt_out_decl ++ " lists names as string literals: pub const " ++ opt_out_decl ++ " = .{ \"field_name\" };";
|
|
319
|
+
var names: []const []const u8 = &.{};
|
|
320
|
+
switch (@typeInfo(V)) {
|
|
321
|
+
.@"struct" => |info| {
|
|
322
|
+
if (!info.is_tuple) @compileError(teaching);
|
|
323
|
+
for (info.fields) |field| {
|
|
324
|
+
const name: []const u8 = @field(value, field.name);
|
|
325
|
+
names = names ++ &[_][]const u8{name};
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
.array, .pointer => {
|
|
329
|
+
for (value) |name| {
|
|
330
|
+
const slice: []const u8 = name;
|
|
331
|
+
names = names ++ &[_][]const u8{slice};
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
else => @compileError(teaching),
|
|
335
|
+
}
|
|
336
|
+
return names;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// -------------------------------------------------------------- messages
|
|
341
|
+
//
|
|
342
|
+
// The view→model failure vocabulary is the ENGINES' vocabulary: the
|
|
343
|
+
// conformance suite (ui_markup_contract_tests.zig) drives the interpreter
|
|
344
|
+
// over the same fixtures and asserts these strings match its diagnostics,
|
|
345
|
+
// so the two checkers cannot drift apart silently.
|
|
346
|
+
|
|
347
|
+
pub const binding_model_message = "binding does not name a model field";
|
|
348
|
+
pub const binding_item_message = "binding does not name a field on the loop item";
|
|
349
|
+
pub const value_arg_fields_message = "template arg values have no fields";
|
|
350
|
+
pub const slice_arg_value_message = "slice-valued template args are only usable with for each";
|
|
351
|
+
pub const item_value_message = "loop items of this type cannot be used as values";
|
|
352
|
+
pub const each_message = "each does not name an iterable (a model slice, array, or fn - or a slice-valued template arg)";
|
|
353
|
+
pub const key_field_message = "key does not name a field on the item";
|
|
354
|
+
pub const key_kind_message = "key fields must be integers or strings";
|
|
355
|
+
pub const attr_key_kind_message = "keys must be integers or strings";
|
|
356
|
+
pub const unknown_tag_message = "unknown message tag";
|
|
357
|
+
pub const no_payload_message = "message does not take a payload";
|
|
358
|
+
pub const payload_required_message = "message requires a payload";
|
|
359
|
+
pub const payload_type_message = "payload type does not match the message";
|
|
360
|
+
pub const on_input_payload_message = "on-input tag must carry a TextInputEvent payload";
|
|
361
|
+
pub const number_attr_message = "expected a number";
|
|
362
|
+
pub const whole_attr_message = "expected a whole number";
|
|
363
|
+
pub const text_attr_message = "expected text";
|
|
364
|
+
pub const option_attr_message = "expected an option name";
|
|
365
|
+
pub const role_attr_message = "role expects a role name";
|
|
366
|
+
pub const label_attr_message = "label expects text";
|
|
367
|
+
|
|
368
|
+
// Contract-only findings (no engine counterpart, like the dead-state
|
|
369
|
+
// lint): `app:` icon names are runtime registrations the engines cannot
|
|
370
|
+
// prove, so this pass is where they get their typed check.
|
|
371
|
+
pub const unknown_app_icon_message = "app: does not name a registered app icon (the contract's app_icons list)";
|
|
372
|
+
pub const no_app_icons_message = "app: references a registered app icon, but this app registers none - declare pub const app_icons on the app root, pass it to canvas.icons.registerAppIcons at boot, and refresh the contract";
|
|
373
|
+
pub const icon_binding_kind_message = "icon bindings must produce a string naming an icon (a built-in name or app:<name>)";
|
|
374
|
+
|
|
375
|
+
// ------------------------------------------------- attribute kind classes
|
|
376
|
+
|
|
377
|
+
/// What kind of value each generic option attribute consumes, mirroring
|
|
378
|
+
/// how both engines apply `Ui.ElementOptions` fields (float fields take
|
|
379
|
+
/// numbers, enum fields take option-name strings, bool fields take any
|
|
380
|
+
/// truthy value, ...). Derived from the registry's value classes; a
|
|
381
|
+
/// conformance test in ui_markup_view_tests.zig derives the same classes
|
|
382
|
+
/// from the real field types, so registry, check-time pass, and
|
|
383
|
+
/// build-time pass cannot drift.
|
|
384
|
+
pub const AttrClass = enum { number, whole, truthy, text, option };
|
|
385
|
+
|
|
386
|
+
pub const AttrKindRule = struct { name: []const u8, class: AttrClass };
|
|
387
|
+
|
|
388
|
+
pub const attr_kind_rules = blk: {
|
|
389
|
+
@setEvalBranchQuota(10_000);
|
|
390
|
+
var rules: [schema.option_field_pairs.len]AttrKindRule = undefined;
|
|
391
|
+
var index: usize = 0;
|
|
392
|
+
for (schema.attrs) |entry| {
|
|
393
|
+
if (entry.group != .option or entry.field.len == 0) continue;
|
|
394
|
+
rules[index] = .{ .name = entry.name, .class = switch (entry.class) {
|
|
395
|
+
.text => .text,
|
|
396
|
+
.number => .number,
|
|
397
|
+
.whole => .whole,
|
|
398
|
+
.flag => .truthy,
|
|
399
|
+
.option => .option,
|
|
400
|
+
else => @compileError("field-backed option attr with a non-generic value class: " ++ entry.name),
|
|
401
|
+
} };
|
|
402
|
+
index += 1;
|
|
403
|
+
}
|
|
404
|
+
break :blk rules;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
fn attrClass(name: []const u8) ?AttrClass {
|
|
408
|
+
for (attr_kind_rules) |rule| {
|
|
409
|
+
if (std.mem.eql(u8, rule.name, name)) return rule.class;
|
|
410
|
+
}
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ------------------------------------------------------------ dead state
|
|
415
|
+
|
|
416
|
+
/// Which contract entries the checked views actually bound: fed by
|
|
417
|
+
/// `checkDocument` across every view in an app, read once by `deadState`.
|
|
418
|
+
/// Name-based on purpose — a []const u8 field appears both as a scalar
|
|
419
|
+
/// and (as a byte iterable) in the iterables list, and binding it either
|
|
420
|
+
/// way counts.
|
|
421
|
+
pub const Usage = struct {
|
|
422
|
+
scalar_used: []bool,
|
|
423
|
+
group_used: []bool,
|
|
424
|
+
iterable_used: []bool,
|
|
425
|
+
msg_used: []bool,
|
|
426
|
+
|
|
427
|
+
pub fn init(arena: std.mem.Allocator, contract: *const Contract) error{OutOfMemory}!Usage {
|
|
428
|
+
const usage = Usage{
|
|
429
|
+
.scalar_used = try arena.alloc(bool, contract.model.scalars.len),
|
|
430
|
+
.group_used = try arena.alloc(bool, contract.model.groups.len),
|
|
431
|
+
.iterable_used = try arena.alloc(bool, contract.iterables.len),
|
|
432
|
+
.msg_used = try arena.alloc(bool, contract.msgs.len),
|
|
433
|
+
};
|
|
434
|
+
@memset(usage.scalar_used, false);
|
|
435
|
+
@memset(usage.group_used, false);
|
|
436
|
+
@memset(usage.iterable_used, false);
|
|
437
|
+
@memset(usage.msg_used, false);
|
|
438
|
+
return usage;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
fn markModel(self: *Usage, contract: *const Contract, name: []const u8) void {
|
|
442
|
+
for (contract.model.scalars, 0..) |scalar, index| {
|
|
443
|
+
if (std.mem.eql(u8, scalar.name, name)) self.scalar_used[index] = true;
|
|
444
|
+
}
|
|
445
|
+
for (contract.model.groups, 0..) |group, index| {
|
|
446
|
+
if (std.mem.eql(u8, group.name, name)) self.group_used[index] = true;
|
|
447
|
+
}
|
|
448
|
+
for (contract.iterables, 0..) |iterable, index| {
|
|
449
|
+
if (std.mem.eql(u8, iterable.name, name)) self.iterable_used[index] = true;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn markMsg(self: *Usage, contract: *const Contract, name: []const u8) void {
|
|
454
|
+
for (contract.msgs, 0..) |tag, index| {
|
|
455
|
+
if (std.mem.eql(u8, tag.name, name)) self.msg_used[index] = true;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
fn usedName(self: *const Usage, contract: *const Contract, name: []const u8) bool {
|
|
460
|
+
for (contract.model.scalars, 0..) |scalar, index| {
|
|
461
|
+
if (self.scalar_used[index] and std.mem.eql(u8, scalar.name, name)) return true;
|
|
462
|
+
}
|
|
463
|
+
for (contract.model.groups, 0..) |group, index| {
|
|
464
|
+
if (self.group_used[index] and std.mem.eql(u8, group.name, name)) return true;
|
|
465
|
+
}
|
|
466
|
+
for (contract.iterables, 0..) |iterable, index| {
|
|
467
|
+
if (self.iterable_used[index] and std.mem.eql(u8, iterable.name, name)) return true;
|
|
468
|
+
}
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/// Model→view lint: model state and Msg tags no checked view binds or
|
|
474
|
+
/// dispatches. WARNING class — model-only state is legitimate; the
|
|
475
|
+
/// opt-out is `pub const view_unbound = .{ "name" };` on Model/Msg, and
|
|
476
|
+
/// promotion to a failure is the caller's call (the CLI's --strict).
|
|
477
|
+
/// Positions are zeroed: these findings live on the Zig side, which the
|
|
478
|
+
/// contract does not carry source spans for.
|
|
479
|
+
pub fn deadState(arena: std.mem.Allocator, contract: *const Contract, usage: *const Usage) error{OutOfMemory}![]const markup.MarkupErrorInfo {
|
|
480
|
+
var out: std.ArrayListUnmanaged(markup.MarkupErrorInfo) = .empty;
|
|
481
|
+
// One finding per top-level name: a []const u8 fn is both a string
|
|
482
|
+
// scalar and (as bytes) an iterable, and one warning is the truth.
|
|
483
|
+
var reported: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
484
|
+
for (contract.model.scalars, 0..) |scalar, index| {
|
|
485
|
+
if (usage.scalar_used[index]) continue;
|
|
486
|
+
if (usage.usedName(contract, scalar.name)) continue;
|
|
487
|
+
if (nameListed(contract.model_unbound, scalar.name)) continue;
|
|
488
|
+
if (nameListed(reported.items, scalar.name)) continue;
|
|
489
|
+
try reported.append(arena, scalar.name);
|
|
490
|
+
const message = if (scalar.fn_backed)
|
|
491
|
+
try std.fmt.allocPrint(arena, "model fn \"{s}\" ({s}) is never bound in markup - bind it, remove it, or add it to pub const view_unbound if only update/fx logic or a Zig-built view calls it", .{ scalar.name, scalar.type_name })
|
|
492
|
+
else
|
|
493
|
+
try std.fmt.allocPrint(arena, "model field \"{s}\" ({s}) is never bound in markup - bind it, or add it to pub const view_unbound if only update/fx logic or a Zig-built view reads it", .{ scalar.name, scalar.type_name });
|
|
494
|
+
try out.append(arena, .{ .message = message });
|
|
495
|
+
}
|
|
496
|
+
for (contract.model.groups, 0..) |group, index| {
|
|
497
|
+
if (usage.group_used[index]) continue;
|
|
498
|
+
if (usage.usedName(contract, group.name)) continue;
|
|
499
|
+
if (nameListed(contract.model_unbound, group.name)) continue;
|
|
500
|
+
if (nameListed(reported.items, group.name)) continue;
|
|
501
|
+
try reported.append(arena, group.name);
|
|
502
|
+
try out.append(arena, .{ .message = try std.fmt.allocPrint(arena, "model field \"{s}\" ({s}) has no markup binding into any of its fields - bind one, or add it to pub const view_unbound if only update/fx logic or a Zig-built view reads it", .{ group.name, group.type_name }) });
|
|
503
|
+
}
|
|
504
|
+
for (contract.iterables, 0..) |iterable, index| {
|
|
505
|
+
if (usage.iterable_used[index]) continue;
|
|
506
|
+
if (usage.usedName(contract, iterable.name)) continue;
|
|
507
|
+
if (nameListed(contract.model_unbound, iterable.name)) continue;
|
|
508
|
+
if (std.mem.eql(u8, iterable.name, opt_out_decl)) continue;
|
|
509
|
+
if (nameListed(reported.items, iterable.name)) continue;
|
|
510
|
+
try reported.append(arena, iterable.name);
|
|
511
|
+
try out.append(arena, .{ .message = try std.fmt.allocPrint(arena, "model iterable \"{s}\" (items: {s}) is never iterated in markup - use it with for each, or add it to pub const view_unbound if only update/fx logic or a Zig-built view reads it", .{ iterable.name, iterable.item_type }) });
|
|
512
|
+
}
|
|
513
|
+
for (contract.msgs, 0..) |tag, index| {
|
|
514
|
+
if (usage.msg_used[index]) continue;
|
|
515
|
+
if (nameListed(contract.msg_unbound, tag.name)) continue;
|
|
516
|
+
try out.append(arena, .{ .message = try std.fmt.allocPrint(arena, "Msg tag \"{s}\" is never dispatched from markup - wire it to an on-* event, or add it to pub const view_unbound if only Zig code sends it", .{tag.name}) });
|
|
517
|
+
}
|
|
518
|
+
return out.items;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
fn nameListed(names: []const []const u8, name: []const u8) bool {
|
|
522
|
+
for (names) |candidate| {
|
|
523
|
+
if (std.mem.eql(u8, candidate, name)) return true;
|
|
524
|
+
}
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ------------------------------------------------------------- the check
|
|
529
|
+
|
|
530
|
+
/// Check one RESOLVED document (imports merged, `markup.validate` green)
|
|
531
|
+
/// against a contract. Returns the first view→model failure, or null.
|
|
532
|
+
/// A document without a view root is a component file: its templates are
|
|
533
|
+
/// checked through every view that imports them, where use-site argument
|
|
534
|
+
/// kinds exist. `usage` (optional) accumulates dead-state facts across an
|
|
535
|
+
/// app's documents.
|
|
536
|
+
pub fn checkDocument(
|
|
537
|
+
arena: std.mem.Allocator,
|
|
538
|
+
document: markup.MarkupDocument,
|
|
539
|
+
contract: *const Contract,
|
|
540
|
+
usage: ?*Usage,
|
|
541
|
+
) error{OutOfMemory}!?markup.MarkupErrorInfo {
|
|
542
|
+
const root = document.root orelse return null;
|
|
543
|
+
if (document.imports.len > 0) {
|
|
544
|
+
return .{
|
|
545
|
+
.line = document.imports[0].line,
|
|
546
|
+
.column = document.imports[0].column,
|
|
547
|
+
.message = markup.import_unresolved_message,
|
|
548
|
+
.path = document.imports[0].src_path,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
var checker = Checker{
|
|
552
|
+
.arena = arena,
|
|
553
|
+
.document = document,
|
|
554
|
+
.contract = contract,
|
|
555
|
+
.usage = usage,
|
|
556
|
+
};
|
|
557
|
+
checker.checkNode(root) catch |err| switch (err) {
|
|
558
|
+
error.OutOfMemory => return error.OutOfMemory,
|
|
559
|
+
error.ContractCheck => return checker.failure,
|
|
560
|
+
};
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const CheckErr = error{ OutOfMemory, ContractCheck };
|
|
565
|
+
|
|
566
|
+
const max_scope_depth = 16;
|
|
567
|
+
const max_use_depth = 128;
|
|
568
|
+
|
|
569
|
+
/// A resolved binding: its value kind when statically known, and the Zig
|
|
570
|
+
/// type name for teaching messages.
|
|
571
|
+
const Resolved = struct {
|
|
572
|
+
kind: ?ValueKind = null,
|
|
573
|
+
type_name: []const u8 = "",
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
const ItemRef = struct {
|
|
577
|
+
type_name: []const u8 = "",
|
|
578
|
+
kind: ?ValueKind = null,
|
|
579
|
+
scalar: bool = false,
|
|
580
|
+
group: *const Group,
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const SlotCapture = struct {
|
|
584
|
+
nodes: []const markup.MarkupNode,
|
|
585
|
+
len: usize,
|
|
586
|
+
floor: usize,
|
|
587
|
+
template_ctx: ?usize,
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const Binder = union(enum) {
|
|
591
|
+
item: ItemRef,
|
|
592
|
+
slice: ItemRef,
|
|
593
|
+
value: ?ValueKind,
|
|
594
|
+
slot: SlotCapture,
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
const ScopeEntry = struct {
|
|
598
|
+
name: []const u8,
|
|
599
|
+
binder: Binder,
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
const Checker = struct {
|
|
603
|
+
arena: std.mem.Allocator,
|
|
604
|
+
document: markup.MarkupDocument,
|
|
605
|
+
contract: *const Contract,
|
|
606
|
+
usage: ?*Usage,
|
|
607
|
+
failure: markup.MarkupErrorInfo = .{},
|
|
608
|
+
entries: [max_scope_depth]ScopeEntry = undefined,
|
|
609
|
+
len: usize = 0,
|
|
610
|
+
/// Bindings resolve entries[floor..len] then the model: a template
|
|
611
|
+
/// body sees its args and its own loop variables, never the loop
|
|
612
|
+
/// variables at the expansion site (interpreter parity).
|
|
613
|
+
floor: usize = 0,
|
|
614
|
+
use_depth: usize = 0,
|
|
615
|
+
template_ctx: ?usize = null,
|
|
616
|
+
|
|
617
|
+
// ------------------------------------------------------ diagnostics
|
|
618
|
+
|
|
619
|
+
fn fail(self: *Checker, node: markup.MarkupNode, message: []const u8) CheckErr {
|
|
620
|
+
self.failure = .{ .line = node.line, .column = node.column, .message = message, .path = node.src_path };
|
|
621
|
+
return error.ContractCheck;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
fn failAttr(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, message: []const u8) CheckErr {
|
|
625
|
+
self.failure = .{ .line = attribute.line, .column = attribute.column, .message = message, .path = node.src_path };
|
|
626
|
+
return error.ContractCheck;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/// The engines' message plus a token/did-you-mean suffix — the base
|
|
630
|
+
/// stays a prefix so the conformance suite can hold the vocabularies
|
|
631
|
+
/// equal.
|
|
632
|
+
fn failNamed(self: *Checker, node: markup.MarkupNode, base: []const u8, token: []const u8, candidates: NameSource) CheckErr {
|
|
633
|
+
const message = self.namedMessage(base, token, candidates) catch return error.OutOfMemory;
|
|
634
|
+
return self.fail(node, message);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
fn namedMessage(self: *Checker, base: []const u8, token: []const u8, candidates: NameSource) error{OutOfMemory}![]const u8 {
|
|
638
|
+
if (nearestCandidate(token, candidates)) |suggestion| {
|
|
639
|
+
return std.fmt.allocPrint(self.arena, "{s} (\"{s}\" - did you mean \"{s}\"?)", .{ base, token, suggestion });
|
|
640
|
+
}
|
|
641
|
+
return std.fmt.allocPrint(self.arena, "{s} (\"{s}\")", .{ base, token });
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// ------------------------------------------------------------ scope
|
|
645
|
+
|
|
646
|
+
fn lookup(self: *const Checker, head: []const u8) ?*const ScopeEntry {
|
|
647
|
+
var index = self.len;
|
|
648
|
+
while (index > self.floor) {
|
|
649
|
+
index -= 1;
|
|
650
|
+
if (std.mem.eql(u8, self.entries[index].name, head)) return &self.entries[index];
|
|
651
|
+
}
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
fn slotCapture(self: *const Checker) ?SlotCapture {
|
|
656
|
+
var index = self.len;
|
|
657
|
+
while (index > self.floor) {
|
|
658
|
+
index -= 1;
|
|
659
|
+
if (self.entries[index].binder == .slot) return self.entries[index].binder.slot;
|
|
660
|
+
}
|
|
661
|
+
return null;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// --------------------------------------------------------- bindings
|
|
665
|
+
|
|
666
|
+
fn markModel(self: *Checker, name: []const u8) void {
|
|
667
|
+
if (self.usage) |usage| usage.markModel(self.contract, name);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
fn resolveBinding(self: *Checker, node: markup.MarkupNode, path: []const u8, allow_arena: bool) CheckErr!Resolved {
|
|
671
|
+
const head = pathHead(path);
|
|
672
|
+
if (self.lookup(head)) |entry| {
|
|
673
|
+
switch (entry.binder) {
|
|
674
|
+
.item => |item| {
|
|
675
|
+
if (pathTail(path)) |tail| {
|
|
676
|
+
switch (resolveOnGroup(item.group, tail, allow_arena)) {
|
|
677
|
+
.ok => |resolved| return resolved,
|
|
678
|
+
.arena_blocked => return self.fail(node, markup.arena_scalar_equality_message),
|
|
679
|
+
.missing => return self.failNamed(node, binding_item_message, path, .{ .group = item.group }),
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (!item.scalar) return self.fail(node, item_value_message);
|
|
683
|
+
return .{ .kind = item.kind, .type_name = item.type_name };
|
|
684
|
+
},
|
|
685
|
+
.value => |kind| {
|
|
686
|
+
if (pathTail(path) != null) return self.fail(node, value_arg_fields_message);
|
|
687
|
+
return .{ .kind = kind };
|
|
688
|
+
},
|
|
689
|
+
.slice => return self.fail(node, slice_arg_value_message),
|
|
690
|
+
// Slot captures carry an empty name, which no binding
|
|
691
|
+
// head can equal.
|
|
692
|
+
.slot => unreachable,
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
switch (resolveOnGroup(&self.contract.model, path, allow_arena)) {
|
|
696
|
+
.ok => |resolved| {
|
|
697
|
+
self.markModel(head);
|
|
698
|
+
return resolved;
|
|
699
|
+
},
|
|
700
|
+
.arena_blocked => return self.fail(node, markup.arena_scalar_equality_message),
|
|
701
|
+
.missing => {
|
|
702
|
+
// A TextBuffer field is the edit model, not bindable
|
|
703
|
+
// text: the reflected contract carries it as a group
|
|
704
|
+
// whose type name spells the buffer out (engine parity).
|
|
705
|
+
for (self.contract.model.groups) |group| {
|
|
706
|
+
if (std.mem.eql(u8, group.name, head) and std.mem.indexOf(u8, group.type_name, "TextBuffer(") != null) {
|
|
707
|
+
return self.fail(node, markup.binding_text_buffer_message);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return self.failNamed(node, binding_model_message, path, .{ .model = self.contract });
|
|
711
|
+
},
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// ------------------------------------------------------ expressions
|
|
716
|
+
|
|
717
|
+
/// Static kind of an attribute value, with every binding resolved
|
|
718
|
+
/// against the contract and the whole expression run through the
|
|
719
|
+
/// shared type checker with those kinds — `{count > 'a'}` fails here
|
|
720
|
+
/// with the evaluator's teaching message and the model field's type.
|
|
721
|
+
fn attrKind(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, raw: []const u8) CheckErr!?ValueKind {
|
|
722
|
+
const expression = markup.parseAttrExpression(raw) orelse {
|
|
723
|
+
return self.failAttr(node, attribute, markup.invalid_expression_message);
|
|
724
|
+
};
|
|
725
|
+
return switch (expression) {
|
|
726
|
+
.literal => |text| expr.kindOf(reflect.literalValue(text)),
|
|
727
|
+
.binding => |path| (try self.resolveBinding(node, path, true)).kind,
|
|
728
|
+
.equals => |sides| blk: {
|
|
729
|
+
// Arena-computed bindings are excluded from equality on
|
|
730
|
+
// purpose (engine parity): compare source fields, or bind
|
|
731
|
+
// a bool-returning fn.
|
|
732
|
+
_ = try self.resolveBinding(node, sides.left, false);
|
|
733
|
+
_ = try self.resolveBinding(node, sides.right, false);
|
|
734
|
+
break :blk .boolean;
|
|
735
|
+
},
|
|
736
|
+
.expression => |inner| try self.exprTreeKind(node, inner),
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
fn exprTreeKind(self: *Checker, node: markup.MarkupNode, inner: []const u8) CheckErr!?ValueKind {
|
|
741
|
+
var tree: expr.ExprTree = .{};
|
|
742
|
+
var diagnostic: expr.Diagnostic = .{};
|
|
743
|
+
if (!expr.parse(inner, &tree, &diagnostic)) {
|
|
744
|
+
return self.fail(node, diagnostic.message);
|
|
745
|
+
}
|
|
746
|
+
var kinds: [expr.max_expression_nodes]?ValueKind = @splat(null);
|
|
747
|
+
var names: [expr.max_expression_nodes][]const u8 = undefined;
|
|
748
|
+
var types: [expr.max_expression_nodes][]const u8 = undefined;
|
|
749
|
+
var binding_count: usize = 0;
|
|
750
|
+
for (tree.nodes[0..tree.len], 0..) |expr_node, index| {
|
|
751
|
+
if (expr_node.kind != .binding) continue;
|
|
752
|
+
// Comparison operands reject arena-computed scalars, the same
|
|
753
|
+
// teaching rule as `{a == b}`.
|
|
754
|
+
const resolved = try self.resolveBinding(node, expr_node.text, !expr_node.comparison_operand);
|
|
755
|
+
kinds[index] = resolved.kind;
|
|
756
|
+
if (resolved.type_name.len > 0 and binding_count < names.len) {
|
|
757
|
+
names[binding_count] = expr_node.text;
|
|
758
|
+
types[binding_count] = resolved.type_name;
|
|
759
|
+
binding_count += 1;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
const result = expr.checkTypes(&tree, &kinds, &diagnostic) catch {
|
|
763
|
+
return self.failExprType(node, diagnostic.message, names[0..binding_count], types[0..binding_count]);
|
|
764
|
+
};
|
|
765
|
+
return result;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/// A type-discipline failure with the model bindings named: the
|
|
769
|
+
/// evaluator's teaching message, then which binding is which Zig type.
|
|
770
|
+
fn failExprType(self: *Checker, node: markup.MarkupNode, base: []const u8, names: []const []const u8, types: []const []const u8) CheckErr {
|
|
771
|
+
if (names.len == 0) return self.fail(node, base);
|
|
772
|
+
var out: std.ArrayListUnmanaged(u8) = .empty;
|
|
773
|
+
out.appendSlice(self.arena, base) catch return error.OutOfMemory;
|
|
774
|
+
out.appendSlice(self.arena, " (where ") catch return error.OutOfMemory;
|
|
775
|
+
const shown = @min(names.len, 4);
|
|
776
|
+
for (names[0..shown], types[0..shown], 0..) |name, type_name, index| {
|
|
777
|
+
if (index > 0) out.appendSlice(self.arena, ", ") catch return error.OutOfMemory;
|
|
778
|
+
out.appendSlice(self.arena, name) catch return error.OutOfMemory;
|
|
779
|
+
out.appendSlice(self.arena, ": ") catch return error.OutOfMemory;
|
|
780
|
+
out.appendSlice(self.arena, type_name) catch return error.OutOfMemory;
|
|
781
|
+
}
|
|
782
|
+
out.appendSlice(self.arena, ")") catch return error.OutOfMemory;
|
|
783
|
+
return self.fail(node, out.items);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
fn requireAttrKind(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, kind: ?ValueKind, allowed: []const ValueKind, message: []const u8) CheckErr!void {
|
|
787
|
+
const known = kind orelse return;
|
|
788
|
+
for (allowed) |candidate| {
|
|
789
|
+
if (known == candidate) return;
|
|
790
|
+
}
|
|
791
|
+
return self.failAttr(node, attribute, message);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
fn checkClassAttr(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, class: AttrClass) CheckErr!void {
|
|
795
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
796
|
+
switch (class) {
|
|
797
|
+
.number => try self.requireAttrKind(node, attribute, kind, &.{ .integer, .float }, number_attr_message),
|
|
798
|
+
.whole => try self.requireAttrKind(node, attribute, kind, &.{.integer}, whole_attr_message),
|
|
799
|
+
.text => try self.requireAttrKind(node, attribute, kind, &.{.string}, text_attr_message),
|
|
800
|
+
.option => try self.requireAttrKind(node, attribute, kind, &.{.string}, option_attr_message),
|
|
801
|
+
// Bool fields accept any value through truthiness (engine
|
|
802
|
+
// parity) — resolving the bindings was the whole check.
|
|
803
|
+
.truthy => {},
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// --------------------------------------------------------- messages
|
|
808
|
+
|
|
809
|
+
fn findMsg(self: *Checker, tag: []const u8) ?MsgTag {
|
|
810
|
+
for (self.contract.msgs) |candidate| {
|
|
811
|
+
if (std.mem.eql(u8, candidate.name, tag)) {
|
|
812
|
+
if (self.usage) |usage| usage.markMsg(self.contract, tag);
|
|
813
|
+
return candidate;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
fn checkMessageAttr(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr) CheckErr!void {
|
|
820
|
+
const expression = markup.parseMessageExpression(attribute.value) orelse return;
|
|
821
|
+
const event = attribute.name[3..];
|
|
822
|
+
const tag = self.findMsg(expression.tag);
|
|
823
|
+
if (std.mem.eql(u8, event, "input")) {
|
|
824
|
+
const found = tag orelse return self.failAttr(node, attribute, on_input_payload_message);
|
|
825
|
+
if (found.payload != .text_input) return self.failAttr(node, attribute, on_input_payload_message);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
if (std.mem.eql(u8, event, "scroll")) {
|
|
829
|
+
const found = tag orelse return self.failAttr(node, attribute, markup.on_scroll_payload_message);
|
|
830
|
+
if (found.payload != .scroll_state) return self.failAttr(node, attribute, markup.on_scroll_payload_message);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
if (std.mem.eql(u8, event, "resize")) {
|
|
834
|
+
const found = tag orelse return self.failAttr(node, attribute, markup.on_resize_payload_message);
|
|
835
|
+
if (found.payload != .float or !std.mem.eql(u8, found.payload_type, "f32")) {
|
|
836
|
+
return self.failAttr(node, attribute, markup.on_resize_payload_message);
|
|
837
|
+
}
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
const found = tag orelse return self.failNamed(node, unknown_tag_message, expression.tag, .{ .msgs = self.contract });
|
|
841
|
+
if (found.payload == .none) {
|
|
842
|
+
if (expression.payload.len > 0) return self.failAttr(node, attribute, no_payload_message);
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
if (expression.payload.len == 0) return self.failAttr(node, attribute, payload_required_message);
|
|
846
|
+
const resolved = try self.resolveBinding(node, expression.payload, true);
|
|
847
|
+
switch (found.payload) {
|
|
848
|
+
.integer => try self.requirePayloadKind(node, attribute, resolved, &.{.integer}, found),
|
|
849
|
+
.float => try self.requirePayloadKind(node, attribute, resolved, &.{ .float, .integer }, found),
|
|
850
|
+
.string, .enum_tag => try self.requirePayloadKind(node, attribute, resolved, &.{.string}, found),
|
|
851
|
+
// Bool payloads accept any value through truthiness.
|
|
852
|
+
.boolean => {},
|
|
853
|
+
// These payloads cannot be constructed from a markup binding
|
|
854
|
+
// (input/scroll payloads bind through their own events).
|
|
855
|
+
.text_input, .scroll_state, .unsupported => return self.failPayloadType(node, attribute, resolved, found),
|
|
856
|
+
.none => unreachable,
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
fn requirePayloadKind(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, resolved: Resolved, allowed: []const ValueKind, tag: MsgTag) CheckErr!void {
|
|
861
|
+
const known = resolved.kind orelse return;
|
|
862
|
+
for (allowed) |candidate| {
|
|
863
|
+
if (known == candidate) return;
|
|
864
|
+
}
|
|
865
|
+
return self.failPayloadType(node, attribute, resolved, tag);
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
fn failPayloadType(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr, resolved: Resolved, tag: MsgTag) CheckErr {
|
|
869
|
+
const message = if (resolved.type_name.len > 0)
|
|
870
|
+
std.fmt.allocPrint(self.arena, "{s} (\"{s}\" carries {s}; the binding is {s})", .{ payload_type_message, tag.name, tag.payload_type, resolved.type_name }) catch return error.OutOfMemory
|
|
871
|
+
else
|
|
872
|
+
std.fmt.allocPrint(self.arena, "{s} (\"{s}\" carries {s})", .{ payload_type_message, tag.name, tag.payload_type }) catch return error.OutOfMemory;
|
|
873
|
+
return self.failAttr(node, attribute, message);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// -------------------------------------------------------- iterables
|
|
877
|
+
|
|
878
|
+
fn resolveIterable(self: *Checker, node: markup.MarkupNode, name: []const u8, message: []const u8) CheckErr!ItemRef {
|
|
879
|
+
if (self.lookup(name)) |entry| {
|
|
880
|
+
if (entry.binder == .slice) return entry.binder.slice;
|
|
881
|
+
return self.fail(node, message);
|
|
882
|
+
}
|
|
883
|
+
for (self.contract.iterables) |*iterable| {
|
|
884
|
+
if (std.mem.eql(u8, iterable.name, name)) {
|
|
885
|
+
self.markModel(name);
|
|
886
|
+
return itemRefOf(iterable);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return self.failNamed(node, message, name, .{ .iterables = self.contract });
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
fn checkFor(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
893
|
+
const each = node.attr("each") orelse return;
|
|
894
|
+
const as_name = node.attr("as") orelse return;
|
|
895
|
+
const item = try self.resolveIterable(node, each, each_message);
|
|
896
|
+
if (node.attr("key")) |field| {
|
|
897
|
+
// Keys stay identity-stable data: fields and zero-arg methods
|
|
898
|
+
// only, never arena-computed values (engine parity).
|
|
899
|
+
switch (resolveOnGroup(item.group, field, false)) {
|
|
900
|
+
.ok => |resolved| {
|
|
901
|
+
if (resolved.kind) |known| {
|
|
902
|
+
if (known != .integer and known != .string) {
|
|
903
|
+
return self.fail(node, key_kind_message);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
},
|
|
907
|
+
.arena_blocked, .missing => {
|
|
908
|
+
return self.failNamed(node, key_field_message, field, .{ .group = item.group });
|
|
909
|
+
},
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (self.len >= max_scope_depth) return self.fail(node, "for nesting is too deep");
|
|
913
|
+
self.entries[self.len] = .{ .name = as_name, .binder = .{ .item = item } };
|
|
914
|
+
self.len += 1;
|
|
915
|
+
defer self.len -= 1;
|
|
916
|
+
try self.checkChildList(node.children);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// -------------------------------------------------------- templates
|
|
920
|
+
|
|
921
|
+
fn checkUse(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
922
|
+
const template_name = node.attr("template") orelse return;
|
|
923
|
+
const template_index = self.document.templateIndex(template_name) orelse {
|
|
924
|
+
return self.fail(node, markup.use_undefined_template_message);
|
|
925
|
+
};
|
|
926
|
+
// The define-before-use rule again at check time: it is what
|
|
927
|
+
// bounds expansion, so an unvalidated document cannot recurse.
|
|
928
|
+
if (self.template_ctx) |ctx_index| {
|
|
929
|
+
if (template_index >= ctx_index) {
|
|
930
|
+
return self.fail(node, markup.use_earlier_template_message);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (self.use_depth >= max_use_depth) {
|
|
934
|
+
return self.fail(node, "template expansion nests too deeply");
|
|
935
|
+
}
|
|
936
|
+
const template_node = self.document.templates[template_index];
|
|
937
|
+
if (template_node.children.len != 1 or template_node.children[0].kind != .element) return;
|
|
938
|
+
|
|
939
|
+
// Evaluate every arg's kind against the pristine use-site scope
|
|
940
|
+
// before any entry is pushed, so args cannot see each other.
|
|
941
|
+
const saved_len = self.len;
|
|
942
|
+
const saved_floor = self.floor;
|
|
943
|
+
const saved_ctx = self.template_ctx;
|
|
944
|
+
var arg_count: usize = 0;
|
|
945
|
+
var args = markup.templateArgs(template_node);
|
|
946
|
+
while (args.next()) |token| {
|
|
947
|
+
const arg = markup.parseTemplateArg(token);
|
|
948
|
+
if (saved_len + arg_count >= max_scope_depth) {
|
|
949
|
+
return self.fail(node, "template args nest too deep");
|
|
950
|
+
}
|
|
951
|
+
const binder: Binder = if (attrOf(node, arg.name)) |attribute|
|
|
952
|
+
try self.argBinder(node, attribute)
|
|
953
|
+
else if (arg.default) |default| blk: {
|
|
954
|
+
if (std.mem.indexOfScalar(u8, default, '{') != null) {
|
|
955
|
+
return self.fail(template_node, markup.template_default_literal_message);
|
|
956
|
+
}
|
|
957
|
+
// Quotes are not string delimiters in a default; they
|
|
958
|
+
// would render verbatim (engine and validator parity).
|
|
959
|
+
if (default.len > 0 and (default[0] == '\'' or default[0] == '"')) {
|
|
960
|
+
return self.fail(template_node, markup.template_default_quoted_message);
|
|
961
|
+
}
|
|
962
|
+
break :blk .{ .value = expr.kindOf(reflect.literalValue(default)) };
|
|
963
|
+
} else return self.fail(node, markup.use_missing_arg_message);
|
|
964
|
+
self.entries[saved_len + arg_count] = .{ .name = arg.name, .binder = binder };
|
|
965
|
+
arg_count += 1;
|
|
966
|
+
}
|
|
967
|
+
if (saved_len + arg_count >= max_scope_depth) {
|
|
968
|
+
return self.fail(node, "template args nest too deep");
|
|
969
|
+
}
|
|
970
|
+
self.entries[saved_len + arg_count] = .{
|
|
971
|
+
.name = "",
|
|
972
|
+
.binder = .{ .slot = .{
|
|
973
|
+
.nodes = node.children,
|
|
974
|
+
.len = saved_len,
|
|
975
|
+
.floor = saved_floor,
|
|
976
|
+
.template_ctx = saved_ctx,
|
|
977
|
+
} },
|
|
978
|
+
};
|
|
979
|
+
arg_count += 1;
|
|
980
|
+
|
|
981
|
+
self.len = saved_len + arg_count;
|
|
982
|
+
self.floor = saved_len;
|
|
983
|
+
self.use_depth += 1;
|
|
984
|
+
self.template_ctx = template_index;
|
|
985
|
+
defer {
|
|
986
|
+
self.len = saved_len;
|
|
987
|
+
self.floor = saved_floor;
|
|
988
|
+
self.use_depth -= 1;
|
|
989
|
+
self.template_ctx = saved_ctx;
|
|
990
|
+
}
|
|
991
|
+
try self.checkElement(template_node.children[0]);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/// A template arg's binder, mirroring the engines' resolution order:
|
|
995
|
+
/// a bare binding naming a slice arg in scope re-passes it; a binding
|
|
996
|
+
/// naming a model iterable (strings stay scalars) binds as a slice;
|
|
997
|
+
/// anything else is a value arg carrying its use-site kind — which is
|
|
998
|
+
/// how argument types flow through a template's interface.
|
|
999
|
+
fn argBinder(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr) CheckErr!Binder {
|
|
1000
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse {
|
|
1001
|
+
return self.failAttr(node, attribute, markup.invalid_expression_message);
|
|
1002
|
+
};
|
|
1003
|
+
if (expression == .binding) {
|
|
1004
|
+
const path = expression.binding;
|
|
1005
|
+
if (self.lookup(pathHead(path))) |entry| {
|
|
1006
|
+
if (entry.binder == .slice and pathTail(path) == null) {
|
|
1007
|
+
return entry.binder;
|
|
1008
|
+
}
|
|
1009
|
+
} else {
|
|
1010
|
+
for (self.contract.iterables) |*iterable| {
|
|
1011
|
+
if (std.mem.eql(u8, iterable.name, path) and !std.mem.eql(u8, iterable.item_type, "u8")) {
|
|
1012
|
+
self.markModel(path);
|
|
1013
|
+
return .{ .slice = itemRefOf(iterable) };
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
return .{ .value = try self.attrKind(node, attribute, attribute.value) };
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
fn checkSlot(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1022
|
+
_ = node;
|
|
1023
|
+
const capture = self.slotCapture() orelse return;
|
|
1024
|
+
if (capture.nodes.len == 0) return;
|
|
1025
|
+
// Slot content checks in the CONSUMER's scope, exactly where the
|
|
1026
|
+
// <use> was written (engine parity).
|
|
1027
|
+
var saved_entries: [max_scope_depth]ScopeEntry = undefined;
|
|
1028
|
+
const saved_len = self.len;
|
|
1029
|
+
const saved_floor = self.floor;
|
|
1030
|
+
const saved_ctx = self.template_ctx;
|
|
1031
|
+
for (self.entries[capture.len..saved_len], 0..) |entry, offset| {
|
|
1032
|
+
saved_entries[offset] = entry;
|
|
1033
|
+
}
|
|
1034
|
+
self.len = capture.len;
|
|
1035
|
+
self.floor = capture.floor;
|
|
1036
|
+
self.template_ctx = capture.template_ctx;
|
|
1037
|
+
defer {
|
|
1038
|
+
for (saved_entries[0 .. saved_len - capture.len], 0..) |entry, offset| {
|
|
1039
|
+
self.entries[capture.len + offset] = entry;
|
|
1040
|
+
}
|
|
1041
|
+
self.len = saved_len;
|
|
1042
|
+
self.floor = saved_floor;
|
|
1043
|
+
self.template_ctx = saved_ctx;
|
|
1044
|
+
}
|
|
1045
|
+
try self.checkChildList(capture.nodes);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// ------------------------------------------------------------ walk
|
|
1049
|
+
|
|
1050
|
+
fn checkNode(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1051
|
+
switch (node.kind) {
|
|
1052
|
+
.element => try self.checkElement(node),
|
|
1053
|
+
.use_block => try self.checkUse(node),
|
|
1054
|
+
else => {},
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
fn checkChildList(self: *Checker, children: []const markup.MarkupNode) CheckErr!void {
|
|
1059
|
+
var index: usize = 0;
|
|
1060
|
+
while (index < children.len) : (index += 1) {
|
|
1061
|
+
const child = children[index];
|
|
1062
|
+
switch (child.kind) {
|
|
1063
|
+
.element => try self.checkElement(child),
|
|
1064
|
+
.use_block => try self.checkUse(child),
|
|
1065
|
+
.slot_block => try self.checkSlot(child),
|
|
1066
|
+
.text => try self.checkTextRun(child),
|
|
1067
|
+
.for_block => {
|
|
1068
|
+
try self.checkFor(child);
|
|
1069
|
+
if (index + 1 < children.len and children[index + 1].kind == .else_block) {
|
|
1070
|
+
index += 1;
|
|
1071
|
+
try self.checkChildList(children[index].children);
|
|
1072
|
+
}
|
|
1073
|
+
},
|
|
1074
|
+
.if_block => {
|
|
1075
|
+
if (child.attr("test")) |test_value| {
|
|
1076
|
+
// Conditions are truthy over any kind; resolving
|
|
1077
|
+
// the bindings is the check.
|
|
1078
|
+
const test_attr = attrOf(child, "test").?;
|
|
1079
|
+
_ = try self.attrKind(child, test_attr, test_value);
|
|
1080
|
+
}
|
|
1081
|
+
try self.checkChildList(child.children);
|
|
1082
|
+
if (index + 1 < children.len and children[index + 1].kind == .else_block) {
|
|
1083
|
+
index += 1;
|
|
1084
|
+
try self.checkChildList(children[index].children);
|
|
1085
|
+
}
|
|
1086
|
+
},
|
|
1087
|
+
.else_block, .template_block, .import_block => {},
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
fn checkTextRun(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1093
|
+
var rest = node.text;
|
|
1094
|
+
while (std.mem.indexOfScalar(u8, rest, '{')) |open| {
|
|
1095
|
+
const close = std.mem.indexOfScalarPos(u8, rest, open, '}') orelse return;
|
|
1096
|
+
const inner = std.mem.trim(u8, rest[open + 1 .. close], " ");
|
|
1097
|
+
if (markup.isBindingPath(inner)) {
|
|
1098
|
+
_ = try self.resolveBinding(node, inner, true);
|
|
1099
|
+
} else {
|
|
1100
|
+
_ = try self.exprTreeKind(node, inner);
|
|
1101
|
+
}
|
|
1102
|
+
rest = rest[close + 1 ..];
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
fn checkElement(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1107
|
+
if (std.mem.eql(u8, node.name, "span")) return self.checkSpan(node);
|
|
1108
|
+
if (std.mem.eql(u8, node.name, "markdown")) return self.checkMarkdown(node);
|
|
1109
|
+
if (std.mem.eql(u8, node.name, "stepper")) return self.checkStepper(node);
|
|
1110
|
+
if (std.mem.eql(u8, node.name, "timeline-item")) return self.checkTimelineItem(node);
|
|
1111
|
+
if (std.mem.eql(u8, node.name, "chart")) return self.checkChart(node);
|
|
1112
|
+
if (std.mem.eql(u8, node.name, "timeline")) {
|
|
1113
|
+
for (node.attrs) |attribute| {
|
|
1114
|
+
if (std.mem.eql(u8, attribute.name, "gap") or std.mem.eql(u8, attribute.name, "grow")) {
|
|
1115
|
+
try self.checkClassAttr(node, attribute, .number);
|
|
1116
|
+
} else if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1117
|
+
try self.checkKeyAttr(node, attribute);
|
|
1118
|
+
} else if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1119
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1120
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
return self.checkChildList(node.children);
|
|
1124
|
+
}
|
|
1125
|
+
for (node.attrs) |attribute| {
|
|
1126
|
+
if (std.mem.eql(u8, attribute.name, "kind")) continue;
|
|
1127
|
+
if (std.mem.startsWith(u8, attribute.name, "on-")) {
|
|
1128
|
+
try self.checkMessageAttr(node, attribute);
|
|
1129
|
+
continue;
|
|
1130
|
+
}
|
|
1131
|
+
if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1132
|
+
try self.checkKeyAttr(node, attribute);
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
if (std.mem.eql(u8, attribute.name, "role")) {
|
|
1136
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1137
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, role_attr_message);
|
|
1138
|
+
continue;
|
|
1139
|
+
}
|
|
1140
|
+
if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1141
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1142
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
if (std.mem.eql(u8, attribute.name, "image")) {
|
|
1146
|
+
// Runtime image ids are model integers (engine parity).
|
|
1147
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse continue;
|
|
1148
|
+
if (expression != .binding) continue;
|
|
1149
|
+
const resolved = try self.resolveBinding(node, expression.binding, true);
|
|
1150
|
+
try self.requireAttrKind(node, attribute, resolved.kind, &.{.integer}, markup.avatar_image_message);
|
|
1151
|
+
continue;
|
|
1152
|
+
}
|
|
1153
|
+
if (std.mem.eql(u8, attribute.name, "icon") or
|
|
1154
|
+
(std.mem.eql(u8, attribute.name, "name") and std.mem.eql(u8, node.name, "icon")))
|
|
1155
|
+
{
|
|
1156
|
+
try self.checkIconAttr(node, attribute);
|
|
1157
|
+
continue;
|
|
1158
|
+
}
|
|
1159
|
+
// Literal-vocabulary attributes (built-in icon names, anchors,
|
|
1160
|
+
// style tokens) are the structural validator's job; unknown
|
|
1161
|
+
// attributes are too.
|
|
1162
|
+
if (attrClass(attribute.name)) |class| {
|
|
1163
|
+
try self.checkClassAttr(node, attribute, class);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
try self.checkChildList(node.children);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
fn checkKeyAttr(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr) CheckErr!void {
|
|
1170
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1171
|
+
try self.requireAttrKind(node, attribute, kind, &.{ .integer, .string }, attr_key_kind_message);
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/// Icon-valued attributes (`<icon name>`, the inline icon attribute,
|
|
1175
|
+
/// timeline-item's indicator icon): bare literals stay the structural
|
|
1176
|
+
/// validator's closed built-in vocabulary; this pass adds the two
|
|
1177
|
+
/// forms only the contract can see — `app:<name>` against the app's
|
|
1178
|
+
/// registered `app_icons` list (with a did-you-mean over it), and
|
|
1179
|
+
/// `{bindings}` against the model (an icon name is a string).
|
|
1180
|
+
fn checkIconAttr(self: *Checker, node: markup.MarkupNode, attribute: markup.MarkupAttr) CheckErr!void {
|
|
1181
|
+
switch (markup.attrTyped(attribute)) {
|
|
1182
|
+
.literal => |text| {
|
|
1183
|
+
if (!std.mem.startsWith(u8, text, app_icon_prefix)) return;
|
|
1184
|
+
const bare = text[app_icon_prefix.len..];
|
|
1185
|
+
if (nameListed(self.contract.app_icons, bare)) return;
|
|
1186
|
+
if (self.contract.app_icons.len == 0) {
|
|
1187
|
+
return self.failAttr(node, attribute, no_app_icons_message);
|
|
1188
|
+
}
|
|
1189
|
+
return self.failNamed(node, unknown_app_icon_message, bare, .{ .names = self.contract.app_icons });
|
|
1190
|
+
},
|
|
1191
|
+
.binding => |path| {
|
|
1192
|
+
const resolved = try self.resolveBinding(node, path, true);
|
|
1193
|
+
try self.requireAttrKind(node, attribute, resolved.kind, &.{.string}, icon_binding_kind_message);
|
|
1194
|
+
},
|
|
1195
|
+
else => {},
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
fn checkMarkdown(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1200
|
+
for (node.attrs) |attribute| {
|
|
1201
|
+
if (std.mem.eql(u8, attribute.name, "source")) {
|
|
1202
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse continue;
|
|
1203
|
+
if (expression != .binding) continue;
|
|
1204
|
+
const resolved = try self.resolveBinding(node, expression.binding, true);
|
|
1205
|
+
try self.requireAttrKind(node, attribute, resolved.kind, &.{.string}, markup.markdown_source_message);
|
|
1206
|
+
continue;
|
|
1207
|
+
}
|
|
1208
|
+
if (std.mem.eql(u8, attribute.name, "on-link")) {
|
|
1209
|
+
const expression = markup.parseMessageExpression(attribute.value) orelse continue;
|
|
1210
|
+
const tag = self.findMsg(expression.tag) orelse return self.failAttr(node, attribute, markup.markdown_on_link_message);
|
|
1211
|
+
if (tag.payload != .string or !std.mem.eql(u8, tag.payload_type, "[]const u8")) {
|
|
1212
|
+
return self.failAttr(node, attribute, markup.markdown_on_link_message);
|
|
1213
|
+
}
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
if (std.mem.eql(u8, attribute.name, "on-details")) {
|
|
1217
|
+
const expression = markup.parseMessageExpression(attribute.value) orelse continue;
|
|
1218
|
+
const tag = self.findMsg(expression.tag) orelse return self.failAttr(node, attribute, markup.markdown_on_details_message);
|
|
1219
|
+
if (tag.payload != .integer or !std.mem.eql(u8, tag.payload_type, "usize")) {
|
|
1220
|
+
return self.failAttr(node, attribute, markup.markdown_on_details_message);
|
|
1221
|
+
}
|
|
1222
|
+
continue;
|
|
1223
|
+
}
|
|
1224
|
+
if (std.mem.eql(u8, attribute.name, "details-expanded")) {
|
|
1225
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse continue;
|
|
1226
|
+
if (expression != .binding) continue;
|
|
1227
|
+
const item = try self.resolveIterable(node, expression.binding, markup.markdown_details_expanded_message);
|
|
1228
|
+
if (!std.mem.eql(u8, item.type_name, "bool")) {
|
|
1229
|
+
return self.failAttr(node, attribute, markup.markdown_details_expanded_message);
|
|
1230
|
+
}
|
|
1231
|
+
continue;
|
|
1232
|
+
}
|
|
1233
|
+
if (std.mem.eql(u8, attribute.name, "issue-link-base")) {
|
|
1234
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1235
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, markup.markdown_issue_link_base_message);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
fn checkStepper(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1241
|
+
for (node.attrs) |attribute| {
|
|
1242
|
+
if (std.mem.eql(u8, attribute.name, "active")) {
|
|
1243
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1244
|
+
try self.requireAttrKind(node, attribute, kind, &.{.integer}, markup.stepper_active_message);
|
|
1245
|
+
} else if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1246
|
+
try self.checkKeyAttr(node, attribute);
|
|
1247
|
+
} else if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1248
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1249
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
for (node.children) |child| {
|
|
1253
|
+
for (child.children) |run| {
|
|
1254
|
+
if (run.kind == .text) try self.checkTextRun(run);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
/// `<chart>` and its `<series>` children: options resolve like the
|
|
1260
|
+
/// engines' (numbers, whole numbers, truthy flags, text), and every
|
|
1261
|
+
/// series `values` binding must name an f32 iterable — a wrong item
|
|
1262
|
+
/// type fails with the model type named, so the fix is visible from
|
|
1263
|
+
/// the finding.
|
|
1264
|
+
fn checkChart(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1265
|
+
for (node.attrs) |attribute| {
|
|
1266
|
+
const number_attr = std.mem.eql(u8, attribute.name, "y-min") or
|
|
1267
|
+
std.mem.eql(u8, attribute.name, "y-max") or
|
|
1268
|
+
std.mem.eql(u8, attribute.name, "stroke-width") or
|
|
1269
|
+
std.mem.eql(u8, attribute.name, "width") or
|
|
1270
|
+
std.mem.eql(u8, attribute.name, "height") or
|
|
1271
|
+
std.mem.eql(u8, attribute.name, "grow") or
|
|
1272
|
+
std.mem.eql(u8, attribute.name, "padding");
|
|
1273
|
+
if (number_attr) {
|
|
1274
|
+
try self.checkClassAttr(node, attribute, .number);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
if (std.mem.eql(u8, attribute.name, "grid-lines")) {
|
|
1278
|
+
try self.checkClassAttr(node, attribute, .whole);
|
|
1279
|
+
continue;
|
|
1280
|
+
}
|
|
1281
|
+
if (std.mem.eql(u8, attribute.name, "baseline") or
|
|
1282
|
+
std.mem.eql(u8, attribute.name, "y-labels") or
|
|
1283
|
+
std.mem.eql(u8, attribute.name, "hover-details"))
|
|
1284
|
+
{
|
|
1285
|
+
_ = try self.attrKind(node, attribute, attribute.value);
|
|
1286
|
+
continue;
|
|
1287
|
+
}
|
|
1288
|
+
if (std.mem.eql(u8, attribute.name, "x-labels")) {
|
|
1289
|
+
// Like series values, but the iterable's items are
|
|
1290
|
+
// strings: a wrong item type fails with the model type
|
|
1291
|
+
// named, so the fix is visible from the finding.
|
|
1292
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse continue;
|
|
1293
|
+
if (expression != .binding) continue;
|
|
1294
|
+
const item = try self.resolveIterable(node, expression.binding, markup.chart_x_labels_message);
|
|
1295
|
+
if (!std.mem.eql(u8, item.type_name, "[]const u8")) {
|
|
1296
|
+
const message = std.fmt.allocPrint(self.arena, "{s} (\"{s}\" iterates {s})", .{
|
|
1297
|
+
markup.chart_x_labels_message, expression.binding, item.type_name,
|
|
1298
|
+
}) catch return error.OutOfMemory;
|
|
1299
|
+
return self.failAttr(node, attribute, message);
|
|
1300
|
+
}
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1304
|
+
try self.checkKeyAttr(node, attribute);
|
|
1305
|
+
continue;
|
|
1306
|
+
}
|
|
1307
|
+
if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1308
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1309
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
for (node.children) |child| {
|
|
1313
|
+
if (child.kind != .element or !std.mem.eql(u8, child.name, "series")) continue;
|
|
1314
|
+
try self.checkSeries(child);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
fn checkSeries(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1319
|
+
for (node.attrs) |attribute| {
|
|
1320
|
+
if (std.mem.eql(u8, attribute.name, "values")) {
|
|
1321
|
+
const expression = markup.parseAttrExpression(attribute.value) orelse continue;
|
|
1322
|
+
if (expression != .binding) continue;
|
|
1323
|
+
const item = try self.resolveIterable(node, expression.binding, markup.series_values_message);
|
|
1324
|
+
if (!std.mem.eql(u8, item.type_name, "f32")) {
|
|
1325
|
+
const message = std.fmt.allocPrint(self.arena, "{s} (\"{s}\" iterates {s})", .{
|
|
1326
|
+
markup.series_values_message, expression.binding, item.type_name,
|
|
1327
|
+
}) catch return error.OutOfMemory;
|
|
1328
|
+
return self.failAttr(node, attribute, message);
|
|
1329
|
+
}
|
|
1330
|
+
continue;
|
|
1331
|
+
}
|
|
1332
|
+
if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1333
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1334
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, markup.series_label_message);
|
|
1335
|
+
}
|
|
1336
|
+
// kind and color are closed literal vocabularies — the
|
|
1337
|
+
// structural validator's job.
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
/// One `<span>` inside a text paragraph: weight resolves like any
|
|
1342
|
+
/// option-name attribute (bindings must produce a string), scale
|
|
1343
|
+
/// bindings must produce a NUMBER (the multiplier on the paragraph's
|
|
1344
|
+
/// base size — positivity is a value check, the engines' job), the
|
|
1345
|
+
/// flags resolve truthy over any kind, and the run's `{bindings}`
|
|
1346
|
+
/// check through the child walk exactly like any rendered text.
|
|
1347
|
+
/// foreground is a closed literal vocabulary — the structural
|
|
1348
|
+
/// validator's job.
|
|
1349
|
+
fn checkSpan(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1350
|
+
for (node.attrs) |attribute| {
|
|
1351
|
+
if (std.mem.eql(u8, attribute.name, "weight")) {
|
|
1352
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1353
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, markup.span_weight_value_message);
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
if (std.mem.eql(u8, attribute.name, "scale")) {
|
|
1357
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1358
|
+
try self.requireAttrKind(node, attribute, kind, &.{ .integer, .float }, markup.span_scale_value_message);
|
|
1359
|
+
continue;
|
|
1360
|
+
}
|
|
1361
|
+
if (std.mem.eql(u8, attribute.name, "mono") or std.mem.eql(u8, attribute.name, "italic") or std.mem.eql(u8, attribute.name, "underline")) {
|
|
1362
|
+
_ = try self.attrKind(node, attribute, attribute.value);
|
|
1363
|
+
continue;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
try self.checkChildList(node.children);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
fn checkTimelineItem(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1370
|
+
for (node.attrs) |attribute| {
|
|
1371
|
+
const text_attr = std.mem.eql(u8, attribute.name, "title") or
|
|
1372
|
+
std.mem.eql(u8, attribute.name, "description") or
|
|
1373
|
+
std.mem.eql(u8, attribute.name, "meta") or
|
|
1374
|
+
std.mem.eql(u8, attribute.name, "indicator");
|
|
1375
|
+
if (text_attr) {
|
|
1376
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1377
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, markup.timeline_item_text_attr_message);
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
if (std.mem.eql(u8, attribute.name, "variant")) {
|
|
1381
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1382
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, option_attr_message);
|
|
1383
|
+
continue;
|
|
1384
|
+
}
|
|
1385
|
+
if (std.mem.eql(u8, attribute.name, "connector") or std.mem.eql(u8, attribute.name, "selected")) {
|
|
1386
|
+
_ = try self.attrKind(node, attribute, attribute.value);
|
|
1387
|
+
continue;
|
|
1388
|
+
}
|
|
1389
|
+
if (std.mem.eql(u8, attribute.name, "icon")) {
|
|
1390
|
+
try self.checkIconAttr(node, attribute);
|
|
1391
|
+
continue;
|
|
1392
|
+
}
|
|
1393
|
+
if (std.mem.eql(u8, attribute.name, "on-press")) {
|
|
1394
|
+
try self.checkMessageAttr(node, attribute);
|
|
1395
|
+
continue;
|
|
1396
|
+
}
|
|
1397
|
+
if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1398
|
+
try self.checkKeyAttr(node, attribute);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
fn attrOf(node: markup.MarkupNode, name: []const u8) ?markup.MarkupAttr {
|
|
1405
|
+
for (node.attrs) |attribute| {
|
|
1406
|
+
if (std.mem.eql(u8, attribute.name, name)) return attribute;
|
|
1407
|
+
}
|
|
1408
|
+
return null;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
fn itemRefOf(iterable: *const Iterable) ItemRef {
|
|
1412
|
+
return .{
|
|
1413
|
+
.type_name = iterable.item_type,
|
|
1414
|
+
.kind = iterable.item_kind,
|
|
1415
|
+
.scalar = iterable.item_scalar,
|
|
1416
|
+
.group = &iterable.item,
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
const GroupResolve = union(enum) {
|
|
1421
|
+
ok: Resolved,
|
|
1422
|
+
arena_blocked,
|
|
1423
|
+
missing,
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
/// Resolve a dotted path on a group: nested groups for every segment but
|
|
1427
|
+
/// the last, then a scalar leaf. Mirrors the interpreter's `resolveOn`
|
|
1428
|
+
/// (fields, zero-arg methods, arena methods) over the contract's data.
|
|
1429
|
+
fn resolveOnGroup(group: *const Group, path: []const u8, allow_arena: bool) GroupResolve {
|
|
1430
|
+
const head = pathHead(path);
|
|
1431
|
+
if (pathTail(path)) |tail| {
|
|
1432
|
+
for (group.groups) |*named| {
|
|
1433
|
+
if (std.mem.eql(u8, named.name, head)) return resolveOnGroup(&named.group, tail, allow_arena);
|
|
1434
|
+
}
|
|
1435
|
+
return .missing;
|
|
1436
|
+
}
|
|
1437
|
+
for (group.scalars) |scalar| {
|
|
1438
|
+
if (!std.mem.eql(u8, scalar.name, head)) continue;
|
|
1439
|
+
if (scalar.arena and !allow_arena) return .arena_blocked;
|
|
1440
|
+
return .{ .ok = .{ .kind = scalar.kind, .type_name = scalar.type_name } };
|
|
1441
|
+
}
|
|
1442
|
+
return .missing;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
fn pathHead(path: []const u8) []const u8 {
|
|
1446
|
+
const dot = std.mem.indexOfScalar(u8, path, '.') orelse return path;
|
|
1447
|
+
return path[0..dot];
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
fn pathTail(path: []const u8) ?[]const u8 {
|
|
1451
|
+
const dot = std.mem.indexOfScalar(u8, path, '.') orelse return null;
|
|
1452
|
+
return path[dot + 1 ..];
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
// ----------------------------------------------------------- did-you-mean
|
|
1456
|
+
|
|
1457
|
+
const NameSource = union(enum) {
|
|
1458
|
+
model: *const Contract,
|
|
1459
|
+
group: *const Group,
|
|
1460
|
+
iterables: *const Contract,
|
|
1461
|
+
msgs: *const Contract,
|
|
1462
|
+
/// A plain name list (the contract's registered app icons).
|
|
1463
|
+
names: []const []const u8,
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
fn nearestCandidate(token: []const u8, source: NameSource) ?[]const u8 {
|
|
1467
|
+
var best: ?[]const u8 = null;
|
|
1468
|
+
var best_distance: usize = 3;
|
|
1469
|
+
switch (source) {
|
|
1470
|
+
.model => |contract| {
|
|
1471
|
+
for (contract.model.scalars) |scalar| considerName(token, scalar.name, &best, &best_distance);
|
|
1472
|
+
for (contract.model.groups) |group| considerName(token, group.name, &best, &best_distance);
|
|
1473
|
+
for (contract.iterables) |iterable| considerName(token, iterable.name, &best, &best_distance);
|
|
1474
|
+
},
|
|
1475
|
+
.group => |group| {
|
|
1476
|
+
for (group.scalars) |scalar| considerName(token, scalar.name, &best, &best_distance);
|
|
1477
|
+
for (group.groups) |nested| considerName(token, nested.name, &best, &best_distance);
|
|
1478
|
+
},
|
|
1479
|
+
.iterables => |contract| {
|
|
1480
|
+
for (contract.iterables) |iterable| considerName(token, iterable.name, &best, &best_distance);
|
|
1481
|
+
},
|
|
1482
|
+
.msgs => |contract| {
|
|
1483
|
+
for (contract.msgs) |tag| considerName(token, tag.name, &best, &best_distance);
|
|
1484
|
+
},
|
|
1485
|
+
.names => |names| {
|
|
1486
|
+
for (names) |name| considerName(token, name, &best, &best_distance);
|
|
1487
|
+
},
|
|
1488
|
+
}
|
|
1489
|
+
return best;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
fn considerName(token: []const u8, name: []const u8, best: *?[]const u8, best_distance: *usize) void {
|
|
1493
|
+
const distance = editDistance(token, name) orelse return;
|
|
1494
|
+
if (distance < best_distance.*) {
|
|
1495
|
+
best_distance.* = distance;
|
|
1496
|
+
best.* = name;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
/// Bounded Levenshtein distance; null when either side is too long for
|
|
1501
|
+
/// the fixed buffer (binding names are short).
|
|
1502
|
+
fn editDistance(a: []const u8, b: []const u8) ?usize {
|
|
1503
|
+
if (b.len > 63 or a.len > 255) return null;
|
|
1504
|
+
var previous: [64]usize = undefined;
|
|
1505
|
+
var current: [64]usize = undefined;
|
|
1506
|
+
for (0..b.len + 1) |j| previous[j] = j;
|
|
1507
|
+
for (a, 0..) |a_char, i| {
|
|
1508
|
+
current[0] = i + 1;
|
|
1509
|
+
for (b, 0..) |b_char, j| {
|
|
1510
|
+
const substitution_cost: usize = if (a_char == b_char) 0 else 1;
|
|
1511
|
+
current[j + 1] = @min(
|
|
1512
|
+
previous[j] + substitution_cost,
|
|
1513
|
+
@min(current[j] + 1, previous[j + 1] + 1),
|
|
1514
|
+
);
|
|
1515
|
+
}
|
|
1516
|
+
@memcpy(previous[0 .. b.len + 1], current[0 .. b.len + 1]);
|
|
1517
|
+
}
|
|
1518
|
+
return previous[b.len];
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
// -------------------------------------------------------------- artifact
|
|
1522
|
+
|
|
1523
|
+
/// Hash the app's Zig sources: every `.zig` file under `root_path`,
|
|
1524
|
+
/// path-sorted, path and contents both mixed in. The emit step stamps
|
|
1525
|
+
/// this into the artifact; `native check` recomputes it and DEGRADES to
|
|
1526
|
+
/// structural checking on any mismatch — a stale contract can hide new
|
|
1527
|
+
/// fields or resurrect deleted ones, so it must never pass silently.
|
|
1528
|
+
pub fn hashSourceDir(allocator: std.mem.Allocator, io: std.Io, root_path: []const u8) !u64 {
|
|
1529
|
+
return hashSourceDirAt(allocator, io, std.Io.Dir.cwd(), root_path);
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
pub fn hashSourceDirAt(allocator: std.mem.Allocator, io: std.Io, base: std.Io.Dir, root_path: []const u8) !u64 {
|
|
1533
|
+
var paths: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
1534
|
+
defer {
|
|
1535
|
+
for (paths.items) |path| allocator.free(path);
|
|
1536
|
+
paths.deinit(allocator);
|
|
1537
|
+
}
|
|
1538
|
+
var root = try base.openDir(io, root_path, .{ .iterate = true });
|
|
1539
|
+
defer root.close(io);
|
|
1540
|
+
var walker = try root.walk(allocator);
|
|
1541
|
+
defer walker.deinit();
|
|
1542
|
+
while (try walker.next(io)) |entry| {
|
|
1543
|
+
if (entry.kind == .file and std.mem.endsWith(u8, entry.path, ".zig")) {
|
|
1544
|
+
try paths.append(allocator, try allocator.dupe(u8, entry.path));
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
std.mem.sort([]const u8, paths.items, {}, stringLessThan);
|
|
1548
|
+
var hasher = std.hash.Wyhash.init(0x5eed_c0de);
|
|
1549
|
+
for (paths.items) |path| {
|
|
1550
|
+
hasher.update(path);
|
|
1551
|
+
hasher.update(&.{0});
|
|
1552
|
+
var file = try root.openFile(io, path, .{});
|
|
1553
|
+
defer file.close(io);
|
|
1554
|
+
var read_buffer: [16 * 1024]u8 = undefined;
|
|
1555
|
+
var reader = file.reader(io, &read_buffer);
|
|
1556
|
+
const contents = try reader.interface.allocRemaining(allocator, .limited(64 * 1024 * 1024));
|
|
1557
|
+
defer allocator.free(contents);
|
|
1558
|
+
hasher.update(contents);
|
|
1559
|
+
hasher.update(&.{0});
|
|
1560
|
+
}
|
|
1561
|
+
return hasher.final();
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
fn stringLessThan(_: void, a: []const u8, b: []const u8) bool {
|
|
1565
|
+
return std.mem.lessThan(u8, a, b);
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
/// Parse a serialized contract artifact. The result's slices are
|
|
1569
|
+
/// allocated from `allocator` (hand it an arena).
|
|
1570
|
+
pub fn parseArtifact(allocator: std.mem.Allocator, source: []const u8) error{ OutOfMemory, ParseZon }!Contract {
|
|
1571
|
+
const source_z = try allocator.dupeZ(u8, source);
|
|
1572
|
+
return std.zon.parse.fromSliceAlloc(Contract, allocator, source_z, null, .{});
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
/// Serialize a contract as ZON (the artifact format `parseArtifact`
|
|
1576
|
+
/// reads back).
|
|
1577
|
+
pub fn writeArtifact(contract: Contract, writer: *std.Io.Writer) !void {
|
|
1578
|
+
try std.zon.stringify.serializeMaxDepth(contract, .{}, writer, 256);
|
|
1579
|
+
try writer.writeByte('\n');
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
// ------------------------------------------------------------- emit main
|
|
1583
|
+
|
|
1584
|
+
/// The whole body of the per-app contract emit program: the app's build
|
|
1585
|
+
/// wires a tiny root that calls this with its own module. Apps without a
|
|
1586
|
+
/// public Model/Msg pair have no markup contract; the step is a silent
|
|
1587
|
+
/// no-op for them.
|
|
1588
|
+
pub fn emitMain(comptime app: type, comptime specials: Specials, init: std.process.Init) !void {
|
|
1589
|
+
if (comptime !(@hasDecl(app, "Model") and @hasDecl(app, "Msg"))) return;
|
|
1590
|
+
if (comptime (@typeInfo(app.Model) != .@"struct" or @typeInfo(app.Msg) != .@"union")) return;
|
|
1591
|
+
const allocator = init.arena.allocator();
|
|
1592
|
+
const args = try init.minimal.args.toSlice(allocator);
|
|
1593
|
+
var src_root: []const u8 = default_source_root;
|
|
1594
|
+
var out_path: []const u8 = default_artifact_path;
|
|
1595
|
+
var index: usize = 1;
|
|
1596
|
+
while (index < args.len) : (index += 1) {
|
|
1597
|
+
if (std.mem.eql(u8, args[index], "--src") and index + 1 < args.len) {
|
|
1598
|
+
index += 1;
|
|
1599
|
+
src_root = args[index];
|
|
1600
|
+
} else if (std.mem.eql(u8, args[index], "--out") and index + 1 < args.len) {
|
|
1601
|
+
index += 1;
|
|
1602
|
+
out_path = args[index];
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
var contract = comptime describe(app.Model, app.Msg, specials);
|
|
1606
|
+
// The registered icon vocabulary rides the same artifact: one
|
|
1607
|
+
// `pub const app_icons` declaration feeds boot-time registration and
|
|
1608
|
+
// this reflection, so `native check` verifies `app:` markup
|
|
1609
|
+
// references against exactly what the app registers.
|
|
1610
|
+
contract.app_icons = comptime appIconNames(app);
|
|
1611
|
+
contract.source_root = src_root;
|
|
1612
|
+
contract.source_hash = try hashSourceDir(allocator, init.io, src_root);
|
|
1613
|
+
var out: std.Io.Writer.Allocating = .init(allocator);
|
|
1614
|
+
defer out.deinit();
|
|
1615
|
+
try writeArtifact(contract, &out.writer);
|
|
1616
|
+
if (std.fs.path.dirname(out_path)) |dir| {
|
|
1617
|
+
std.Io.Dir.cwd().createDirPath(init.io, dir) catch {};
|
|
1618
|
+
}
|
|
1619
|
+
try std.Io.Dir.cwd().writeFile(init.io, .{ .sub_path = out_path, .data = out.written() });
|
|
1620
|
+
}
|