add url type support

pull/9/head
Matthew Butterick 11 years ago
parent ceb6fc40b6
commit 2fffe88d83

@ -1,5 +1,5 @@
#lang racket/base #lang racket/base
(require racket/contract) (require racket/contract net/url)
(require (only-in racket/list empty? range splitf-at dropf dropf-right)) (require (only-in racket/list empty? range splitf-at dropf dropf-right))
(require (only-in racket/format ~a)) (require (only-in racket/format ~a))
(require (only-in racket/string string-join)) (require (only-in racket/string string-join))
@ -31,6 +31,7 @@
[(empty? x) ""] [(empty? x) ""]
[(symbol? x) (symbol->string x)] [(symbol? x) (symbol->string x)]
[(number? x) (number->string x)] [(number? x) (number->string x)]
[(url? x) (->string (->path x))]
[(path? x) (path->string x)] [(path? x) (path->string x)]
[(char? x) (~a x)] [(char? x) (~a x)]
[else (error (format "Can't make ~a into string" x))])) [else (error (format "Can't make ~a into string" x))]))
@ -40,6 +41,7 @@
(check-equal? (->string '()) "") (check-equal? (->string '()) "")
(check-equal? (->string 'foo) "foo") (check-equal? (->string 'foo) "foo")
(check-equal? (->string 123) "123") (check-equal? (->string 123) "123")
(check-equal? (->string (string->url "foo/bar.html")) "foo/bar.html")
(define file-name-as-text "foo.txt") (define file-name-as-text "foo.txt")
(check-equal? (->string (string->path file-name-as-text)) file-name-as-text) (check-equal? (->string (string->path file-name-as-text)) file-name-as-text)
(check-equal? (->string #\¶) "")) (check-equal? (->string #\¶) ""))
@ -51,9 +53,24 @@
(string->symbol (->string thing))) (string->symbol (->string thing)))
;; general way of coercing to path ;; general way of coercing to path
(define (->path thing) (define/contract (->path thing)
(any/c . -> . path?)
; todo: on bad input, it will pop a string error rather than path error ; todo: on bad input, it will pop a string error rather than path error
(string->path (->string thing))) (cond
[(url? thing) (apply build-path (map path/param-path (url-path thing)))]
[else (string->path (->string thing))]))
(module+ test
(check-equal? (->path "foo") (string->path "foo"))
(check-equal? (->path 'foo) (string->path "foo"))
(check-equal? (->path 123) (string->path "123"))
(check-equal? (->path (string->url "foo/bar.html")) (string->path "foo/bar.html")))
;; general way of coercing to url
(define/contract (->url thing)
(any/c . -> . url?)
; todo: on bad input, it will pop a string error rather than url error
(string->url (->string thing)))
(define (->complete-path thing) (define (->complete-path thing)
(path->complete-path (->path thing))) (path->complete-path (->path thing)))

Loading…
Cancel
Save