diff --git a/gleam.toml b/gleam.toml index eef183d..85c95f7 100644 --- a/gleam.toml +++ b/gleam.toml @@ -22,6 +22,7 @@ parallel_map = ">= 2.0.0 and < 3.0.0" filepath = ">= 1.0.0 and < 2.0.0" tom = ">= 1.0.0 and < 2.0.0" file_streams = ">= 0.6.3 and < 1.0.0" +decode = ">= 0.2.0 and < 1.0.0" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index a29e23e..0e5ff33 100644 --- a/manifest.toml +++ b/manifest.toml @@ -3,6 +3,7 @@ packages = [ { name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" }, + { name = "decode", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "965F517F67B8C172CA27A5C8E34C73733139E8C9E64736181B8C3179281F9793" }, { name = "file_streams", version = "0.6.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "EC0DA36A4A5A79EB1281DD21247F062AD64D171D53C06ED4B4F520B0BC77F710" }, { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, { name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" }, @@ -27,7 +28,8 @@ packages = [ [requirements] argv = { version = "~> 1.0" } -file_streams = { version = ">= 0.6.3 and < 1.0.0"} +decode = { version = ">= 0.2.0 and < 1.0.0"} +file_streams = { version = ">= 0.6.3 and < 1.0.0" } filepath = { version = ">= 1.0.0 and < 2.0.0" } gleam_erlang = { version = "~> 0.7" } gleam_json = { version = "~> 1.0" } diff --git a/src/gladvent/internal/cmd/run.gleam b/src/gladvent/internal/cmd/run.gleam index b36339f..88ed4d0 100644 --- a/src/gladvent/internal/cmd/run.gleam +++ b/src/gladvent/internal/cmd/run.gleam @@ -1,3 +1,4 @@ +import decode import filepath import gladvent/internal/cmd.{Ending, Endless} import gladvent/internal/parse.{type Day} @@ -132,24 +133,38 @@ type GleamErr { ) } -fn decode_gleam_err() { - dynamic.decode6( - GleamErr, - dynamic.field(atom.create_from_string("gleam_error"), atom.from_dynamic), - dynamic.field(atom.create_from_string("module"), dynamic.string), - dynamic.field(atom.create_from_string("function"), dynamic.string), - dynamic.field(atom.create_from_string("line"), dynamic.int), - dynamic.field(atom.create_from_string("message"), dynamic.string), - dynamic.any([ - dynamic.field( - atom.create_from_string("value"), - dynamic.optional(dynamic.dynamic), - ), - fn(_) { Ok(None) }, - ]), +fn decode_gleam_err(dyn: dynamic.Dynamic) { + decode.into({ + use gleam_error <- decode.parameter + use module <- decode.parameter + use function <- decode.parameter + use line <- decode.parameter + use message <- decode.parameter + use value <- decode.parameter + GleamErr(gleam_error, module, function, line, message, value) + }) + |> decode.field(atom.create_from_string("gleam_error"), { + use dyn <- decode.then(decode.dynamic) + case atom.from_dynamic(dyn) { + Ok(a) -> decode.into(a) + Error(e) -> + decode.fail("failed to decode gleam error: " <> string.inspect(e)) + } + }) + |> decode.field(atom.create_from_string("module"), decode.string) + |> decode.field(atom.create_from_string("function"), decode.string) + |> decode.field(atom.create_from_string("line"), decode.int) + |> decode.field(atom.create_from_string("message"), decode.string) + |> decode.field( + atom.create_from_string("value"), + decode.optional(decode.dynamic), ) + |> decode.from(dyn) + |> io.debug } +import gleam/io + fn gleam_err_to_string(g: GleamErr) -> String { string.join( [