guile-netlink/netlink/data.scm

data.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (netlink data)
19
  #:use-module (ice-9 match)
20
  #:use-module (srfi srfi-9)
21
  #:export (make-nl-data
22
            nl-data-data nl-data-size-proc nl-data-serialize-proc
23
            data-size ensure-data-size
24
            serialize deserialize
25
            get-current-deserialize get-next-deserialize
26
            define-data-type))
27
28
(define-record-type nl-data
29
  (make-nl-data data size-proc serialize-proc)
30
  nl-data?
31
  (data            nl-data-data)
32
  (size-proc       nl-data-size-proc)
33
  (serialize-proc   nl-data-serialize-proc))
34
35
(define (data-size data)
36
  ((nl-data-size-proc data) (nl-data-data data)))
37
38
(define (serialize data pos bv)
39
  ((nl-data-serialize-proc data) (nl-data-data data) pos bv))
40
41
(define (ensure-data-size data size)
42
  (make-nl-data
43
    (nl-data-data data)
44
    (const size)
45
    (nl-data-serialize-proc data)))
46
47
(define (get-next-deserialize decoder current-type target-type)
48
  (match (assoc-ref decoder current-type)
49
    ((_ . type-alist)
50
     (or (assoc-ref type-alist target-type)
51
         (assoc-ref type-alist 'default)))
52
    (#f (throw 'no-decoder current-type))))
53
  
54
(define (get-current-deserialize decoder current-type)
55
  (match (assoc-ref decoder current-type)
56
    ((current-deserialize . _) current-deserialize)
57
    (#f (throw 'no-decoder current-type))))
58
59
(define (deserialize type decoder bv pos)
60
  (let ((deserialize (get-current-deserialize decoder type)))
61
    (deserialize decoder bv pos)))
62
63
(define* (syntax-append x . s)
64
  (define (->symbol s)
65
    (if (symbol? s) s (syntax->datum s)))
66
  (datum->syntax x (apply symbol-append (map ->symbol s))))
67
68
(define-syntax define-data-type
69
  (lambda (x)
70
    (syntax-case x ()
71
      ((_ name size-proc serialize-proc (field accessor internal-accessor) ...)
72
       #`(begin
73
           (define-record-type #,(syntax-append x #'name '-type)
74
            (#,(syntax-append x 'make- #'name '-type) field ...)
75
            #,(syntax-append x #'name '-type?)
76
            (field internal-accessor) ...)
77
78
           (define (accessor data)
79
            (internal-accessor (nl-data-data data)))
80
           ...
81
82
           (define (#,(syntax-append x 'make- #'name) field ...)
83
            (make-nl-data
84
              (#,(syntax-append x 'make- #'name '-type) field ...)
85
              size-proc
86
              serialize-proc))
87
88
           (define (#,(syntax-append x #'name '?) data)
89
             (#,(syntax-append x #'name '-type?)
90
              (nl-data-data data))))))))
91