From: Tim Vaughan Date: Wed, 1 May 2019 22:28:52 +0000 (+0200) Subject: Using (match) to interpret index file entries. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=52779888899c967898021f835ede56e8efe3d625;p=scratchy.git Using (match) to interpret index file entries. --- diff --git a/burrow.scm b/burrow.scm index 26814d8..899edae 100644 --- a/burrow.scm +++ b/burrow.scm @@ -8,7 +8,7 @@ (chicken condition) (chicken process) (chicken process-context) - srfi-13) + srfi-13 matchable) (define gopher-index-file-name "index") @@ -51,21 +51,30 @@ (server-loop)) (tcp-close listener))) +;;; Selector type inference -;;; Selector retrieval +(define (has-suffix? selector . suffixes) + (if (null? suffixes) + #f + (if (string-suffix? (car suffixes) selector) + #t + (apply has-suffix? selector (cdr suffixes))))) -(define (directory-selector? selector) - (string-suffix? "/" selector)) +(define (infer-selector-type selector) + (let ((l (string-downcase selector))) + (cond + ((or (= (string-length l) 0) (string-suffix? "/" l)) 1) + ((has-suffix? l ".txt" ".org" ".md") 0) + ((has-suffix? l ".png" ".jpg" ".gif" ".bmp" ".tif" ".tga") 'I) + (else 9)))) + +;;; Selector retrieval -(define (text-selector? selector) - (apply or (map (lambda (ext) (string-suffix? ext selector)) - '(".txt" ".org" ".md")))) - (define (serve-selector selector config) - ((cond - ((directory-selector? selector) serve-directory) - ((text-selector? seletor) serve-text-file) - (else serve-binary-file)) + ((case (infer-selector-type selector) + ((1) serve-directory) + ((0) serve-text-file) + (else serve-binary-file)) selector config)) (define (serve-directory path config) @@ -117,6 +126,26 @@ (define (serve-info-record info-string) (print "i" info-string "\tfake\tfake\t1\r")) +(define (serve-record record path config) + (match record + ((type display-string selector host port) + (print type display-string "\t" selector "\t" host "\t" port "\r")) + ((type display-string selector host) + (serve-record (list type display-string selector host 70) + path config)) + ((type display-string selector) + (serve-record (list type display-string selector + (server-host config) (server-port config)) + path config)) + ((display-string selector) + (serve-record (list (infer-selector-type selector) display-string selector) + path config)) + ((selector) + (serve-record (list (infer-selector-type selector) selector) + path config)) + (else (error "Unknown record type.")))) + + ;;; main (define (main) @@ -135,9 +164,9 @@ (run-server (make-server-config root hostname port)) (error "Invalid port argument." port)))))) -(main) +;; (main) -;; (define (test) - ;; (run-server (make-server-config "gopher-root" "localhost" 70))) +(define (test) + (run-server (make-server-config "gopher-root" "localhost" 70))) ;; (test)