Add WASM parser and Vue dissector UI.

Ship a working HexPigeon: typed binary parsing in Rust/WASM, a three-pane frontend, and unit tests covering the field types.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-03 16:26:52 +00:00
co-authored by Cursor
parent f4b19e87b1
commit 71974f5107
35 changed files with 4719 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
<template>
<div class="app">
<header class="app-header">
<div class="brand">
<span class="logo">HexPigeon</span>
<span class="subtitle">Binary Protocol Dissector</span>
</div>
<div class="header-actions">
<button class="btn btn-primary" @click="store.runParse()" :disabled="!store.wasmReady">
{{ store.wasmReady ? 'Parse' : 'Loading engine...' }}
</button>
</div>
</header>
<div v-if="store.wasmError" class="banner banner-error">
{{ store.wasmError }}
<small>Run <code>make wasm</code> then <code>make dev</code>.</small>
</div>
<main class="panels">
<aside class="panel panel-schema">
<PanelHeader title="Schema">
<select v-model="store.schemaFormat" class="format-select" aria-label="Schema format">
<option value="json">JSON</option>
<option value="yaml">YAML</option>
</select>
</PanelHeader>
<SchemaEditor />
</aside>
<section class="panel panel-hex">
<PanelHeader title="Hex View">
<span class="byte-count">{{ store.bytes.length }} bytes</span>
</PanelHeader>
<HexInput />
<HexViewer />
</section>
<aside class="panel panel-tree">
<PanelHeader title="Parsed Fields">
<span v-if="store.parseResult" class="byte-count">
{{ store.parseResult.consumed_bytes }}/{{ store.parseResult.total_bytes }} B
</span>
</PanelHeader>
<div v-if="store.parseError" class="parse-error">
Error: {{ store.parseError }}
</div>
<ParsedTree v-if="store.parseResult" :fields="store.parseResult.fields" />
<div v-else-if="!store.parseError" class="empty-hint">
Define a schema and hex dump, then select <strong>Parse</strong>.
</div>
</aside>
</main>
</div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue'
import { useParserStore } from '@/stores/parser'
import PanelHeader from '@/components/PanelHeader.vue'
import SchemaEditor from '@/components/SchemaEditor.vue'
import HexInput from '@/components/HexInput.vue'
import HexViewer from '@/components/HexViewer.vue'
import ParsedTree from '@/components/ParsedTree.vue'
const store = useParserStore()
onMounted(async () => {
await store.initWasm()
store.runParse()
})
</script>
+16
View File
@@ -0,0 +1,16 @@
<template>
<div class="hex-input-wrapper">
<textarea
v-model="store.hexInput"
class="hex-input"
spellcheck="false"
placeholder="Paste hex dump here…"
rows="4"
/>
</div>
</template>
<script setup lang="ts">
import { useParserStore } from '@/stores/parser'
const store = useParserStore()
</script>
+130
View File
@@ -0,0 +1,130 @@
<template>
<div class="hex-viewer" ref="viewerEl">
<div class="hex-grid">
<!-- Row labels + hex + ascii -->
<template v-for="(row, ri) in rows" :key="ri">
<!-- Offset -->
<span class="hex-offset">{{ row.offset.toString(16).padStart(4, '0') }}</span>
<!-- Hex bytes -->
<span class="hex-bytes">
<span
v-for="(byte, ci) in row.bytes"
:key="ci"
class="hex-byte"
:class="byteClass(row.offset + ci)"
@mouseenter="onHoverByte(row.offset + ci)"
@mouseleave="onLeave"
@click="onClickByte(row.offset + ci)"
>{{ byte.toString(16).padStart(2, '0') }}</span>
<!-- Padding for last row -->
<span
v-for="p in (16 - row.bytes.length)"
:key="'p' + p"
class="hex-byte hex-pad"
> </span>
</span>
<!-- ASCII -->
<span class="hex-ascii">
<span
v-for="(byte, ci) in row.bytes"
:key="ci"
class="ascii-char"
:class="byteClass(row.offset + ci)"
@mouseenter="onHoverByte(row.offset + ci)"
@mouseleave="onLeave"
>{{ toAscii(byte) }}</span>
</span>
</template>
</div>
<!-- Legend -->
<div v-if="store.selectedField" class="hex-legend">
<span class="legend-dot selected" /> Selected:
<strong>{{ store.selectedField.name }}</strong>
offset {{ store.selectedField.offset }}, {{ store.selectedField.size }}B
</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useParserStore } from '@/stores/parser'
import type { ParsedField } from '@/types/parser'
const store = useParserStore()
const viewerEl = ref<HTMLElement | null>(null)
const BYTES_PER_ROW = 16
interface Row { offset: number; bytes: number[] }
const rows = computed<Row[]>(() => {
const bs = store.bytes
const out: Row[] = []
for (let i = 0; i < bs.length; i += BYTES_PER_ROW) {
out.push({ offset: i, bytes: bs.slice(i, i + BYTES_PER_ROW) })
}
return out
})
/** Flatten all fields (incl. children) for range lookups */
const allFields = computed<ParsedField[]>(() => {
if (!store.parseResult) return []
return flatten(store.parseResult.fields)
})
function flatten(fields: ParsedField[]): ParsedField[] {
return fields.flatMap(f => [f, ...(f.children ? flatten(f.children) : [])])
}
/** Return the field containing byte index `i` */
function fieldAt(i: number): ParsedField | undefined {
return allFields.value.find(f => i >= f.offset && i < f.offset + f.size)
}
function byteClass(i: number) {
const hovered = store.hoveredRange
const selected = store.selectedField
const classes: string[] = []
if (selected && i >= selected.offset && i < selected.offset + selected.size) {
classes.push('byte-selected')
}
if (hovered && i >= hovered[0] && i < hovered[1]) {
classes.push('byte-hovered')
}
const field = fieldAt(i)
if (field) {
classes.push('byte-parsed')
if (field.checksum_valid === false) classes.push('byte-error')
else if (field.checksum_valid === true) classes.push('byte-ok')
}
return classes
}
function onHoverByte(i: number) {
const f = fieldAt(i)
if (f) store.setHovered([f.offset, f.offset + f.size])
else store.setHovered(null)
}
function onLeave() {
store.setHovered(null)
}
function onClickByte(i: number) {
const f = fieldAt(i)
store.setSelected(f ?? null)
}
function toAscii(b: number): string {
if (b >= 0x20 && b < 0x7f) return String.fromCharCode(b)
return '·'
}
</script>
+10
View File
@@ -0,0 +1,10 @@
<template>
<div class="panel-header">
<span class="panel-title">{{ title }}</span>
<slot />
</div>
</template>
<script setup lang="ts">
defineProps<{ title: string }>()
</script>
+128
View File
@@ -0,0 +1,128 @@
<template>
<div
class="parsed-node"
:class="{
'node-selected': isSelected,
'node-hovered': isHovered,
'node-error': field.checksum_valid === false,
'node-ok': field.checksum_valid === true,
}"
:style="{ paddingLeft: depth * 16 + 8 + 'px' }"
@mouseenter="onHover"
@mouseleave="onLeave"
@click.stop="onClick"
>
<button
v-if="hasChildren"
class="expand-btn"
:aria-expanded="expanded"
@click.stop="expanded = !expanded"
>
<span class="expand-icon" :class="{ expanded }" />
</button>
<span v-else class="expand-spacer" />
<span class="node-kind">{{ kindLabel }}</span>
<span class="node-name">{{ field.name }}</span>
<span class="node-value">
<template v-if="Array.isArray(field.value)">
<template v-if="isFlagArray(field.value)">
<span
v-for="flag in (field.value as FlagValue[])"
:key="flag.bit"
class="flag-chip"
:class="flag.set ? 'flag-set' : 'flag-unset'"
>{{ flag.name }}</span>
</template>
<template v-else>
<span class="bytes-preview">
{{ (field.value as number[]).map(b => b.toString(16).padStart(2, '0')).join(' ') }}
</span>
</template>
</template>
<template v-else-if="field.value !== null">
<span :class="valueClass">{{ displayValue }}</span>
</template>
</span>
<span class="node-meta">+{{ field.offset }} / {{ field.size }}B</span>
<span v-if="field.checksum_valid === true" class="badge badge-ok">VALID</span>
<span v-else-if="field.checksum_valid === false" class="badge badge-err">INVALID</span>
</div>
<div v-if="field.description && isSelected" class="node-desc" :style="{ paddingLeft: depth * 16 + 36 + 'px' }">
{{ field.description }}
</div>
<template v-if="hasChildren && expanded">
<ParsedNode
v-for="child in field.children"
:key="child.name + child.offset"
:field="child"
:depth="depth + 1"
/>
</template>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { useParserStore } from '@/stores/parser'
import type { ParsedField, FlagValue } from '@/types/parser'
const props = defineProps<{ field: ParsedField; depth: number }>()
const store = useParserStore()
const expanded = ref(true)
const hasChildren = computed(() => !!props.field.children?.length)
const isSelected = computed(() =>
store.selectedField?.offset === props.field.offset &&
store.selectedField?.name === props.field.name
)
const isHovered = computed(() => {
const h = store.hoveredRange
if (!h) return false
return props.field.offset >= h[0] && props.field.offset < h[1]
})
const kindLabel = computed(() =>
props.field.kind.replace(/([A-Z])/g, ' $1').trim()
)
const displayValue = computed(() => {
const v = props.field.value
if (typeof v === 'number') {
if (props.field.kind.toLowerCase().includes('float')) return v.toFixed(6)
return `${v} (0x${v.toString(16).toUpperCase()})`
}
return String(v)
})
const valueClass = computed(() => {
const k = props.field.kind.toLowerCase()
if (k.includes('uint') || k.includes('int')) return 'val-int'
if (k.includes('float')) return 'val-float'
if (k.includes('string')) return 'val-string'
if (k.includes('crc')) return 'val-crc'
return ''
})
function isFlagArray(v: unknown[]): v is FlagValue[] {
return v.length > 0 && typeof (v[0] as FlagValue).bit === 'number'
}
function onHover() {
store.setHovered([props.field.offset, props.field.offset + props.field.size])
}
function onLeave() {
store.setHovered(null)
}
function onClick() {
store.setSelected(isSelected.value ? null : props.field)
}
</script>
+17
View File
@@ -0,0 +1,17 @@
<template>
<div class="parsed-tree">
<ParsedNode
v-for="field in fields"
:key="field.name + field.offset"
:field="field"
:depth="0"
/>
</div>
</template>
<script setup lang="ts">
import type { ParsedField } from '@/types/parser'
import ParsedNode from './ParsedNode.vue'
defineProps<{ fields: ParsedField[] }>()
</script>
+48
View File
@@ -0,0 +1,48 @@
<template>
<div class="schema-editor">
<textarea
v-model="store.schemaText"
class="schema-textarea"
spellcheck="false"
autocomplete="off"
@keydown.tab.prevent="onTab"
@input="onInput"
/>
<div v-if="schemaError" class="schema-error">Error: {{ schemaError }}</div>
</div>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import { useParserStore } from '@/stores/parser'
import { validateJsonSchema } from '@/utils/schema'
const store = useParserStore()
const schemaError = ref<string | null>(null)
function validate() {
if (store.schemaFormat === 'json') {
schemaError.value = validateJsonSchema(store.schemaText)
} else {
schemaError.value = null
}
}
function onInput() {
validate()
}
function onTab(e: KeyboardEvent) {
const el = e.target as HTMLTextAreaElement
const start = el.selectionStart
const end = el.selectionEnd
store.schemaText =
store.schemaText.substring(0, start) + ' ' + store.schemaText.substring(end)
// Move cursor
setTimeout(() => {
el.selectionStart = el.selectionEnd = start + 2
})
}
watch(() => store.schemaFormat, validate)
</script>
+51
View File
@@ -0,0 +1,51 @@
{
"name": "Example Frame",
"endian": "big",
"fields": [
{
"name": "magic",
"type": "uint8",
"description": "Protocol magic byte (0xFF)"
},
{
"name": "payload_len",
"type": "uint8",
"description": "Length of payload string"
},
{
"name": "payload",
"type": "string_fixed",
"length": 5,
"description": "Fixed-length ASCII payload"
},
{
"name": "raw_data",
"type": "bytes",
"length": 4,
"description": "Raw 4-byte data blob"
},
{
"name": "tag",
"type": "string_fixed",
"length": 4,
"description": "ASCII tag (ABCD)"
},
{
"name": "checksum",
"type": "crc32",
"checksum_range": [0, 11],
"description": "CRC-32 over bytes 0-10"
},
{
"name": "flags",
"type": "bitflags16",
"description": "Status flags",
"flags": [
{ "bit": 0, "name": "ACK", "description": "Acknowledge" },
{ "bit": 1, "name": "SYN", "description": "Synchronise" },
{ "bit": 2, "name": "FIN", "description": "Finished" },
{ "bit": 7, "name": "ERR", "description": "Error" }
]
}
]
}
+64
View File
@@ -0,0 +1,64 @@
export const EXAMPLE_SCHEMA = JSON.stringify(
{
name: 'Example Frame',
endian: 'big',
fields: [
{
name: 'magic',
type: 'uint8',
description: 'Protocol magic byte (0xFF)',
},
{
name: 'payload_len',
type: 'uint8',
description: 'Length of payload string',
},
{
name: 'payload',
type: 'string_fixed',
length: 5,
description: 'Fixed-length ASCII payload',
},
{
name: 'raw_data',
type: 'bytes',
length: 4,
description: 'Raw 4-byte data blob',
},
{
name: 'tag',
type: 'string_fixed',
length: 4,
description: 'ASCII tag (ABCD)',
},
{
name: 'checksum',
type: 'crc32',
checksum_range: [0, 11],
description: 'CRC-32 over bytes 0-10',
},
{
name: 'flags',
type: 'bitflags16',
description: 'Status flags',
flags: [
{ bit: 0, name: 'ACK', description: 'Acknowledge' },
{ bit: 1, name: 'SYN', description: 'Synchronise' },
{ bit: 2, name: 'FIN', description: 'Finished' },
{ bit: 7, name: 'ERR', description: 'Error' },
],
},
],
},
null,
2,
)
/** Example frame with valid CRC-32 (ISO HDLC) over bytes 0-10 and flags ACK|SYN. */
export const EXAMPLE_HEX = [
'FF 05 48 65 6C 6C 6F',
'00 01 02 03',
'41 42 43 44',
'E8 6B B6 E1',
'00 03',
].join('\n')
+8
View File
@@ -0,0 +1,8 @@
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import './styles/main.css'
const app = createApp(App)
app.use(createPinia())
app.mount('#app')
+62
View File
@@ -0,0 +1,62 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { useParserStore } from '@/stores/parser'
import { EXAMPLE_HEX } from '@/fixtures/example'
import type { ParseResult } from '@/types/parser'
describe('useParserStore', () => {
beforeEach(() => {
setActivePinia(createPinia())
})
it('derives bytes from hex input', () => {
const store = useParserStore()
store.hexInput = 'FF 00 AB'
expect(store.bytes).toEqual([0xFF, 0x00, 0xAB])
})
it('loads example fixture with valid byte length', () => {
const store = useParserStore()
store.hexInput = EXAMPLE_HEX
expect(store.bytes.length).toBe(21)
})
it('reports parse error when WASM is unavailable', () => {
const store = useParserStore()
store.runParse()
expect(store.parseError).toBe('WASM module not ready.')
})
it('stores parse result from WASM module', () => {
const store = useParserStore()
const result: ParseResult = {
fields: [],
total_bytes: 4,
consumed_bytes: 4,
}
store.wasmReady = true
store.wasm = {
parse: vi.fn().mockReturnValue(result),
hex_to_bytes_js: vi.fn(),
}
store.runParse()
expect(store.parseResult).toEqual(result)
expect(store.parseError).toBeNull()
})
it('captures invalid JSON schema errors', () => {
const store = useParserStore()
store.wasmReady = true
store.wasm = {
parse: vi.fn(),
hex_to_bytes_js: vi.fn(),
}
store.schemaText = '{bad json'
store.runParse()
expect(store.parseError).toMatch(/JSON schema error/)
expect(store.parseResult).toBeNull()
})
})
+100
View File
@@ -0,0 +1,100 @@
import { defineStore } from 'pinia'
import { ref, computed, shallowRef } from 'vue'
import type { ParseResult, ParsedField } from '@/types/parser'
import { parseHexInput } from '@/utils/hex'
import { validateJsonSchema } from '@/utils/schema'
import { EXAMPLE_HEX, EXAMPLE_SCHEMA } from '@/fixtures/example'
interface WasmModule {
parse: (hex: string, schema: string) => ParseResult
hex_to_bytes_js: (hex: string) => Uint8Array
}
export const useParserStore = defineStore('parser', () => {
// ── State ──────────────────────────────────────────────────────────────
const wasm = shallowRef<WasmModule | null>(null)
const wasmReady = ref(false)
const wasmError = ref<string | null>(null)
const hexInput = ref<string>(EXAMPLE_HEX)
const schemaText = ref<string>(EXAMPLE_SCHEMA)
const schemaFormat = ref<'json' | 'yaml'>('json')
const parseResult = ref<ParseResult | null>(null)
const parseError = ref<string | null>(null)
// Highlighted byte range [start, end) for hover sync.
const hoveredRange = ref<[number, number] | null>(null)
// Clicked / selected field for persistent highlight.
const selectedField = ref<ParsedField | null>(null)
// ── WASM init ──────────────────────────────────────────────────────────
async function initWasm() {
try {
// Dynamic import — wasm-pack output directory resolved at build time.
const mod = await import('../wasm/hex_pigeon_parser.js')
await mod.default() // run wasm-bindgen init
wasm.value = mod as unknown as WasmModule
wasmReady.value = true
} catch (e) {
wasmError.value = `Failed to load WASM: ${e}`
}
}
// ── Actions ────────────────────────────────────────────────────────────
function runParse() {
parseError.value = null
parseResult.value = null
if (!wasmReady.value || !wasm.value) {
parseError.value = 'WASM module not ready.'
return
}
let schemaJson = schemaText.value
if (schemaFormat.value === 'yaml') {
try {
// @ts-ignore dynamic import resolved at runtime
const yaml = (window as any).__yaml
schemaJson = JSON.stringify(yaml.load(schemaText.value))
} catch (e) {
parseError.value = `YAML parse error: ${e}`
return
}
} else {
const err = validateJsonSchema(schemaJson)
if (err) {
parseError.value = `JSON schema error: ${err}`
return
}
}
try {
const result = wasm.value.parse(hexInput.value, schemaJson)
parseResult.value = result
} catch (e) {
parseError.value = String(e)
}
}
const bytes = computed<number[]>(() => parseHexInput(hexInput.value))
function setHovered(range: [number, number] | null) {
hoveredRange.value = range
}
function setSelected(field: ParsedField | null) {
selectedField.value = field
}
return {
wasm, wasmReady, wasmError,
hexInput, schemaText, schemaFormat,
parseResult, parseError,
hoveredRange, selectedField,
bytes,
initWasm, runParse, setHovered, setSelected,
}
})
export { EXAMPLE_HEX, EXAMPLE_SCHEMA }
+227
View File
@@ -0,0 +1,227 @@
/* ── Reset & tokens ────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg0: #0d1117;
--bg1: #161b22;
--bg2: #21262d;
--bg3: #30363d;
--border: #30363d;
--text: #e6edf3;
--text-muted: #7d8590;
--accent: #58a6ff;
--accent2: #3fb950;
--warn: #f85149;
--yellow: #e3b341;
--purple: #bc8cff;
--orange: #ffa657;
--font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
--font-ui: system-ui, -apple-system, 'Segoe UI', sans-serif;
--radius: 6px;
}
html, body { height: 100%; background: var(--bg0); color: var(--text); font-family: var(--font-ui); font-size: 14px; }
/* ── App shell ──────────────────────────────────────────────────────────── */
.app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
/* ── Header ────────────────────────────────────────────────────────────── */
.app-header {
display: flex; align-items: center; justify-content: space-between;
padding: 10px 16px;
background: var(--bg1);
border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
.brand { display: flex; align-items: baseline; gap: 12px; }
.logo {
font-size: 1rem;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
color: var(--text);
}
.subtitle { color: var(--text-muted); font-size: 0.82rem; }
.header-actions { display: flex; gap: 8px; }
/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn { padding: 6px 16px; border-radius: var(--radius); border: none; cursor: pointer; font-weight: 600; transition: opacity 0.15s; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-primary:not(:disabled):hover { border-color: var(--accent); color: var(--accent); }
/* ── Banners ────────────────────────────────────────────────────────────── */
.banner { padding: 8px 16px; font-size: 0.85rem; }
.banner-error { background: #2d1a1a; color: var(--warn); border-bottom: 1px solid var(--warn); }
.banner code { background: rgba(255,255,255,0.08); padding: 0 4px; border-radius: 3px; }
/* ── Panels ─────────────────────────────────────────────────────────────── */
.panels { display: grid; grid-template-columns: 320px 1fr 340px; flex: 1; overflow: hidden; }
.panel { display: flex; flex-direction: column; overflow: hidden; border-right: 1px solid var(--border); }
.panel:last-child { border-right: none; }
.panel-header {
display: flex; align-items: center; justify-content: space-between;
padding: 6px 12px;
background: var(--bg2);
border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
.panel-title {
font-weight: 600;
font-size: 0.72rem;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--text-muted);
}
.byte-count { font-family: var(--font-mono); font-size: 0.78rem; color: var(--text-muted); }
.format-select {
background: var(--bg3); color: var(--text); border: 1px solid var(--border);
border-radius: var(--radius); padding: 2px 6px; font-size: 0.8rem;
}
/* ── Schema editor ───────────────────────────────────────────────────────── */
.schema-editor { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.schema-textarea {
flex: 1; resize: none; font-family: var(--font-mono); font-size: 0.8rem;
background: var(--bg1); color: var(--text); border: none; outline: none;
padding: 12px; line-height: 1.6; tab-size: 2;
}
.schema-error { padding: 6px 12px; background: #2d1a1a; color: var(--warn); font-size: 0.8rem; flex-shrink: 0; }
/* ── Hex input ───────────────────────────────────────────────────────────── */
.hex-input-wrapper { flex-shrink: 0; border-bottom: 1px solid var(--border); }
.hex-input {
width: 100%; resize: none; font-family: var(--font-mono); font-size: 0.85rem;
background: var(--bg2); color: var(--accent); border: none; outline: none;
padding: 10px 12px; line-height: 1.7;
}
/* ── Hex viewer ──────────────────────────────────────────────────────────── */
.hex-viewer { flex: 1; overflow-y: auto; padding: 8px 0; }
.hex-grid {
display: grid;
grid-template-columns: 50px 1fr auto;
gap: 0;
font-family: var(--font-mono);
font-size: 0.8rem;
line-height: 1.9;
}
.hex-offset { color: var(--text-muted); padding: 0 8px; user-select: none; }
.hex-bytes { display: flex; flex-wrap: wrap; gap: 2px; padding: 0 4px; }
.hex-byte {
display: inline-block;
width: 22px; text-align: center;
cursor: pointer;
border-radius: 3px;
transition: background 0.08s;
padding: 0 1px;
}
.hex-pad { color: transparent; }
.hex-ascii { color: var(--text-muted); padding: 0 8px; user-select: none; letter-spacing: 0.05em; }
.ascii-char { cursor: pointer; border-radius: 2px; }
/* ── Byte states ─────────────────────────────────────────────────────────── */
.byte-parsed { color: var(--text); }
.byte-hovered { background: rgba(88,166,255,0.25); color: #fff; }
.byte-selected { background: var(--accent); color: #000 !important; border-radius: 3px; }
.byte-error { color: var(--warn); }
.byte-ok { color: var(--accent2); }
/* ── Hex legend ─────────────────────────────────────────────────────────── */
.hex-legend {
display: flex; align-items: center; gap: 6px;
padding: 6px 12px; font-size: 0.78rem; color: var(--text-muted);
border-top: 1px solid var(--border); flex-shrink: 0;
}
.legend-dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.legend-dot.selected { background: var(--accent); }
/* ── Parsed tree ────────────────────────────────────────────────────────── */
.parsed-tree { flex: 1; overflow-y: auto; }
.parsed-node {
display: flex; align-items: center; gap: 6px;
padding: 3px 8px; cursor: pointer;
border-left: 2px solid transparent;
transition: background 0.08s;
font-size: 0.82rem;
}
.parsed-node:hover { background: var(--bg2); }
.node-selected { background: rgba(88,166,255,0.12) !important; border-left-color: var(--accent); }
.node-hovered { background: rgba(88,166,255,0.07); }
.node-error { border-left-color: var(--warn); }
.node-ok { border-left-color: var(--accent2); }
.expand-btn {
background: none; border: none; cursor: pointer;
width: 14px; height: 14px; flex-shrink: 0;
display: inline-flex; align-items: center; justify-content: center;
}
.expand-icon {
display: block;
width: 0; height: 0;
border-top: 4px solid transparent;
border-bottom: 4px solid transparent;
border-left: 6px solid var(--text-muted);
transition: transform 0.12s ease;
}
.expand-icon.expanded { transform: rotate(90deg); }
.expand-spacer { display: inline-block; width: 14px; }
.node-kind {
font-size: 0.68rem; font-family: var(--font-mono);
background: var(--bg3); color: var(--purple);
border-radius: 3px; padding: 1px 5px;
white-space: nowrap; flex-shrink: 0;
}
.node-name { font-weight: 600; color: var(--text); flex-shrink: 0; }
.node-value { flex: 1; font-family: var(--font-mono); font-size: 0.78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-meta { color: var(--text-muted); font-family: var(--font-mono); font-size: 0.7rem; flex-shrink: 0; }
.val-int { color: var(--orange); }
.val-float { color: var(--yellow); }
.val-string { color: var(--accent2); }
.val-crc { color: var(--purple); }
.node-desc {
font-size: 0.78rem; color: var(--text-muted);
padding: 2px 8px 4px;
font-style: italic;
}
/* ── Flag chips ─────────────────────────────────────────────────────────── */
.flag-chip {
display: inline-block; font-size: 0.65rem; font-family: var(--font-mono);
padding: 1px 5px; border-radius: 3px; margin-right: 3px; font-weight: 700;
}
.flag-set { background: rgba(63,185,80,0.25); color: var(--accent2); }
.flag-unset { background: var(--bg3); color: var(--text-muted); }
.bytes-preview { color: var(--accent); font-size: 0.72rem; }
/* ── Badges ─────────────────────────────────────────────────────────────── */
.badge { font-size: 0.65rem; font-weight: 700; padding: 1px 6px; border-radius: 10px; flex-shrink: 0; }
.badge-ok { background: rgba(63,185,80,0.2); color: var(--accent2); }
.badge-err { background: rgba(248,81,73,0.2); color: var(--warn); }
/* ── Empty hints ────────────────────────────────────────────────────────── */
.empty-hint { padding: 24px 16px; color: var(--text-muted); text-align: center; }
.parse-error { padding: 10px 14px; background: #2d1a1a; color: var(--warn); font-size: 0.82rem; border-bottom: 1px solid var(--warn); }
/* ── Scrollbars ─────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg0); }
::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
+70
View File
@@ -0,0 +1,70 @@
// ── Schema types (mirrors Rust schema.rs) ──────────────────────────────────
export type Endian = 'big' | 'little'
export type FieldKind =
| 'uint8' | 'uint16' | 'uint32' | 'uint64'
| 'int8' | 'int16' | 'int32'
| 'float32' | 'float64'
| 'bitflags8' | 'bitflags16'
| 'bytes' | 'string_fixed' | 'string_lp8' | 'string_lp16'
| 'crc16' | 'crc32'
| 'repeated' | 'padding'
export interface FlagDef {
bit: number
name: string
description?: string
}
export type CountSpec = number | string
export interface FieldDef {
name: string
type: FieldKind
endian?: Endian
description?: string
flags?: FlagDef[]
length?: number
checksum_range?: [number, number]
count?: CountSpec
fields?: FieldDef[]
}
export interface Schema {
name?: string
endian?: Endian
fields: FieldDef[]
}
// ── ParseResult types (mirrors Rust parser.rs) ─────────────────────────────
export type ParsedValue =
| number
| string
| number[] // bytes
| FlagValue[]
| null
export interface FlagValue {
bit: number
name: string
set: boolean
}
export interface ParsedField {
name: string
kind: string
offset: number
size: number
value: ParsedValue
description?: string
children?: ParsedField[]
checksum_valid?: boolean
}
export interface ParseResult {
fields: ParsedField[]
total_bytes: number
consumed_bytes: number
}
+26
View File
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest'
import { parseHexInput, formatHexBytes } from '@/utils/hex'
describe('parseHexInput', () => {
it('parses spaced hex pairs', () => {
expect(parseHexInput('FF 05 48 65')).toEqual([0xFF, 0x05, 0x48, 0x65])
})
it('ignores newlines and tabs', () => {
expect(parseHexInput('FF\n05\t48')).toEqual([0xFF, 0x05, 0x48])
})
it('returns empty array for odd-length input', () => {
expect(parseHexInput('ABC')).toEqual([])
})
it('returns empty array for invalid digits', () => {
expect(parseHexInput('GG')).toEqual([])
})
})
describe('formatHexBytes', () => {
it('formats bytes as uppercase hex', () => {
expect(formatHexBytes([0x0a, 0xff])).toBe('0A FF')
})
})
+17
View File
@@ -0,0 +1,17 @@
/** Parse a hex dump string into byte values. Whitespace is ignored. */
export function parseHexInput(hex: string): number[] {
const clean = hex.replace(/\s/g, '')
if (clean.length % 2 !== 0) return []
const out: number[] = []
for (let i = 0; i < clean.length; i += 2) {
const b = parseInt(clean.slice(i, i + 2), 16)
if (Number.isNaN(b)) return []
out.push(b)
}
return out
}
/** Format bytes as uppercase hex pairs separated by spaces. */
export function formatHexBytes(bytes: number[]): string {
return bytes.map(b => b.toString(16).padStart(2, '0').toUpperCase()).join(' ')
}
+13
View File
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest'
import { validateJsonSchema } from '@/utils/schema'
describe('validateJsonSchema', () => {
it('accepts valid JSON', () => {
expect(validateJsonSchema('{"fields":[]}')).toBeNull()
})
it('rejects invalid JSON', () => {
const err = validateJsonSchema('{invalid')
expect(err).not.toBeNull()
})
})
+9
View File
@@ -0,0 +1,9 @@
/** Validate JSON schema text. Returns an error message or null if valid. */
export function validateJsonSchema(text: string): string | null {
try {
JSON.parse(text)
return null
} catch (e) {
return String(e)
}
}