portify pt 1

main
Matthew Butterick 8 years ago
parent 58b7d10c53
commit d398398c2e

@ -3,16 +3,16 @@
(provide (all-defined-out))
(define-generics codable
(decode codable stream [parent])
(encode codable stream [val] [parent]))
(decode codable #:parent [parent] [stream])
(encode codable [val] [stream] #:parent [parent]))
(define codable<%>
(interface* ()
([(generic-property gen:codable)
(generic-method-table gen:codable
(define (decode o stream [parent #f]) (send o decode stream parent))
(define (encode o stream [val #f] [parent #f]) (send o encode stream val parent)))])))
(define (decode o [stream (current-input-port)] #:parent [parent #f]) (send o decode stream parent))
(define (encode o [val #f] [stream (current-output-port)] #:parent [parent #f]) (send o encode stream val parent)))])))
(define-generics sizable

@ -202,9 +202,12 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(define/overment (decode x [parent #f])
(when parent (unless (indexable? parent)
(raise-argument-error 'Streamcoder:decode "hash or indexable" x)))
(define stream (if (bytes? x) (+DecodeStream x) x))
(define stream (cond
[(bytes? x) (+DecodeStream x)]
[(input-port? x) (+DecodeStream (port->bytes x))]
[else x]))
(unless (DecodeStream? stream)
(raise-argument-error 'Streamcoder:decode "bytes or DecodeStream" x))
(raise-argument-error 'Streamcoder:decode "bytes or input port or DecodeStream" x))
(inner (void) decode stream parent))
(define/overment (encode x [val #f] [parent #f])
@ -228,4 +231,4 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(check-exn exn:fail:contract? (λ () (decode d 42)))
(check-not-exn (λ () (decode d #"foo")))
(check-exn exn:fail:contract? (λ () (encode d 42 21)))
(check-not-exn (λ () (encode d (open-output-bytes) 42))))
(check-not-exn (λ () (encode d 42 (open-output-bytes) ))))

@ -13,18 +13,17 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
; array = new ArrayT uint8, 4
; array.decode(stream).should.deep.equal [1, 2, 3, 4]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint8 4)])
(check-equal? (decode array stream) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8 4)) '(1 2 3 4)))
; it 'should decode fixed amount of bytes', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new ArrayT uint16, 4, 'bytes'
; array.decode(stream).should.deep.equal [258, 772]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint16be 4 'bytes)])
(check-equal? (decode array stream) '(258 772)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint16be 4 'bytes)) '(258 772)))
; it 'should decode length from parent key', ->
@ -32,100 +31,101 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
; array = new ArrayT uint8, 'len'
; array.decode(stream, len: 4).should.deep.equal [1, 2, 3, 4]
;
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint8 4 'len)])
(check-equal? (decode array stream (mhash 'len 4)) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8 4 'len) #:parent (mhash 'len 4)) '(1 2 3 4)))
; it 'should decode amount of bytes from parent key', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new ArrayT uint16, 'len', 'bytes'
; array.decode(stream, len: 4).should.deep.equal [258, 772]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint16be 'len 'bytes)])
(check-equal? (decode array stream (mhash 'len 4)) '(258 772)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint16be 'len 'bytes) #:parent (mhash 'len 4)) '(258 772)))
; it 'should decode length as number before array', ->
; stream = new DecodeStream new Buffer [4, 1, 2, 3, 4, 5]
; array = new ArrayT uint8, uint8
; array.decode(stream).should.deep.equal [1, 2, 3, 4]
(let ([stream (+DecodeStream (+Buffer '(4 1 2 3 4 5)))]
[array (+ArrayT uint8 uint8)])
(check-equal? (decode array stream) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8 uint8)) '(1 2 3 4)))
; it 'should decode amount of bytes as number before array', ->
; stream = new DecodeStream new Buffer [4, 1, 2, 3, 4, 5]
; array = new ArrayT uint16, uint8, 'bytes'
; array.decode(stream).should.deep.equal [258, 772]
(let ([stream (+DecodeStream (+Buffer '(4 1 2 3 4 5)))]
[array (+ArrayT uint16be uint8 'bytes)])
(check-equal? (decode array stream) '(258 772)))
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint16be uint8 'bytes)) '(258 772)))
; it 'should decode length from function', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new ArrayT uint8, -> 4
; array.decode(stream).should.deep.equal [1, 2, 3, 4]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint8 (λ _ 4))])
(check-equal? (decode array stream) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8 (λ _ 4))) '(1 2 3 4)))
; it 'should decode amount of bytes from function', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new ArrayT uint16, (-> 4), 'bytes'
; array.decode(stream).should.deep.equal [258, 772]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint16be (λ _ 4) 'bytes)])
(check-equal? (decode array stream) '(258 772)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint16be (λ _ 4) 'bytes)) '(258 772)))
; it 'should decode to the end of the parent if no length is given', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new ArrayT uint8
; array.decode(stream, _length: 4, _startOffset: 0).should.deep.equal [1, 2, 3, 4]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint8)])
(check-equal? (decode array stream (mhash '_length 4 '_startOffset 0)) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8) #:parent (mhash '_length 4 '_startOffset 0)) '(1 2 3 4)))
; decode to the end of the stream if parent exists, but its length is 0
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+ArrayT uint8)])
(check-equal? (decode array stream (mhash '_length 0 '_startOffset 0)) '(1 2 3 4 5)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+ArrayT uint8) #:parent (mhash '_length 0 '_startOffset 0)) '(1 2 3 4 5)))
; it 'should decode to the end of the stream if no parent and length is given', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4]
; array = new ArrayT uint8
; array.decode(stream).should.deep.equal [1, 2, 3, 4]
(let ([stream (+DecodeStream (+Buffer '(1 2 3 4)))]
[array (+ArrayT uint8)])
(check-equal? (decode array stream) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4))])
(check-equal? (decode (+ArrayT uint8)) '(1 2 3 4 )))
; describe 'size', ->
; it 'should use array length', ->
; array = new ArrayT uint8, 10
; array.size([1, 2, 3, 4]).should.equal 4
(let ([array (+ArrayT uint8 10)])
(check-equal? (size array '(1 2 3 4)) 4))
(check-equal? (size (+ArrayT uint8 10) '(1 2 3 4)) 4)
; it 'should add size of length field before string', ->
; array = new ArrayT uint8, uint8
; array.size([1, 2, 3, 4]).should.equal 5
;
(let ([array (+ArrayT uint8 uint8)])
(check-equal? (size array '(1 2 3 4)) 5))
(check-equal? (size (+ArrayT uint8 uint8) '(1 2 3 4)) 5)
; it 'should use defined length if no value given', ->
; array = new ArrayT uint8, 10
; array.size().should.equal 10
(let ([array (+ArrayT uint8 10)])
(check-equal? (size array) 10))
(check-equal? (size (+ArrayT uint8 10)) 10)
; describe 'encode', ->
@ -139,9 +139,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
; array.encode(stream, [1, 2, 3, 4])
; stream.end()
(let ([stream (+EncodeStream)]
[array (+ArrayT uint8 10)])
(check-equal? (encode array #f '(1 2 3 4)) (+Buffer '(1 2 3 4))))
(check-equal? (encode (+ArrayT uint8 10) '(1 2 3 4) #f) (bytes 1 2 3 4))
;
@ -155,9 +153,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
; array.encode(stream, [1, 2, 3, 4])
; stream.end()
(let ([stream (+EncodeStream)]
[array (+ArrayT uint8 uint8)])
(check-equal? (encode array #f '(1 2 3 4)) (+Buffer '(4 1 2 3 4))))
(check-equal? (encode (+ArrayT uint8 uint8) '(1 2 3 4) #f) (bytes 4 1 2 3 4))
; it 'should add pointers after array if length is encoded at start', (done) ->
@ -170,6 +166,4 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
; array.encode(stream, [1, 2, 3, 4])
; stream.end()
(let ([stream (+EncodeStream)]
[array (+ArrayT (+Pointer uint8 uint8) uint8)])
(check-equal? (encode array #f '(1 2 3 4)) (+Buffer '(4 5 6 7 8 1 2 3 4))))
(check-equal? (encode (+ArrayT (+Pointer uint8 uint8) uint8) '(1 2 3 4) #f) (bytes 4 5 6 7 8 1 2 3 4))
Loading…
Cancel
Save