Skip to content

Commit

Permalink
refactor: move error mapping close to decoder
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Gurianov <gurianov@gmail.com>
  • Loading branch information
Guria committed Sep 29, 2024
1 parent e6caf45 commit 90b5545
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/gleojson.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -506,24 +506,6 @@ pub fn feature_decoder(
named: "properties",
of: dynamic.optional(dynamic.dict(dynamic.string, dynamic.dynamic)),
)(dynamic_value)

let id_result =
dynamic.optional_field(named: "id", of: dynamic.dynamic)(
dynamic_value,
)

let geometry_result =
geometry_result
|> result.map_error(fn(_errs) {
[
dynamic.DecodeError(expected: "Geometry", found: "Invalid", path: [
"geometry",
]),
]
})

let properties_result =
properties_result
|> result.map_error(fn(_errs) {
[
dynamic.DecodeError(
Expand All @@ -535,13 +517,25 @@ pub fn feature_decoder(
})

let id_result =
id_result
dynamic.optional_field(named: "id", of: dynamic.dynamic)(
dynamic_value,
)
|> result.map_error(fn(_errs) {
[
dynamic.DecodeError(expected: "ID", found: "Invalid", path: ["id"]),
]
})

let geometry_result =
geometry_result
|> result.map_error(fn(_errs) {
[
dynamic.DecodeError(expected: "Geometry", found: "Invalid", path: [
"geometry",
]),
]
})

result.try(geometry_result, fn(geometry_opt) {
result.try(properties_result, fn(properties_opt) {
result.map(id_result, fn(id_opt) {
Expand Down

0 comments on commit 90b5545

Please sign in to comment.