-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg.ss
134 lines (119 loc) · 3.45 KB
/
arg.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#lang scheme/base
(require "base.ss")
(require net/uri-codec
scheme/string
(unlib-in date)
"core.ss")
; -> arg
(define (boolean-arg)
(make-arg
"yes|no|true|false|y|n|t|f|1|0"
(lambda (raw)
(and (member (string-downcase raw)
(list "yes" "true" "y" "t" "1"))
#t))
(lambda (arg)
(if arg "yes" "no"))))
; -> arg
(define (integer-arg)
(make-arg
"[-]?[0-9]+"
(lambda (raw)
(string->number raw))
(lambda (arg)
(if (integer? arg)
(number->string arg)
(raise-type-error 'integer-arg "integer" arg)))))
; -> arg
(define (number-arg)
(make-arg
"[-]?[0-9]+|[-]?[0-9]*.[0-9]*?"
(lambda (raw)
(string->number raw))
(lambda (arg)
(cond [(integer? arg) (number->string arg)]
[(real? arg) (number->string (exact->inexact arg))]
[else (raise-type-error 'real-arg "real" arg)]))))
; -> arg
(define (string-arg)
(make-arg
"[^/]+"
(lambda (raw)
(uri-decode raw))
(lambda (arg)
(if (string? arg)
(uri-encode arg)
(raise-type-error 'string-arg "string" arg)))))
; -> arg
(define (symbol-arg)
(make-arg
"[^/]+"
(lambda (raw)
(string->symbol (uri-decode raw)))
(lambda (arg)
(if (symbol? arg)
(uri-encode (symbol->string arg))
(raise-type-error 'symbol-arg "symbol" arg)))))
; -> arg
(define (url-arg)
(make-arg
"[^/]+"
(lambda (raw)
(string->url (uri-decode raw)))
(lambda (arg)
(cond [(string? arg) (uri-encode arg)]
[(url? arg) (uri-encode (url->string arg))]
[else (raise-type-error 'string-arg "string" arg)]))))
(define (time-utc-arg [fmt (current-time-format)] #:tz-ref [tz-ref (lambda _ "GMT")])
(make-arg
"[^/]+"
(lambda (raw)
(let ([date (safe-string->date raw fmt #:tz (tz-ref))])
(if date
(date->time-utc date)
(raise-exn exn:dispatch "no match for date-arg"))))
(lambda (time)
(if (time-utc? time)
(date->string (time-utc->date time #:tz (tz-ref)) fmt #:tz (tz-ref))
(raise-type-error 'time-utc-arg "time-utc" time)))))
; [natural] -> arg
(define (rest-arg [min-length 0])
(make-arg
(if (zero? min-length)
".*"
(format ".{~a,}.*" min-length))
(lambda (raw)
(uri-decode raw))
(lambda (arg)
(if (string? arg)
(uri-encode arg)
(raise-type-error 'rest-arg "string" arg)))))
; enum -> arg
(define (enum-arg enum)
(make-arg
(string-join (map regexp-quote
(map (cut format "~a" <>)
(enum-values enum)))
"|")
(lambda (raw)
(for/or ([val (in-list (enum-values enum))])
(and (equal? (format "~a" val) raw) val)))
(lambda (val)
(if (enum-value? enum val)
(format "~a" val)
(raise-type-error (enum-name enum) (format "~a" (enum-values enum)) val)))))
; Helpers ----------------------------------------
(define (safe-string->date str fmt #:tz tz)
(with-handlers ([exn? (lambda _ #f)])
(string->date str fmt #:tz tz)))
; Provide statements -----------------------------
(provide/contract
[boolean-arg (-> arg?)]
[integer-arg (-> arg?)]
[number-arg (-> arg?)]
[string-arg (-> arg?)]
[symbol-arg (-> arg?)]
[url-arg (-> arg?)]
[time-utc-arg (->* () (string? #:tz-ref (-> string?)) arg?)]
[rest-arg (->* () (natural-number/c) arg?)]
[enum-arg (-> enum? arg?)])