Add initial bencode serialization
This commit is contained in:
parent
b6d2775f20
commit
a7c418a1c0
|
@ -0,0 +1,31 @@
|
|||
(define-module (guile-nrepl bencode)
|
||||
#:use-module (ice-9 format)
|
||||
#:export (string->bencode
|
||||
integer->bencode
|
||||
list->bencode
|
||||
pair->bencode))
|
||||
|
||||
(define (string->bencode input-str)
|
||||
(when (string? input-str)
|
||||
(format #f "~d:~a" (string-length input-str) input-str)))
|
||||
|
||||
(define (integer->bencode input-int)
|
||||
(when (integer? input-int)
|
||||
(format #f "i~de" input-int)))
|
||||
|
||||
(define (list->bencode input-list)
|
||||
(when (list? input-list)
|
||||
(format #f "l~ae" (apply string-append (map bencode-encode-dispatch input-list)))))
|
||||
|
||||
(define (pair->bencode input-pair)
|
||||
(when (pair? input-pair)
|
||||
(format #f "d~a~ae"
|
||||
(bencode-encode-dispatch (car input-pair))
|
||||
(bencode-encode-dispatch (cdr input-pair)))))
|
||||
|
||||
(define (bencode-encode-dispatch input-value)
|
||||
(cond
|
||||
((string? input-value) (string->bencode input-value))
|
||||
((integer? input-value) (integer->bencode input-value))
|
||||
((list? input-value) (list->bencode input-value))
|
||||
((pair? input-value) (pair->bencode input-value))))
|
Loading…
Reference in New Issue