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.
38 lines
1.2 KiB
Racket
38 lines
1.2 KiB
Racket
#lang racket/base
|
|
(require racket/class "helper.rkt" racket/list)
|
|
(provide (all-defined-out))
|
|
|
|
#|
|
|
approximates
|
|
https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee
|
|
|#
|
|
|
|
(define xenum%
|
|
(class xenobase%
|
|
(super-new)
|
|
(init-field type values)
|
|
|
|
(define/augment (xxdecode port parent)
|
|
(define index (send type xxdecode port parent))
|
|
(or (list-ref values index) index))
|
|
|
|
(define/augment (xxencode val port [parent #f])
|
|
(define index (index-of values val))
|
|
(unless index
|
|
(raise-argument-error 'xenum-encode "valid option" val))
|
|
(send type xxencode index port parent))
|
|
|
|
(define/augment (xxsize [val #f] [parent #f])
|
|
(send type xxsize val parent))))
|
|
|
|
(define (+xenum [type-arg #f] [values-arg #f]
|
|
#:type [type-kwarg #f]
|
|
#:values [values-kwarg #f]
|
|
#:subclass [class xenum%])
|
|
(define type (or type-arg type-kwarg))
|
|
(unless (xenomorphic-type? type)
|
|
(raise-argument-error '+xenum "xenomorphic type" type))
|
|
(define values (or values-arg values-kwarg))
|
|
(unless (list? values)
|
|
(raise-argument-error '+xenum "list of values" values))
|
|
(new class [type type] [values values])) |