#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 stringstring string