|
183 | 183 | //// "grass" -> decode.success(Grass) |
184 | 184 | //// "electric" -> decode.success(Electric) |
185 | 185 | //// // Return a failing decoder for any other strings |
186 | | -//// _ -> decode.failure(Fire, "PocketMonsterType") |
| 186 | +//// _ -> decode.failure(Fire, expected: "PocketMonsterType") |
187 | 187 | //// } |
188 | 188 | //// } |
189 | 189 | //// |
@@ -611,7 +611,9 @@ fn run_dynamic_function( |
611 | 611 | ) -> #(t, List(DecodeError)) { |
612 | 612 | case f(data) { |
613 | 613 | Ok(data) -> #(data, []) |
614 | | - Error(zero) -> #(zero, [DecodeError(name, dynamic.classify(data), [])]) |
| 614 | + Error(placeholder) -> #(placeholder, [ |
| 615 | + DecodeError(name, dynamic.classify(data), []), |
| 616 | + ]) |
615 | 617 | } |
616 | 618 | } |
617 | 619 |
|
@@ -978,26 +980,37 @@ fn run_decoders( |
978 | 980 | } |
979 | 981 | } |
980 | 982 |
|
981 | | -/// Define a decoder that always fails. The parameter for this function is the |
982 | | -/// name of the type that has failed to decode. |
| 983 | +/// Define a decoder that always fails. |
983 | 984 | /// |
984 | | -/// When this decoder is used as part of a larger decoder, the zero value is |
985 | | -/// used as a placeholder so that the rest of the decoder can continue to run |
986 | | -/// and collect all decoding errors. |
| 985 | +/// The first parameter is a "placeholder" value, which is some default value that the |
| 986 | +/// decoder uses internally in place of the value that would have been produced |
| 987 | +/// if the decoder was successful. It doesn't matter what this value is, it is |
| 988 | +/// never returned by the decoder or shown to the user, so pick some arbitrary |
| 989 | +/// value. If it is an int you might pick `0`, if it is a list you might pick |
| 990 | +/// `[]`. |
987 | 991 | /// |
988 | | -pub fn failure(zero: a, expected: String) -> Decoder(a) { |
989 | | - Decoder(function: fn(d) { #(zero, decode_error(expected, d)) }) |
| 992 | +/// The second parameter is the name of the type that has failed to decode. |
| 993 | +/// |
| 994 | +/// ```gleam |
| 995 | +/// decode.failure("User", User(name: "", score: 0, tags: [])) |
| 996 | +/// ``` |
| 997 | +/// |
| 998 | +pub fn failure(placeholder: a, expected name: String) -> Decoder(a) { |
| 999 | + Decoder(function: fn(d) { #(placeholder, decode_error(name, d)) }) |
990 | 1000 | } |
991 | 1001 |
|
992 | 1002 | /// Create a decoder for a new data type from a decoding function. |
993 | 1003 | /// |
994 | 1004 | /// This function is used for new primitive types. For example, you might |
995 | 1005 | /// define a decoder for Erlang's pid type. |
996 | 1006 | /// |
997 | | -/// A default "zero" value is also required to make a decoder. When this |
998 | | -/// decoder is used as part of a larger decoder this zero value is used as |
999 | | -/// a placeholder so that the rest of the decoder can continue to run and |
1000 | | -/// collect all decoding errors. |
| 1007 | +/// A default "placeholder" value is also required to make a decoder. When this |
| 1008 | +/// decoder is used as part of a larger decoder this placeholder value is used |
| 1009 | +/// so that the rest of the decoder can continue to run and |
| 1010 | +/// collect all decoding errors. It doesn't matter what this value is, it is |
| 1011 | +/// never returned by the decoder or shown to the user, so pick some arbitrary |
| 1012 | +/// value. If it is an int you might pick `0`, if it is a list you might pick |
| 1013 | +/// `[]`. |
1001 | 1014 | /// |
1002 | 1015 | /// If you were to make a decoder for the `Int` type (rather than using the |
1003 | 1016 | /// build-in `Int` decoder) you would define it like so: |
@@ -1030,7 +1043,9 @@ pub fn new_primitive_decoder( |
1030 | 1043 | Decoder(function: fn(d) { |
1031 | 1044 | case decoding_function(d) { |
1032 | 1045 | Ok(t) -> #(t, []) |
1033 | | - Error(zero) -> #(zero, [DecodeError(name, dynamic.classify(d), [])]) |
| 1046 | + Error(placeholder) -> #(placeholder, [ |
| 1047 | + DecodeError(name, dynamic.classify(d), []), |
| 1048 | + ]) |
1034 | 1049 | } |
1035 | 1050 | }) |
1036 | 1051 | } |
|
0 commit comments