You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/xenomorph/xenomorph/dict.rkt

128 lines
5.0 KiB
Racket

5 years ago
#lang debug racket/base
6 years ago
(require racket/dict
racket/class
6 years ago
racket/sequence
6 years ago
racket/match
6 years ago
racket/list
5 years ago
racket/contract
"base.rkt"
6 years ago
"number.rkt"
sugar/unstable/dict)
(provide (all-defined-out))
6 years ago
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|#
6 years ago
(define (setup-private-fields port parent len)
(define mheq (make-hasheq))
6 years ago
(dict-set*! mheq
x:parent-key parent
x:start-offset-key (pos port)
x:current-offset-key 0
x:length-key len)
mheq)
6 years ago
6 years ago
(define (parse-fields port mheq fields-arg)
5 years ago
(define fields (if (x:dict? fields-arg) (get-field fields fields-arg) fields-arg))
6 years ago
(unless (assocs? fields)
5 years ago
(raise-argument-error 'x:dict-parse-fields "assocs" fields))
6 years ago
(for ([(key type) (in-dict fields)])
(define val (match type
[(? procedure? proc) (proc mheq)]
[_ (send type x:decode port mheq)]))
(unless (void? val)
(hash-set! mheq key val))
(hash-set! mheq x:current-offset-key (- (pos port) (hash-ref mheq x:start-offset-key))))
6 years ago
mheq)
6 years ago
5 years ago
(define x:dict%
(class x:base%
6 years ago
(super-new)
(init-field [(@fields fields)])
6 years ago
(when @fields (unless (dict? @fields)
(raise-argument-error '+xstruct "dict" @fields)))
6 years ago
5 years ago
(define/augride (x:decode port parent [len 0])
6 years ago
(define res (setup-private-fields port parent len))
6 years ago
(parse-fields port res @fields))
6 years ago
(define/override (post-decode val)
(dict->mutable-hash val))
6 years ago
5 years ago
(define/augride (x:encode field-data port [parent-arg #f])
6 years ago
(unless (dict? field-data)
5 years ago
(raise-result-error 'x:dict-encode "dict" field-data))
6 years ago
;; check keys, because `size` also relies on keys being valid
6 years ago
(unless (andmap (λ (field-key) (memq field-key (dict-keys field-data))) (dict-keys @fields))
5 years ago
(raise-argument-error 'x:dict-encode
6 years ago
(format "dict that contains superset of xstruct keys: ~a"
6 years ago
(dict-keys @fields)) (dict-keys field-data)))
6 years ago
(define parent (mhasheq x:pointers-key null
x:start-offset-key (pos port)
x:parent-key parent-arg
x:val-key field-data
x:pointer-size-key 0))
5 years ago
(hash-set! parent x:pointer-offset-key (+ (pos port) (x:size field-data parent #f)))
6 years ago
(for ([(key type) (in-dict @fields)])
(send type x:encode (dict-ref field-data key) port parent))
6 years ago
(for ([ptr (in-list (hash-ref parent x:pointers-key))])
(match ptr
[(x:ptr type val parent) (send type x:encode val port parent)])))
6 years ago
5 years ago
(define/augride (x:size [val #f] [parent-arg #f] [include-pointers #t])
(define parent (mhasheq x:parent-key parent-arg
x:val-key val
x:pointer-size-key 0))
(define fields-size (for/sum ([(key type) (in-dict @fields)]
#:when (xenomorphic-type? type))
(send type x:size (and val (send type pre-encode (dict-ref val key))) parent)))
(define pointers-size (if include-pointers (dict-ref parent x:pointer-size-key) 0))
6 years ago
(+ fields-size pointers-size))))
6 years ago
5 years ago
(define (x:dict? x) (is-a? x x:dict%))
6 years ago
5 years ago
(define/contract (x:dict #:pre-encode [pre-proc #f]
#:post-decode [post-proc #f]
#:base-class [base-class x:dict%]
. dicts)
5 years ago
(()
(#:pre-encode (or/c (any/c . -> . any/c) #false)
#:post-decode (or/c (any/c . -> . any/c) #false)
5 years ago
#:base-class (λ (c) (subclass? c x:dict%)))
#:rest (listof any/c)
5 years ago
. ->* .
5 years ago
x:dict?)
6 years ago
(define args (flatten dicts))
(unless (even? (length args))
5 years ago
(raise-argument-error 'x:dict "equal number of keys and values" dicts))
6 years ago
(define fields (for/list ([kv (in-slice 2 args)])
(unless (symbol? (car kv))
(raise-argument-error '+xstruct "symbol" (car kv)))
(apply cons kv)))
(new (generate-subclass base-class pre-proc post-proc) [fields fields]))
6 years ago
(module+ test
(require rackunit "number.rkt" "base.rkt")
6 years ago
(define (random-pick xs) (list-ref xs (random (length xs))))
5 years ago
(check-exn exn:fail:contract? (λ () (x:dict 42)))
6 years ago
(for ([i (in-range 20)])
;; make random structs and make sure we can round trip
(define field-types
(for/list ([i (in-range 40)])
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
(define size-num-types
(for/sum ([num-type (in-list field-types)])
(send num-type x:size)))
(define xs (x:dict (for/list ([num-type (in-list field-types)])
(cons (gensym) num-type))))
(define bs (apply bytes (for/list ([i (in-range size-num-types)])
(random 256))))
(check-equal? (encode xs (decode xs bs) #f) bs)))
5 years ago
;; bw compat
(define x:struct% x:dict%)
(define x:struct? x:dict?)
(define x:struct x:dict)