From 76d3edf0e19c2c44e112e73e874ccbeae7daf71c Mon Sep 17 00:00:00 2001 From: Matt Bray Date: Wed, 15 Jun 2022 11:45:22 +0100 Subject: [PATCH] fix: post rebase: 'e type param is fixed to 'value Error.t --- src/decode.ml | 16 +++++++++++----- src/error.ml | 12 +++++++++++- src/error.mli | 2 ++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/decode.ml b/src/decode.ml index 9c7e08a..44583bf 100644 --- a/src/decode.ml +++ b/src/decode.ml @@ -292,7 +292,7 @@ module Make (Decodeable : Decodeable) : ; map : value U.String_map.t } - type 'a obj = (t, 'a * t, value Error.t) Decoder.t + type 'a obj = (t, 'a * t) Decoder.t let succeed x t = Ok (x, t) @@ -338,7 +338,11 @@ module Make (Decodeable : Decodeable) : | Some value -> let m = U.String_map.remove key t.map in let t = { t with map = m } in - (match v_dec value with Ok x -> Ok (Some x, t) | Error e -> Error e) + ( match v_dec value with + | Ok x -> + Ok (Some x, t) + | Error e -> + Error (Error.map_context (fun context -> { t with context }) e) ) let field key v_dec : 'a obj = @@ -350,7 +354,7 @@ module Make (Decodeable : Decodeable) : Error (Error.make (Printf.sprintf "Expected an object with an attribute %S" key) - ~context:t.context ) + ~context:t ) | Error e -> Error e @@ -366,7 +370,7 @@ module Make (Decodeable : Decodeable) : (Printf.sprintf "Expected an empty object, but have unconsumed field %S" k ) - ~context:t.context ) + ~context:t ) let run : 'a obj -> 'a decoder = @@ -375,7 +379,9 @@ module Make (Decodeable : Decodeable) : | Ok l -> let map = U.String_map.of_list l in let t = { context; map } in - dec t |> U.My_result.map (fun (x, _) -> x) + dec t + |> U.My_result.map (fun (x, _) -> x) + |> U.My_result.map_err (Error.map_context (fun t -> t.context)) | Error e -> Error e end diff --git a/src/error.ml b/src/error.ml index ead1fb0..7c77229 100644 --- a/src/error.ml +++ b/src/error.ml @@ -1,3 +1,5 @@ +module U = Decoders_util + type 'a t = | E of { msg : string @@ -25,7 +27,7 @@ let rec pp pp_context fmt = fprintf fmt "@[<2>%s:@ %a@]" msg (pp pp_context) e | Group es -> let max_errors = 5 in - let es_trunc = Decoders_util.My_list.take max_errors es in + let es_trunc = U.My_list.take max_errors es in let not_shown = List.length es - max_errors in fprintf fmt @@ -38,3 +40,11 @@ let rec pp pp_context fmt = let map_tag f = function Tag (s, e) -> Tag (f s, e) | e -> e + +let rec map_context f = function + | E { msg; context } -> + E { msg; context = U.My_opt.map f context } + | Tag (s, e) -> + Tag (s, map_context f e) + | Group es -> + Group (U.My_list.map (map_context f) es) diff --git a/src/error.mli b/src/error.mli index 9b29f9c..d33730f 100644 --- a/src/error.mli +++ b/src/error.mli @@ -12,3 +12,5 @@ val tag_group : string -> 'a t list -> 'a t val pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit val map_tag : (string -> string) -> 'a t -> 'a t + +val map_context : ('a -> 'b) -> 'a t -> 'b t