resolvers.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; Disarchive
  2. ;;; Copyright © 2020, 2021 Timothy Sample <samplet@ngyro.com>
  3. ;;;
  4. ;;; This file is part of Disarchive.
  5. ;;;
  6. ;;; Disarchive is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Disarchive is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Disarchive. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (disarchive resolvers)
  19. #:use-module (disarchive logging)
  20. #:use-module (ice-9 match)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-9)
  23. #:export (<resolver>
  24. make-resolver
  25. resolver?
  26. resolver-name
  27. resolver-file-address
  28. resolver-resolve-address
  29. resolver-serialize-address
  30. resolver-deserialize-address
  31. file-addresses
  32. resolve-address
  33. serialize-address
  34. deserialize-address
  35. resolve-addresses
  36. %resolve-addresses))
  37. (define-record-type <resolver>
  38. (make-resolver name file-address resolve-address
  39. serialize-address deserialize-address)
  40. resolver?
  41. (name resolver-name)
  42. (file-address resolver-file-address)
  43. (resolve-address resolver-resolve-address)
  44. (serialize-address resolver-serialize-address)
  45. (deserialize-address resolver-deserialize-address))
  46. (define (name->resolver name)
  47. (let ((module `(disarchive resolvers ,name)))
  48. (module-ref (resolve-interface module)
  49. (symbol-append name '-resolver))))
  50. (define %resolvers
  51. (delay (map name->resolver
  52. '(swhid))))
  53. (define (lookup-resolver name)
  54. (find (lambda (resolver)
  55. (eq? (resolver-name resolver) name))
  56. (force %resolvers)))
  57. (define (resolve-address address output)
  58. (match-let* (((name payload) address)
  59. (resolver (lookup-resolver name)))
  60. ((resolver-resolve-address resolver) payload output)))
  61. (define (file-addresses filename)
  62. (map (lambda (resolver)
  63. `(,(resolver-name resolver)
  64. ,((resolver-file-address resolver) filename)))
  65. (force %resolvers)))
  66. (define (serialize-address address)
  67. (match-let* (((name payload) address)
  68. (resolver (lookup-resolver name)))
  69. `(,name ,((resolver-serialize-address resolver) payload))))
  70. (define (deserialize-address obj)
  71. (match-let* (((name payload-obj) obj)
  72. (resolver (lookup-resolver name)))
  73. `(,name ,((resolver-deserialize-address resolver) payload-obj))))
  74. (define (resolve-addresses addresses output)
  75. (let ((count (length addresses)))
  76. (if (= count 1)
  77. (message "Checking 1 address")
  78. (message "Checking ~a addresses" count)))
  79. (any (lambda (address)
  80. (match address
  81. ((name _) (start-message " ~a... " name)))
  82. (if (resolve-address address output)
  83. (begin (message "yes!") #t)
  84. (begin (message "no" #f))))
  85. addresses))
  86. ;; In the future, 'resolve-addresses' could be the default resolver,
  87. ;; but right now we only know how to resolve SWHIDs via Guix. In an
  88. ;; effort to avoid Guix as a dependency, the default resolver just
  89. ;; fails, with the expectation that clients will provide their own
  90. ;; resolver.
  91. (define %resolve-addresses
  92. (make-parameter (const #f)))