Start adding serializing vCard alists into strings

This commit is contained in:
Jaidyn Ann 2024-02-15 01:08:56 -06:00
parent 076fe6f24b
commit dabda67b33

View File

@ -148,7 +148,9 @@
(list date-prop
string->any-date
(lambda (datetime)
(date->string datetime "~Y~m~dT~H~M~S~z"))))
(if (date? datetime)
(date->string datetime "~Y~m~dT~H~M~S~z")
datetime))))
vcard-datetime-properties)
(map (lambda (csv-prop)
(list csv-prop
@ -199,11 +201,17 @@
(string-split string delimiter)))))
(define (parse-vcard-prop property elements)
;; Given a vCard property and its values (e.g., “VERSION” and “3.0”),
;; parse them into a list.
;; “EMAIL;TYPE=home” "mom@dad.com” → '(EMAIL ("TYPE=home") "mom@dad.com")
(define (parse-vcard-property property elements)
(append (list property)
(list elements)))
;; Given the value(s) of a vCard elements value string(s), returned a
;; parsed object.
;; 'BIRTHDAY "2024-01-02T00:00:00" → #@2024-01-02T00:00:00-0600
(define (parse-vcard-value prop elements)
(let [(parser-and-unparser-funcs (alist-ref prop vcard-value-parsers))]
(if parser-and-unparser-funcs
@ -213,6 +221,30 @@
(list elements))))
;; With an element of a parsed vCard alist, serialize it (back) into a
;; string.
(define (serialize-vcard-element element)
(let [(property (string-join
(append (list (symbol->string (car element)))
(second element)) ";"))
(value (serialize-vcard-value (car element) (last element)))]
(string-join (list property value) ":")))
;; Serialize the value of a vCard property (from a parsed vCard alist)
;; into a string.
(define (serialize-vcard-value prop value)
(let [(parser-and-unparser-funcs (alist-ref prop vcard-value-parsers))]
(if parser-and-unparser-funcs
(apply (cadr parser-and-unparser-funcs) (list value))
value)))
;; Serialize a vCard parsed-alist into a set of line-strings, once more.
(define (serialized-vcard vcard-alist)
(map serialize-vcard-element vcard-alist))
;; Parse a line of a vcard file into an alist-friendly format:
;; (KEY ("keyprop1=d" "keyprop2=b") "VALUE" ("valprop1=a" "valprop2=b"))
(define (parse-vcard-line line)
@ -220,7 +252,7 @@
(prop-elements (string-split-unescaped (car prop-value-strings) ";"))
(value-elements (cdr prop-value-strings))
(property (string->symbol (string-upcase (car prop-elements))))]
(append (parse-vcard-prop property (cdr prop-elements))
(append (parse-vcard-property property (cdr prop-elements))
(parse-vcard-value property value-elements))))