Skip to content

Commit 36c9c71

Browse files
authored
Add encode function for Package.t (#4815)
* Add equal function for Package.Dependency.Op * Add encode for Package.Depedency.Op * Add encode function for Package.Dependency * Add encode function for Package.Source_kind * Add encode function for Package.Info * Add encode function for Package.t * Improve Dune_lang.Encoder.field_l documentation Signed-off-by: Shon Feder <shon.feder@gmail.com>
1 parent e5e963a commit 36c9c71

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/dune_engine/package.ml

+101
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ module Dependency = struct
9494
| Lt
9595
| Neq
9696

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+
97108
let map =
98109
[ ("=", Eq); (">=", Gte); ("<=", Lte); (">", Gt); ("<", Lt); ("<>", Neq) ]
99110

@@ -114,6 +125,11 @@ module Dependency = struct
114125
| Gt -> `Gt
115126
| Lt -> `Lt
116127
| 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
117133
end
118134

119135
module Constraint = struct
@@ -122,6 +138,10 @@ module Dependency = struct
122138
| QVar of string
123139
| Var of string
124140

141+
let encode = function
142+
| QVar v -> Dune_lang.Encoder.string v
143+
| Var v -> Dune_lang.Encoder.string (":" ^ v)
144+
125145
let decode =
126146
let open Dune_lang.Decoder in
127147
let+ s = string in
@@ -148,6 +168,16 @@ module Dependency = struct
148168
| And of t list
149169
| Or of t list
150170

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+
151181
let decode =
152182
let open Dune_lang.Decoder in
153183
let ops =
@@ -203,6 +233,10 @@ module Dependency = struct
203233
; constraint_ : Constraint.t option
204234
}
205235

236+
let encode { name; constraint_ } =
237+
let open Dune_lang.Encoder in
238+
list sexp [ Name.encode name; option Constraint.encode constraint_ ]
239+
206240
let decode =
207241
let open Dune_lang.Decoder in
208242
let constrained =
@@ -322,6 +356,12 @@ module Source_kind = struct
322356
let constr = to_string kind in
323357
(constr, decode))
324358

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+
325365
let to_string { user; repo; kind } =
326366
sprintf "git+https://%s/%s/%s.git" (host_of_kind kind) user repo
327367
end
@@ -340,6 +380,12 @@ module Source_kind = struct
340380
| Host h -> Host.to_string h
341381
| Url u -> u
342382

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+
343389
let decode =
344390
let open Dune_lang.Decoder in
345391
sum (("uri", string >>| fun s -> Url s) :: Host.enum (fun x -> Host x))
@@ -406,6 +452,26 @@ module Info = struct
406452
; ("authors", option (list string) authors)
407453
]
408454

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+
409475
let decode ?since () =
410476
let open Dune_lang.Decoder in
411477
let v default = Option.value since ~default in
@@ -481,6 +547,41 @@ let name t = t.id.name
481547

482548
let dir t = t.id.dir
483549

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+
484585
let decode ~dir =
485586
let open Dune_lang.Decoder in
486587
let name_map syntax of_list_map to_string name decode print_value error_msg =

src/dune_engine/package.mli

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ val dir : t -> Path.Source.t
154154

155155
val file : dir:Path.t -> name:Name.t -> Path.t
156156

157+
val encode : t Dune_lang.Encoder.t
158+
157159
val decode : dir:Path.Source.t -> t Dune_lang.Decoder.t
158160

159161
val opam_file : t -> Path.Source.t

src/dune_lang/encoder.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ val field_o : string -> 'a t -> 'a option -> field
1717

1818
val field_b : string -> bool -> field
1919

20-
(** Field with inlined list as value *)
20+
(** Field with inlined list as value
21+
22+
The field is left absent if the list is empty. *)
2123
val field_l : string -> 'a t -> 'a list -> field
2224

2325
(** Same as [field_l] but to represent a single value *)

0 commit comments

Comments
 (0)