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.
sugar/coerce.rkt

145 lines
4.0 KiB
Racket

#lang racket/base
(require racket/contract net/url xml racket/set)
(module+ test (require rackunit))
(require "len.rkt" "exception.rkt" "define.rkt" "debug.rkt")
11 years ago
(define (make-coercion-error-handler target-format x)
(λ(e) (error (format "Can't convert ~a to ~a" x target-format))))
;; general way of coercing to integer
(define+provide/contract (->int x)
11 years ago
(any/c . -> . integer?)
11 years ago
(try
(cond
[(or (integer? x) (real? x)) (inexact->exact (floor x))]
[(and (string? x) (> (len x) 0)) (->int (string->number x))]
[(symbol? x) (->int (->string x))]
[(char? x) (char->integer x)]
[else (len x)])
11 years ago
(except [exn:fail? (make-coercion-error-handler 'integer x)])))
11 years ago
;; general way of coercing to string
(define+provide/contract (->string x)
11 years ago
(any/c . -> . string?)
11 years ago
(try
(cond
[(string? x) x]
[(equal? '() x) ""]
[(symbol? x) (symbol->string x)]
[(number? x) (number->string x)]
[(url? x) (->string (->path x))] ; todo: a url is more than just a path-string ... it has character encoding issues
[(path? x) (path->string x)]
[(char? x) (format "~a" x)]
[else (error)]) ; put this last so other xexprish things don't get caught
11 years ago
(except [exn:fail? (make-coercion-error-handler 'string x)])))
11 years ago
;; general way of coercing to html
(define+provide/contract (->html x)
(any/c . -> . string?)
(try (xexpr->string x)
(except [exn:fail? (make-coercion-error-handler 'html x)])))
;; general way of coercing to symbol
(define+provide/contract (->symbol x)
11 years ago
(any/c . -> . symbol?)
11 years ago
(try (string->symbol (->string x))
11 years ago
(except [exn:fail? (make-coercion-error-handler 'symbol x)])))
;; general way of coercing to path
(define+provide/contract (->path x)
11 years ago
(any/c . -> . path?)
11 years ago
(try
(cond
[(url? x) (apply build-path (map path/param-path (url-path x)))]
[else (string->path (->string x))])
11 years ago
(except [exn:fail? (make-coercion-error-handler 'path x)])))
;; general way of coercing to url
(define+provide/contract (->url x)
11 years ago
(any/c . -> . url?)
11 years ago
(try (string->url (->string x))
11 years ago
(except [exn:fail? (make-coercion-error-handler 'url x)])))
(define+provide/contract (->complete-path x)
11 years ago
(any/c . -> . complete-path?)
11 years ago
(try (path->complete-path (->path x))
11 years ago
(except [exn:fail? (make-coercion-error-handler 'complete-path x)])))
;; general way of coercing to a list
(define+provide/contract (->list x)
11 years ago
(any/c . -> . list?)
11 years ago
(try
(cond
[(list? x) x]
[(vector? x) (vector->list x)]
[(set? x) (set->list x)]
[else (list x)])
11 years ago
(except [exn:fail? (make-coercion-error-handler 'list x)])))
;; general way of coercing to vector
(define+provide/contract (->vector x)
11 years ago
(any/c . -> . vector?)
11 years ago
(try
(cond
[(vector? x) x]
[else (list->vector (->list x))])
11 years ago
(except [exn:fail? (make-coercion-error-handler 'vector x)])))
;; general way of coercing to boolean
(define+provide/contract (->boolean x)
11 years ago
(any/c . -> . boolean?)
(if x #t #f))
11 years ago
;;
;; Coercion contracts
;;
(define-syntax-rule (make-blame-handler try-proc expected-sym)
(λ (b)
(λ (x)
(try (try-proc x)
(except [exn:fail? (λ(e)
(raise-blame-error
b x
'(expected: "~a" given: "~e")
expected-sym x))])))))
11 years ago
(define+provide coerce/integer?
11 years ago
(make-contract
#:name 'coerce/integer?
#:projection (make-blame-handler ->int 'can-be-integer?)))
11 years ago
(define+provide coerce/string?
11 years ago
(make-contract
#:name 'coerce/string?
#:projection (make-blame-handler ->string 'can-be-string?)))
11 years ago
(define+provide coerce/symbol?
11 years ago
(make-contract
#:name 'coerce/symbol?
#:projection (make-blame-handler ->symbol 'can-be-symbol?)))
11 years ago
(define+provide coerce/path?
11 years ago
(make-contract
#:name 'coerce/path?
#:projection (make-blame-handler ->path 'can-be-path?)))
11 years ago
(define+provide coerce/boolean?
11 years ago
(make-contract
#:name 'coerce/boolean?
#:projection (make-blame-handler ->boolean 'can-be-boolean?)))