guile-netlink/netlink/data.scm

data.scm

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