pull/2/head
Matthew Butterick 10 years ago
parent 05516c51da
commit ec061a6d89

@ -45,7 +45,7 @@
(try (xexpr->string x) (try (xexpr->string x)
(except [exn:fail? (make-coercion-error-handler 'html x)]))) (except [exn:fail? (make-coercion-error-handler 'html x)])))
; must get this right — no escaped chars ; todo: must get this right — no escaped chars
;(->html '(script ((type "text/javascript")) "3 > 2")) ;(->html '(script ((type "text/javascript")) "3 > 2"))
@ -53,17 +53,21 @@
;; general way of coercing to symbol ;; general way of coercing to symbol
(define+provide/contract (->symbol x) (define+provide/contract (->symbol x)
(any/c . -> . symbol?) (any/c . -> . symbol?)
(try (string->symbol (->string x)) (if (symbol? x)
(except [exn:fail? (make-coercion-error-handler 'symbol x)]))) x
(try (string->symbol (->string x))
(except [exn:fail? (make-coercion-error-handler 'symbol x)]))))
;; general way of coercing to path ;; general way of coercing to path
(define+provide/contract (->path x) (define+provide/contract (->path x)
(any/c . -> . path?) (any/c . -> . path?)
(try (if (path? x)
(cond x
[(url? x) (apply build-path (map path/param-path (url-path x)))] (try
[else (string->path (->string x))]) (cond
(except [exn:fail? (make-coercion-error-handler 'path x)]))) [(url? x) (apply build-path (map path/param-path (url-path x)))]
[else (string->path (->string x))])
(except [exn:fail? (make-coercion-error-handler 'path x)]))))
;; general way of coercing to url ;; general way of coercing to url
@ -81,23 +85,24 @@
;; general way of coercing to a list ;; general way of coercing to a list
(define+provide/contract (->list x) (define+provide/contract (->list x)
(any/c . -> . list?) (any/c . -> . list?)
(try (if (list? x)
(cond x
[(list? x) x] (try
[(vector? x) (vector->list x)] (cond
[(set? x) (set->list x)] [(vector? x) (vector->list x)]
[else (list x)]) [(set? x) (set->list x)]
(except [exn:fail? (make-coercion-error-handler 'list x)]))) [else (list x)])
(except [exn:fail? (make-coercion-error-handler 'list x)]))))
;; general way of coercing to vector ;; general way of coercing to vector
(define+provide/contract (->vector x) (define+provide/contract (->vector x)
(any/c . -> . vector?) (any/c . -> . vector?)
(try (if (vector? x)
(cond x
[(vector? x) x] (try
[else (list->vector (->list x))]) (list->vector (->list x))
(except [exn:fail? (make-coercion-error-handler 'vector x)]))) (except [exn:fail? (make-coercion-error-handler 'vector x)]))))

Loading…
Cancel
Save