1#lang typed/racket/base 2 3;; Detect an image file type from a filename extension. 4 5(require racket/list 6 "types.rkt") 7 8(provide (all-defined-out)) 9 10(: file-type-hash (Listof (Pair Regexp Image-File-Format))) 11(define file-type-hash 12 '((#rx"(?i:.*\\.png)$" . png) 13 (#rx"(?i:.*\\.(jpg)|(jpeg))$" . jpeg) 14 (#rx"(?i:.*\\.xbm)$" . xbm) 15 (#rx"(?i:.*\\.xpm)$" . xpm) 16 (#rx"(?i:.*\\.bmp)$" . bmp) 17 (#rx"(?i:.*\\.pdf)$" . pdf) 18 (#rx"(?i:.*\\.ps)$" . ps) 19 (#rx"(?i:.*\\.svg)$" . svg))) 20 21(: detect-image-file-type (-> Path-String Image-File-Format)) 22(define (detect-image-file-type output) 23 (define name 24 (cond [(string? output) output] 25 [(path? output) (path->string output)] 26 [else (error 'detect-image-file-type "cannot detect file type for ~e" output)])) 27 (let loop ([hash file-type-hash]) 28 (cond [(empty? hash) (error 'detect-image-file-type "~e has unknown file type" name)] 29 [(regexp-match (car (first hash)) name) (cdr (first hash))] 30 [else (loop (rest hash))]))) 31