~ehmry/svui

0b1058dd7ab472d2d806e1bf0b400fff57b68921 — Emery Hemingway 3 years ago 03e7cb2
Attrs are patternable

With syndicate-nim f710419ead4c756261fdb67526936721a1c70a26.
4 files changed, 20 insertions(+), 7 deletions(-)

M .gitignore
M src/svui.nim
M svui.prs
A tests/test_patterns.nim
M .gitignore => .gitignore +1 -0
@@ 1,1 1,2 @@
.tup
test_patterns

M src/svui.nim => src/svui.nim +7 -6
@@ 1,13 1,14 @@

import
  std/typetraits, preserves
  std/typetraits, preserves, std/tables

type
  Svui* {.preservesRecord: "svui".} = object
  Svui*[E] {.preservesRecord: "svui".} = ref object
    `attrs`*: Table[string, Preserve[E]]
    `svg`*: string

proc `$`*(x: Svui): string =
  `$`(toPreserve(x))
proc `$`*[E](x: Svui[E]): string =
  `$`(toPreserve(x, E))

proc encode*(x: Svui): seq[byte] =
  encode(toPreserve(x))
proc encode*[E](x: Svui[E]): seq[byte] =
  encode(toPreserve(x, E))

M svui.prs => svui.prs +1 -1
@@ 1,3 1,3 @@
version 1.

Svui = <svui @svg string>.
Svui = <svui @attrs {symbol: any ...:...} @svg string>.

A tests/test_patterns.nim => tests/test_patterns.nim +11 -0
@@ 0,0 1,11 @@
import std/[typetraits, unittest]
import preserves
import syndicate/patterns
import svui

type Svui = svui.Svui[void]

suite "patterns":
  test "grab-all":
    let pat = Svui ? {0: grab(), 1: grab()}
    check $pat == "<compound <rec svui 2> {0: <bind <_>>, 1: <bind <_>>}>"