Skip to content

Commit

Permalink
use new decode package for decoding gleam runtime errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Jul 8, 2024
1 parent 3248a42 commit ac2c857
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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" }
Expand Down
45 changes: 30 additions & 15 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import decode
import filepath
import gladvent/internal/cmd.{Ending, Endless}
import gladvent/internal/parse.{type Day}
Expand Down Expand Up @@ -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(
[
Expand Down

0 comments on commit ac2c857

Please sign in to comment.