~ruther/guix-local

ref: 82e58c26db9ebe5ccd66fecd92aa423a42ecda87 guix-local/guix/import/crate/cargo-lock.scm -rw-r--r-- 3.1 KiB
82e58c26 — Efraim Flashner guix: Spelling corrections. a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2024 Murilo <murilo@disroot.org>
;;; Copyright © 2024 Luis Guilherme Coelho <lgcoelho@disroot.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix import crate cargo-lock)
  #:use-module (ice-9 peg)
  #:export (cargo-lock-string->scm

            crate-name
            crate-version
            crate-source
            crate-checksum
            crate-dependencies
            cargo-lock))

;;;
;;; PEG parser for ‘Cargo.lock’.
;;;

(define (cargo-lock-string->scm str)
  (peg:tree (search-for-pattern cargo-lock str)))

;; Auxiliary peg patterns
(define-peg-pattern numeric-char body
  (range #\0 #\9))

(define-peg-pattern lowercase-char body
  (range #\a #\z))

(define-peg-pattern uppercase-char body
  (range #\A #\Z))

(define-peg-pattern alphabetic-char body
  (or lowercase-char uppercase-char))

(define-peg-pattern alphanumeric-char body
  (or alphabetic-char numeric-char))

;; name
(define-peg-pattern crate-name all
  (+ (or "-" alphabetic-char
         "_" numeric-char)))

;; version
(define-peg-pattern non-negative-integer body
  (+ numeric-char))

(define-peg-pattern crate-version all
  (and non-negative-integer "."
       non-negative-integer "."
       non-negative-integer
       (? (+ (or "-" lowercase-char
                 "." uppercase-char
                 "+" numeric-char "_")))))

;; source
(define-peg-pattern crate-source all
  (and (or "registry" "git")
       "+https://"
       (+ (or "/" "." "?" "=" "-" "#" "_" "~"
              alphanumeric-char))))

;; checksum
(define-peg-pattern crate-checksum all
  (+ (or lowercase-char numeric-char)))

;; dependency specification
(define-peg-pattern dependency-specification all
  (and crate-name (? (and (ignore " ") crate-version))))

;; dependencies
(define-peg-pattern crate-dependencies all
  (and (ignore "[\n")
       (+ (and (ignore " \"")
               (capture dependency-specification)
               (ignore "\",\n")))
       (ignore "]")))

;; crates
(define-peg-pattern crate all
  (and (ignore "[[package]]\n")
       (ignore "name = \"") (capture crate-name) (ignore "\"\n")
       (ignore "version = \"") (capture crate-version) (ignore "\"\n")
       (? (and (ignore "source = \"") (capture crate-source) (ignore "\"\n")))
       (? (and (ignore "checksum = \"") (capture crate-checksum) (ignore "\"\n")))
       (? (ignore (and "dependencies = " crate-dependencies "\n")))))

;; Cargo.lock
(define-peg-pattern cargo-lock all
  (+ (and (ignore "\n") crate)))