Skip to content

Commit

Permalink
fix: post rebase: 'e type param is fixed to 'value Error.t
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjbray committed Jun 15, 2022
1 parent 3cefe37 commit 76d3edf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 =
Expand All @@ -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

Expand All @@ -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 =
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/error.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module U = Decoders_util

type 'a t =
| E of
{ msg : string
Expand Down Expand Up @@ -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
Expand All @@ -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)
2 changes: 2 additions & 0 deletions src/error.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 76d3edf

Please sign in to comment.