better error msgs

main
Matthew Butterick 6 years ago
parent 31cb060e02
commit 307ac0b792

@ -48,17 +48,19 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(send @type x:decode port new-parent))]
;; we have len, which is treated as count of items
[else (for/list ([i (in-range len)])
(when (eof-object? (peek-byte port))
(raise-argument-error 'decode (format "bytes for ~a items" len) i))
(send @type x:decode port new-parent))]))
(define/augride (x:encode val-arg port [parent #f])
(unless (sequence? val-arg)
(raise-argument-error 'x:list-encode "sequence" val-arg))
(raise-argument-error 'encode "sequence" val-arg))
(define vals (if (list? val-arg) val-arg (sequence->list val-arg)))
;; if @len is not an integer, we have variable length
(define maybe-fixed-len (and (integer? @len) @len))
(when maybe-fixed-len
(unless (= (length vals) maybe-fixed-len)
(raise-argument-error 'x:list-encode "vals equal to length" maybe-fixed-len)))
(unless (eq? (length vals) maybe-fixed-len)
(raise-argument-error 'encode (format "sequence of ~a values" maybe-fixed-len) (length vals))))
(define (encode-items parent)
(for ([item (in-list vals)]
[idx (in-range (or maybe-fixed-len +inf.0))])

@ -1,6 +1,8 @@
#lang debug racket/base
(require racket/class
racket/contract
racket/match
racket/sequence
"base.rkt"
"number.rkt"
"util.rkt"
@ -13,8 +15,8 @@
(super-new)
(define/override (pre-encode val)
(unless (or (vector? val) (list? val))
(raise-argument-error 'x:vector-encode "vector or list" val))
(unless (or (vector? val) (sequence? val))
(raise-argument-error 'encode "vector or sequence" val))
(if (vector? val) (vector->list val) val))
(define/override (post-decode val) (list->vector val))))

Loading…
Cancel
Save