@@ -94,6 +94,17 @@ module Dependency = struct
94
94
| Lt
95
95
| Neq
96
96
97
+ let equal a b =
98
+ match (a, b) with
99
+ | Eq , Eq
100
+ | Gte , Gte
101
+ | Lte , Lte
102
+ | Gt , Gt
103
+ | Lt , Lt
104
+ | Neq , Neq ->
105
+ true
106
+ | _ -> false
107
+
97
108
let map =
98
109
[ (" =" , Eq ); (" >=" , Gte ); (" <=" , Lte ); (" >" , Gt ); (" <" , Lt ); (" <>" , Neq ) ]
99
110
@@ -114,6 +125,11 @@ module Dependency = struct
114
125
| Gt -> `Gt
115
126
| Lt -> `Lt
116
127
| Neq -> `Neq
128
+
129
+ let encode x =
130
+ let f (_ , op ) = equal x op in
131
+ (* Assumes the [map] is complete, so exception is impossible *)
132
+ List. find_exn ~f map |> fst |> Dune_lang.Encoder. string
117
133
end
118
134
119
135
module Constraint = struct
@@ -122,6 +138,10 @@ module Dependency = struct
122
138
| QVar of string
123
139
| Var of string
124
140
141
+ let encode = function
142
+ | QVar v -> Dune_lang.Encoder. string v
143
+ | Var v -> Dune_lang.Encoder. string (" :" ^ v)
144
+
125
145
let decode =
126
146
let open Dune_lang.Decoder in
127
147
let + s = string in
@@ -148,6 +168,16 @@ module Dependency = struct
148
168
| And of t list
149
169
| Or of t list
150
170
171
+ let rec encode c =
172
+ let open Dune_lang.Encoder in
173
+ match c with
174
+ | Bvar x -> Var. encode x
175
+ | Uop (op , x ) -> pair Op. encode Var. encode (op, x)
176
+ | Bop (op , x , y ) -> triple Op. encode Var. encode Var. encode (op, x, y)
177
+ | And conjuncts ->
178
+ list sexp (string " and" :: List. map ~f: encode conjuncts)
179
+ | Or disjuncts -> list sexp (string " or" :: List. map ~f: encode disjuncts)
180
+
151
181
let decode =
152
182
let open Dune_lang.Decoder in
153
183
let ops =
@@ -203,6 +233,10 @@ module Dependency = struct
203
233
; constraint_ : Constraint .t option
204
234
}
205
235
236
+ let encode { name; constraint_ } =
237
+ let open Dune_lang.Encoder in
238
+ list sexp [ Name. encode name; option Constraint. encode constraint_ ]
239
+
206
240
let decode =
207
241
let open Dune_lang.Decoder in
208
242
let constrained =
@@ -322,6 +356,12 @@ module Source_kind = struct
322
356
let constr = to_string kind in
323
357
(constr, decode))
324
358
359
+ let encode { user; repo; kind } =
360
+ let forge = to_string kind in
361
+ let path = repo ^ " /" ^ user in
362
+ let open Dune_lang.Encoder in
363
+ pair string string (forge, path)
364
+
325
365
let to_string { user; repo; kind } =
326
366
sprintf " git+https://%s/%s/%s.git" (host_of_kind kind) user repo
327
367
end
@@ -340,6 +380,12 @@ module Source_kind = struct
340
380
| Host h -> Host. to_string h
341
381
| Url u -> u
342
382
383
+ let encode =
384
+ let open Dune_lang.Encoder in
385
+ function
386
+ | Url url -> pair string string (" uri" , url)
387
+ | Host host -> Host. encode host
388
+
343
389
let decode =
344
390
let open Dune_lang.Decoder in
345
391
sum ((" uri" , string >> | fun s -> Url s) :: Host. enum (fun x -> Host x))
@@ -406,6 +452,26 @@ module Info = struct
406
452
; (" authors" , option (list string ) authors)
407
453
]
408
454
455
+ let encode
456
+ { source
457
+ ; authors
458
+ ; license
459
+ ; homepage
460
+ ; documentation
461
+ ; bug_reports
462
+ ; maintainers
463
+ } =
464
+ let open Dune_lang.Encoder in
465
+ record_fields
466
+ [ field_o " source" Source_kind. encode source
467
+ ; field_o " authors" (list string ) authors
468
+ ; field_o " license" string license
469
+ ; field_o " homepage" string homepage
470
+ ; field_o " documentation" string documentation
471
+ ; field_o " bug_reports" string bug_reports
472
+ ; field_o " maintainers" (list string ) maintainers
473
+ ]
474
+
409
475
let decode ?since () =
410
476
let open Dune_lang.Decoder in
411
477
let v default = Option. value since ~default in
@@ -481,6 +547,41 @@ let name t = t.id.name
481
547
482
548
let dir t = t.id.dir
483
549
550
+ let encode
551
+ { id = _
552
+ ; loc = _
553
+ ; has_opam_file = _
554
+ ; synopsis
555
+ ; description
556
+ ; depends
557
+ ; conflicts
558
+ ; depopts
559
+ ; info
560
+ ; version
561
+ ; tags
562
+ ; deprecated_package_names
563
+ ; sites
564
+ } =
565
+ let open Dune_lang.Encoder in
566
+ let fields =
567
+ record_fields
568
+ [ field_o " synopsis" string synopsis
569
+ ; field_o " description" string description
570
+ ; field_l " depends" Dependency. encode depends
571
+ ; field_l " conflicts" Dependency. encode conflicts
572
+ ; field_l " depopts" Dependency. encode depopts
573
+ ; field_o " version" string version
574
+ ; field_l " tags" string tags
575
+ ; field_l " deprecated_package_names" Name. encode
576
+ (Name.Map. keys deprecated_package_names)
577
+ ; field_l " sits"
578
+ (pair Section.Site. encode Section. encode)
579
+ (Section.Site.Map. to_list sites)
580
+ ]
581
+ @ Info. encode info
582
+ in
583
+ constr " package" (list sexp) fields
584
+
484
585
let decode ~dir =
485
586
let open Dune_lang.Decoder in
486
587
let name_map syntax of_list_map to_string name decode print_value error_msg =
0 commit comments