|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
|
|
|
|
|
#lang debug racket/base
|
|
|
|
|
(require "helper.rkt")
|
|
|
|
|
(require "helper.rkt" racket/class)
|
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
@ -8,54 +7,42 @@ approximates
|
|
|
|
|
https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
(define (resolve-condition xo parent)
|
|
|
|
|
(define maybe-proc (xoptional-condition xo))
|
|
|
|
|
(if (procedure? maybe-proc)
|
|
|
|
|
(maybe-proc parent)
|
|
|
|
|
maybe-proc))
|
|
|
|
|
|
|
|
|
|
(define/post-decode (xoptional-decode xo [port-arg (current-input-port)] #:parent [parent #f])
|
|
|
|
|
(define port (->input-port port-arg))
|
|
|
|
|
(parameterize ([current-input-port port])
|
|
|
|
|
(when (resolve-condition xo parent)
|
|
|
|
|
(xdecode (xoptional-type xo) #:parent parent))))
|
|
|
|
|
|
|
|
|
|
(define/pre-encode (xoptional-encode xo val [port-arg (current-output-port)] #:parent [parent #f])
|
|
|
|
|
(define port (if (output-port? port-arg) port-arg (open-output-bytes)))
|
|
|
|
|
(parameterize ([current-output-port port])
|
|
|
|
|
(when (resolve-condition xo parent)
|
|
|
|
|
(encode (xoptional-type xo) val #:parent parent))
|
|
|
|
|
(unless port-arg (get-output-bytes port))))
|
|
|
|
|
(define xoptional%
|
|
|
|
|
(class xenobase%
|
|
|
|
|
(super-new)
|
|
|
|
|
(init-field type condition)
|
|
|
|
|
|
|
|
|
|
(define/finalize-size (xoptional-size xo [val #f] #:parent [parent #f])
|
|
|
|
|
(when (resolve-condition xo parent)
|
|
|
|
|
(size (xoptional-type xo) val #:parent parent)))
|
|
|
|
|
|
|
|
|
|
(struct xoptional xbase (type condition) #:transparent
|
|
|
|
|
#:methods gen:xenomorphic
|
|
|
|
|
[(define decode xoptional-decode)
|
|
|
|
|
(define xdecode xoptional-decode)
|
|
|
|
|
(define encode xoptional-encode)
|
|
|
|
|
(define size xoptional-size)])
|
|
|
|
|
|
|
|
|
|
#;(define (+xoptional [type-arg #f] [cond-arg #f]
|
|
|
|
|
#:type [type-kwarg #f]
|
|
|
|
|
#:condition [cond-kwarg #f])
|
|
|
|
|
(define type (or type-arg type-kwarg))
|
|
|
|
|
(unless (xenomorphic? type)
|
|
|
|
|
(unless (xenomorphic-type? type)
|
|
|
|
|
(raise-argument-error '+xoptional"xenomorphic type" type))
|
|
|
|
|
(define condition (or cond-arg cond-kwarg))
|
|
|
|
|
(xoptional type condition))
|
|
|
|
|
|
|
|
|
|
(define (resolve-condition parent)
|
|
|
|
|
(define maybe-proc condition)
|
|
|
|
|
(if (procedure? maybe-proc)
|
|
|
|
|
(maybe-proc parent)
|
|
|
|
|
maybe-proc))
|
|
|
|
|
|
|
|
|
|
(define/augment (xxdecode port parent)
|
|
|
|
|
(when (resolve-condition parent)
|
|
|
|
|
(send type xxdecode port parent)))
|
|
|
|
|
|
|
|
|
|
(define/augment (xxencode val port [parent #f])
|
|
|
|
|
(when (resolve-condition parent)
|
|
|
|
|
(send type xxencode val port parent)))
|
|
|
|
|
|
|
|
|
|
(define/augment (xxsize [val #f] [parent #f])
|
|
|
|
|
(if (resolve-condition parent)
|
|
|
|
|
(send type xxsize val parent)
|
|
|
|
|
0))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define no-val (gensym))
|
|
|
|
|
(define (+xoptional [type-arg #f] [cond-arg no-val]
|
|
|
|
|
#:type [type-kwarg #f]
|
|
|
|
|
#:condition [cond-kwarg no-val])
|
|
|
|
|
#:condition [cond-kwarg no-val]
|
|
|
|
|
#:subclass [class xoptional%])
|
|
|
|
|
(define type (or type-arg type-kwarg))
|
|
|
|
|
(unless (xenomorphic? type)
|
|
|
|
|
(raise-argument-error '+xoptional"xenomorphic type" type))
|
|
|
|
|
(define condition (cond
|
|
|
|
|
[(and (eq? cond-arg no-val) (eq? cond-kwarg no-val)) #true]
|
|
|
|
|
[(not (eq? cond-arg no-val)) cond-arg]
|
|
|
|
|
[(not (eq? cond-kwarg no-val)) cond-kwarg]))
|
|
|
|
|
(xoptional type condition))
|
|
|
|
|
(new class [type type] [condition condition]))
|
|
|
|
|