#lang scheme/base
(require mzlib/trace
scheme/contract
"line.ss"
)
(define (header? h)
(and (pair? h)
(string? (car h))
(string? (cdr h))))
(define (header->string h)
(format "~a: ~a" (car h) (cdr h)))
(define (string->header line)
(define (helper match)
(if match
(cons (cadr match) (caddr match))
#f))
(helper (regexp-match #px"^([^:]+)\\s*:\\s*(.+)$" line)))
(define (read-headers in)
(define (return lines)
(map string->header lines))
(define (helper lines)
(let ((l (read-folded-line in)))
(if (string=? l "") (return lines)
(helper (cons l lines)))))
(helper '()))
(provide/contract
(header? (-> any/c boolean?))
(header->string (-> header? string?))
(string->header (-> string? (or/c #f (cons/c string? string?))))
(read-headers (-> input-port? (listof header?)))
)