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.
93 lines
3.6 KiB
Racket
93 lines
3.6 KiB
Racket
#lang restructure/racket
|
|
(require racket/dict "stream.rkt")
|
|
(provide (all-defined-out))
|
|
|
|
#|
|
|
approximates
|
|
https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|
|
|#
|
|
|
|
(define-subclass Streamcoder (Struct [assocs (dictify)])
|
|
(field [key-index (map car assocs)] ; store the original key order
|
|
[fields (mhash)])
|
|
(for ([(k v) (in-dict assocs)])
|
|
(hash-set! fields k v))
|
|
|
|
(define/augride (decode stream [parent #f] [length 0])
|
|
(define res (_setup stream parent length))
|
|
(_parseFields stream res fields)
|
|
#;(hash-set! (hash-ref res '_props) '_currentOffset (· stream pos))
|
|
(send this process res stream)
|
|
res)
|
|
|
|
(define/augride (encode stream input-hash [parent #f])
|
|
(unless (hash? input-hash)
|
|
(raise-argument-error 'Struct:encode "hash" input-hash))
|
|
(define sorted-input-keys (sort (hash-keys input-hash) #:key symbol->string string<?))
|
|
(define sorted-struct-keys (sort key-index #:key symbol->string string<?))
|
|
(unless (equal? sorted-input-keys sorted-struct-keys)
|
|
(raise-argument-error 'Struct:encode (format "hash with same keys as Struct: ~a" sorted-struct-keys) sorted-input-keys))
|
|
|
|
(send this preEncode input-hash stream)
|
|
(for* ([key (in-list key-index)] ; iterate over original keys in order
|
|
[struct-type (in-value (hash-ref fields key))]
|
|
[value-to-encode (in-value (hash-ref input-hash key))])
|
|
(send struct-type encode stream value-to-encode)))
|
|
|
|
(define/public-final (_setup stream parent length)
|
|
(define res (mhasheq))
|
|
|
|
;; define hidden properties
|
|
#;(hash-set! res '_props
|
|
(mhasheq 'parent (mhasheq 'value parent)
|
|
'_startOffset (mhasheq 'value (· stream pos))
|
|
'_currentOffset (mhasheq 'value 0 'writable #t)
|
|
'_length (mhasheq 'value length)))
|
|
res)
|
|
|
|
(define/public-final (_parseFields stream res fields)
|
|
(for ([key (in-list key-index)])
|
|
(define dictvalue (dict-ref fields key))
|
|
(define val
|
|
(if (procedure? dictvalue)
|
|
(dictvalue res)
|
|
(send dictvalue decode stream res)))
|
|
(hash-set! res key val)))
|
|
|
|
(define/override (size [val (mhash)] [parent #f] [includePointers #t])
|
|
(for/sum ([(key type) (in-hash fields)]
|
|
#:when (hash-has-key? val key))
|
|
(send type size (hash-ref val key)))))
|
|
|
|
|
|
|
|
#|
|
|
approximates
|
|
https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|#
|
|
|
|
(define-subclass Struct (VersionedStruct type [versions (dictify)])
|
|
(inherit-field fields key-index)
|
|
(field [forced-version #f])
|
|
|
|
(define/public (force-version! version)
|
|
(set! forced-version version))
|
|
|
|
(define/override (decode stream [parent #f] [length 0])
|
|
(define res (send this _setup stream parent length))
|
|
(define version (cond
|
|
[forced-version] ; for testing purposes: pass an explicit version
|
|
[(procedure? type) (type parent)]
|
|
[(RestructureBase? type) (send type decode stream)]
|
|
[else (raise-argument-error 'decode "way of finding version" type)]))
|
|
(hash-set! res 'version version)
|
|
(set-field! fields this (dict-ref versions version (λ () (raise-argument-error 'RVersionedStruct:decode "valid version key" version))))
|
|
(set-field! key-index this (map car (· this fields)))
|
|
(cond
|
|
[(VersionedStruct? (· this fields)) (send (· this fields) decode stream parent)]
|
|
[else
|
|
(send this _parseFields stream res (· this fields))
|
|
(send this process res stream)
|
|
res]))
|
|
)
|