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 | (raise (condition (&netlink-decoder-error |
| 56 | (type current-type) |
| 57 | (sub-type target-type)))))) |
| 58 | (#f (raise (condition (&netlink-decoder-error |
| 59 | (type current-type) |
| 60 | (sub-type target-type))))))) |
| 61 | |
| 62 | (define (get-current-deserialize decoder current-type) |
| 63 | (match (assoc-ref decoder current-type) |
| 64 | ((current-deserialize . _) current-deserialize) |
| 65 | (#f (raise (condition (&netlink-decoder-error |
| 66 | (type current-type) |
| 67 | (sub-type #f))))))) |
| 68 | |
| 69 | (define (deserialize type decoder bv pos) |
| 70 | (let ((deserialize (get-current-deserialize decoder type))) |
| 71 | (deserialize decoder bv pos))) |
| 72 | |
| 73 | (define* (syntax-append x . s) |
| 74 | (define (->symbol s) |
| 75 | (if (symbol? s) s (syntax->datum s))) |
| 76 | (datum->syntax x (apply symbol-append (map ->symbol s)))) |
| 77 | |
| 78 | (define-syntax define-data-type |
| 79 | (lambda (x) |
| 80 | (syntax-case x () |
| 81 | ((_ name size-proc serialize-proc (field accessor internal-accessor) ...) |
| 82 | #`(begin |
| 83 | (define-record-type #,(syntax-append x #'name '-type) |
| 84 | (#,(syntax-append x 'make- #'name '-type) field ...) |
| 85 | #,(syntax-append x #'name '-type?) |
| 86 | (field internal-accessor) ...) |
| 87 | |
| 88 | (define (accessor data) |
| 89 | (internal-accessor (nl-data-data data))) |
| 90 | ... |
| 91 | |
| 92 | (define (#,(syntax-append x 'make- #'name) field ...) |
| 93 | (make-nl-data |
| 94 | (#,(syntax-append x 'make- #'name '-type) field ...) |
| 95 | size-proc |
| 96 | serialize-proc)) |
| 97 | |
| 98 | (define (#,(syntax-append x #'name '?) data) |
| 99 | (#,(syntax-append x #'name '-type?) |
| 100 | (nl-data-data data)))))))) |
| 101 |