OCaml library and CLI for OCI and Docker image manipulation
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

json: drop [encode] result form; rename [encode_exn] to [encode]

Per the ocaml-encodings convention, encoding never has a meaningful
runtime failure mode: the encoder walks the GADT and invokes
user-supplied [enc] callbacks. The only failure cases are (a) a codec
built around [Codec.ignore] (programmer error), or (b) a user-
supplied [enc] that raises (user's own bug). Neither is something a
caller should recover from, so the [result] variant was dead API
surface and every caller I've seen either [get_ok]-ed it or fell
back to [Json.Null] on error.

Drop [Codec.encode] / [Json.encode] result-returning, rename the
exception-raising [encode_exn] to just [encode]. [decode] keeps both
forms since malformed JSON is a legitimate runtime condition.

Downstream sweep: 28 files across claude / oci / atp / qemu / scitt /
yaml. Most were pattern A ([Error _ -> Json.Null ((), Meta.none)]) or
pattern B (print the error); both collapse to a direct call. Tests
using pattern F ([match Json.encode ... with Ok j -> ... | Error e
-> Alcotest.fail]) collapse to [let j = Json.encode ... in]. A few
helpers in claude/client.ml and the atp shims kept their names as
thin aliases to preserve grep targets.

+17 -47
+4 -2
lib/spec/arch.ml
··· 53 53 | s -> error_msg "Arch.of_string: invalid string (%S)" s 54 54 55 55 let jsont = 56 + let dec = of_string in 57 + let enc = to_string in 56 58 let open Json.Codec in 57 59 map ~kind:"arch" 58 - ~dec:(fun s -> match of_string s with Ok v -> v | Error _ -> Unknown) 59 - ~enc:to_string string 60 + ~dec:(fun s -> match dec s with Ok v -> v | Error _ -> Unknown) 61 + ~enc string 60 62 61 63 let pp = Fmt.of_to_string to_string 62 64
+2 -9
lib/spec/config.ml
··· 183 183 else Ok t 184 184 | Error e -> Error (Error.of_json e) 185 185 186 - let to_yojson t = 187 - match Json.encode jsont t with 188 - | Ok j -> j 189 - | Error _ -> Json.Null ((), Json.Meta.none) 186 + let to_yojson t = Json.encode jsont t 190 187 191 188 let of_string str = 192 189 match json_of_string str with ··· 463 460 else Ok t 464 461 | Error e -> Error (Error.of_json e) 465 462 466 - let to_yojson t = 467 - match Json.encode jsont t with 468 - | Ok j -> j 469 - | Error _ -> Json.Null ((), Json.Meta.none) 470 - 463 + let to_yojson t = Json.encode jsont t 471 464 let pp ppf t = pp_json ppf (to_yojson t) 472 465 473 466 let of_string str =
+1 -5
lib/spec/descriptor.ml
··· 80 80 | Ok t -> Ok t 81 81 | Error e -> Error (Error.of_json e) 82 82 83 - let to_yojson t = 84 - match Json.encode jsont t with 85 - | Ok j -> j 86 - | Error _ -> Json.Null ((), Json.Meta.none) 87 - 83 + let to_yojson t = Json.encode jsont t 88 84 let pp ppf t = pp_json ppf (to_yojson t) 89 85 let to_string = Fmt.to_to_string pp 90 86 let media_type t = t.media_type
+1 -5
lib/spec/index.ml
··· 51 51 | Ok t -> Ok t 52 52 | Error e -> Error (Error.of_json e) 53 53 54 - let to_yojson t = 55 - match Json.encode jsont t with 56 - | Ok j -> j 57 - | Error _ -> Json.Null ((), Json.Meta.none) 58 - 54 + let to_yojson t = Json.encode jsont t 59 55 let pp ppf t = pp_json ppf (to_yojson t) 60 56 let to_string = Fmt.to_to_string pp 61 57
+1 -4
lib/spec/layout.ml
··· 18 18 | Ok t -> Ok t 19 19 | Error e -> Error (Error.of_json e) 20 20 21 - let to_yojson t = 22 - match Json.encode jsont t with 23 - | Ok j -> j 24 - | Error _ -> Json.Null ((), Json.Meta.none) 21 + let to_yojson t = Json.encode jsont t
+2 -10
lib/spec/manifest.ml
··· 68 68 match check_layers () with Error e -> Error e | Ok () -> Ok t)) 69 69 | Error e -> Error (Error.of_json e) 70 70 71 - let to_yojson t = 72 - match Json.encode jsont t with 73 - | Ok j -> j 74 - | Error _ -> Json.Null ((), Json.Meta.none) 75 - 71 + let to_yojson t = Json.encode jsont t 76 72 let pp ppf t = pp_json ppf (to_yojson t) 77 73 let to_string = Fmt.to_to_string pp 78 74 ··· 116 112 | Ok t -> Ok t 117 113 | Error e -> Error (Error.of_json e) 118 114 119 - let to_yojson t = 120 - match Json.encode jsont t with 121 - | Ok j -> j 122 - | Error _ -> Json.Null ((), Json.Meta.none) 123 - 115 + let to_yojson t = Json.encode jsont t 124 116 let layers t = t.layers 125 117 let config t = t.config 126 118 let pp ppf t = pp_json ppf (to_yojson t)
+1 -5
lib/spec/manifest_list.ml
··· 18 18 | Ok t -> Ok t 19 19 | Error e -> Error (Error.of_json e) 20 20 21 - let to_yojson t = 22 - match Json.encode jsont t with 23 - | Ok j -> j 24 - | Error _ -> Json.Null ((), Json.Meta.none) 25 - 21 + let to_yojson t = Json.encode jsont t 26 22 let pp ppf t = pp_json ppf (to_yojson t) 27 23 let to_string = Fmt.to_to_string pp 28 24 let manifests t = t.manifests
+4 -2
lib/spec/media_type.ml
··· 106 106 | OCI t -> OCI.to_string t 107 107 108 108 let jsont = 109 + let dec = of_string in 110 + let enc = to_string in 109 111 let open Json.Codec in 110 112 map ~kind:"media_type" 111 113 ~dec:(fun s -> 112 - match of_string s with 114 + match dec s with 113 115 | Ok t -> t 114 116 | Error (`Msg e) -> 115 117 Json.Error.failf Json.Meta.none "invalid media_type: %s" e) 116 - ~enc:to_string string 118 + ~enc string 117 119 118 120 let extractor = 119 121 let open Json.Codec in
+1 -5
lib/spec/platform.ml
··· 75 75 76 76 let to_string = Fmt.to_to_string pp 77 77 let pp_v = Fmt.option ~none:(Fmt.any "N/A") Arch.pp_variant 78 - 79 - let dump ppf t = 80 - match Json.encode jsont t with 81 - | Ok j -> pp_json ppf j 82 - | Error e -> Fmt.pf ppf "<error: %s>" (Json.Error.to_string e) 78 + let dump ppf t = pp_json ppf (Json.encode jsont t) 83 79 84 80 let err_arch_variant t = 85 81 Fmt.failwith "%a/%a: invalid architecture/variant pair" Arch.pp t.architecture