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/pitfall/restructure/struct.rkt

160 lines
6.7 KiB
Racket

8 years ago
#lang restructure/racket
8 years ago
(require racket/dict "stream.rkt")
8 years ago
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|#
8 years ago
(define-subclass Streamcoder (Struct [assocs (dictify)])
(unless (assocs? assocs)
(raise-argument-error 'Struct "assocs" assocs))
(field [key-index #f] ; store the original key order
8 years ago
[fields (mhash)])
(define/private (update-key-index! assocs)
(unless (assocs? assocs)
(raise-argument-error 'Struct "assocs" assocs))
(set! key-index (map car assocs)))
(update-key-index! assocs)
(define/public-final (update-fields! assocs)
(unless (assocs? assocs)
(raise-argument-error 'Struct "assocs or hash" assocs))
(update-key-index! assocs)
(for ([(k v) (in-dict assocs)])
8 years ago
(hash-set! fields k v)))
(update-fields! assocs)
8 years ago
(define/augride (decode stream [parent #f] [length 0])
8 years ago
(define res (_setup stream parent length))
8 years ago
(_parseFields stream res fields)
#;(hash-set! (hash-ref res '_props) '_currentOffset (· stream pos))
8 years ago
(send this process res stream)
8 years ago
res)
8 years ago
8 years ago
(define/augment (encode stream input-hash [parent #f])
8 years ago
(unless (hash? input-hash)
(raise-argument-error 'Struct:encode "hash" input-hash))
(send this preEncode input-hash stream) ; preEncode goes first, because it might bring input hash into compliance
8 years ago
(inner (void) encode stream input-hash parent)
8 years ago
(unless (andmap (λ (key) (member key (hash-keys input-hash))) key-index)
(raise-argument-error 'Struct:encode (format "hash that contains superset of Struct keys: ~a" key-index) (hash-keys input-hash)))
8 years ago
(for* ([key (in-list key-index)] ; iterate over original keys in order
8 years ago
[struct-type (in-value (hash-ref fields key))]
8 years ago
[value-to-encode (in-value (hash-ref input-hash key))])
8 years ago
(send struct-type encode stream value-to-encode)))
8 years ago
(define/public-final (_setup stream parent length)
8 years ago
(define res (mhasheq))
8 years ago
8 years ago
;; define hidden properties
#;(hash-set! res '_props
8 years ago
(mhasheq 'parent (mhasheq 'value parent)
'_startOffset (mhasheq 'value (· stream pos))
'_currentOffset (mhasheq 'value 0 'writable #t)
'_length (mhasheq 'value length)))
8 years ago
res)
8 years ago
(define/public-final (_parseFields stream res fields)
8 years ago
(for ([key (in-list key-index)])
8 years ago
(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)])
8 years ago
(send type size (hash-ref val key #f)))))
8 years ago
8 years ago
(test-module
(require "number.rkt")
(define (random-pick xs) (list-ref xs (random (length xs))))
(check-exn exn:fail:contract? (λ () (+Struct 42)))
;; make random structs and make sure we can round trip
(for ([i (in-range 100)])
8 years ago
(define field-types (for/list ([i (in-range 200)])
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
(define size-num-types (for/sum ([num-type (in-list field-types)])
(send num-type size)))
(define s (+Struct (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))))
(define es (+EncodeStream))
(send s encode es (send s decode bs))
(check-equal? (send es dump) bs)))
8 years ago
8 years ago
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|#
8 years ago
(define-subclass Struct (VersionedStruct version-resolver [versions (dictify)])
(unless ((disjoin integer? procedure? RestructureBase?) version-resolver)
(raise-argument-error 'VersionedStruct "integer, function, or Restructure object" version-resolver))
(unless (and (dict? versions) (andmap dict? (map cdr versions)))
(raise-argument-error 'VersionedStruct "dict of dicts" versions))
8 years ago
(inherit-field fields key-index)
(field [forced-version #f])
8 years ago
(define/public-final (force-version! version)
8 years ago
(set! forced-version version))
(define/override (decode stream [parent #f] [length 0])
8 years ago
(define res (send this _setup stream parent length))
(define version (cond
8 years ago
[forced-version] ; for testing purposes: pass an explicit version
8 years ago
[(integer? version-resolver) version-resolver]
[(procedure? version-resolver) (version-resolver parent)]
[(RestructureBase? version-resolver) (send version-resolver decode stream)]
[else (raise-argument-error 'VersionedStruct:decode "way of finding version" version-resolver)]))
8 years ago
(hash-set! res 'version version)
(define assocs (dict-ref versions version (λ () (raise-argument-error 'VersionedStruct:decode "valid version key" version))))
(send this update-fields! assocs)
8 years ago
(cond
8 years ago
[(VersionedStruct? fields) (send fields decode stream parent)]
8 years ago
[else
8 years ago
(send this _parseFields stream res fields)
8 years ago
(send this process res stream)
8 years ago
res]))
(define/augment (encode stream input-hash [parent #f])
(define assocs (dict-ref versions (· input-hash version) (λ () (raise-argument-error 'VersionedStruct:encode "valid version key" version))))
(send this update-fields! assocs)))
8 years ago
(test-module
(require "number.rkt")
(check-exn exn:fail:contract? (λ () (+VersionedStruct 42 42)))
;; make random versioned structs and make sure we can round trip
(for ([i (in-range 20)])
8 years ago
(define field-types (for/list ([i (in-range 200)])
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
(define num-versions 20)
(define which-struct (random num-versions))
(define struct-versions (for/list ([v (in-range num-versions)])
(cons v (for/list ([num-type (in-list field-types)])
(cons (gensym) num-type)))))
(define vs (+VersionedStruct which-struct struct-versions))
(define struct-size (for/sum ([num-type (in-list (map cdr (dict-ref struct-versions which-struct)))])
(send num-type size)))
(define bs (apply bytes (for/list ([i (in-range struct-size)])
(random 256))))
(check-equal? (send vs encode #f (send vs decode bs)) bs)))