#lang scheme/base
(require scheme/serialize
(only-in srfi/1 append-map)
"../base.ss"
"../plain/render.ss")
(define-struct csv () #:prefab)
(define-struct (sheet csv) (rows) #:prefab)
(define-struct (row csv) (cells) #:prefab)
(define-struct (cell csv) (value) #:prefab)
(define (create-sheet . rows)
(make-sheet (normalize rows)))
(define (create-row . cells)
(make-row (normalize cells)))
(define create-cell
make-cell)
(define (normalize data)
(cond [(null? data) null]
[(pair? data) (append-map normalize data)]
[else (list data)]))
(define rows/c
(flat-rec-contract row-or-row-list/c
row?
(listof row-or-row-list/c)))
(define cells/c
(flat-rec-contract cell-or-cell-list/c
cell?
(listof cell-or-cell-list/c)))
(provide/contract
[struct csv ()]
[struct (sheet csv) ([rows (listof row?)])]
[struct (row csv) ([cells (listof cell?)])]
[struct (cell csv) ([value quotable-value?])]
[create-sheet (->* () () #:rest (listof rows/c) sheet?)]
[create-row (->* () () #:rest (listof cells/c) row?)]
[create-cell (-> quotable-value? cell?)]
[quotable-value? procedure?])