main
Matthew Butterick 6 years ago
parent 4c50c422a1
commit ecb3e4a1cf

@ -13,7 +13,8 @@
(define private-keys '(parent _startOffset _currentOffset _length))
(define (dict->mutable-hash x)
(define h (make-hasheq))
(for ([(k v) (in-dict x)])
(for ([(k v) (in-dict x)]
#:unless (memq k private-keys))
(hash-set! h k v))
h)

@ -73,7 +73,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(raise-result-error 'xstruct-decode "dict" res))
res))
(define/finalize-size (xstruct-size xs [val #f] #:parent [parent-arg #f] [include-pointers #t])
(define/finalize-size (xstruct-size xs [val #f] #:parent [parent-arg #f]
#:include-pointers [include-pointers #t])
(define parent (mhasheq 'parent parent-arg
'val val
'pointerSize 0))
@ -95,7 +96,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
val))
(unless (andmap (λ (key) (memq key (d:dict-keys val))) (d:dict-keys (xstruct-fields xs)))
(raise-argument-error 'xstruct-encode
(format "dict that contains superset of Struct keys: ~a" (d:dict-keys (xstruct-fields xs))) (d:dict-keys val)))
(format "dict that contains superset of xstruct keys: ~a" (d:dict-keys (xstruct-fields xs))) (d:dict-keys val)))
(define parent (mhash 'pointers empty
'startOffset (pos port)
@ -104,7 +105,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
'pointerSize 0))
; deliberately use `xstruct-size` instead of `size` to use extra arg
(d:dict-set! parent 'pointerOffset (+ (pos port) (xstruct-size xs val #:parent parent #f)))
(d:dict-set! parent 'pointerOffset
(+ (pos port) (xstruct-size xs val #:parent parent #:include-pointers #f)))
(for ([(key type) (d:in-dict (xstruct-fields xs))])
(encode type (d:dict-ref val key) #:parent parent))
@ -123,7 +125,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define (+xstruct . dicts)
(define args (flatten dicts))
(unless (even? (length args))
(raise-argument-error '+xstruct "equal keys and values" dicts))
(raise-argument-error '+xstruct "equal number of keys and values" dicts))
(define fields (for/list ([kv (in-slice 2 args)])
(unless (symbol? (car kv))
(raise-argument-error '+xstruct "symbol" (car kv)))

Loading…
Cancel
Save