Skip to content

Commit

Permalink
stringify types more nicely for error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Mar 6, 2024
1 parent 724f79f commit 6931206
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/actions/test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
description: erlang-otp version
required: false
default: "~> 26.2"
rebar3-version:
description: rebar3 version
required: false
default: "~> 3.18"

runs:
using: composite
Expand All @@ -19,6 +23,7 @@ runs:
with:
otp-version: ${{ inputs.erlang-version }}
gleam-version: ${{ inputs.gleam-version }}
rebar3-version: ${{ inputs.rebar3-version }}
- uses: actions/setup-node@v2
with:
node-version: ${{ inputs.node-version }}
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
erlang: ["~> 26.2"]
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/test
Expand Down
38 changes: 33 additions & 5 deletions src/gladvent/internal/runners.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn runner_retrieval_error_to_snag(e: RunnerRetrievalErr) -> snag.Snag {
"function '"
<> f
<> "' has parameter(s) "
<> string.inspect(g)
<> type_list_to_string(g)
<> ", but should only have one parameter and it must be of type "
<> e
}
Expand All @@ -126,7 +126,6 @@ pub fn get_day(
use <- snagify_error(with: runner_retrieval_error_to_snag)
let module_name =
"aoc_" <> int.to_string(year) <> "/day_" <> int.to_string(day)
// let module_atom = atom.create_from_string(to_erlang_module_name(module_name))

// get the module for the specified year + day
use module <- result.try(
Expand All @@ -153,9 +152,8 @@ pub fn get_day(
_,
runner_param_type,
)
// get pt_1

use pt_1 <- result.try(retrieve_runner("pt_1"))
// get pt_2
use pt_2 <- result.try(retrieve_runner("pt_2"))

Ok(#(
Expand Down Expand Up @@ -185,7 +183,7 @@ fn retrieve_runner(
},
return: Error(IncorrectInputParameters(
function: function_name,
expected: string.inspect(runner_param_type),
expected: type_to_string(runner_param_type),
got: list.map(pt_1.parameters, fn(p) { p.type_ }),
)),
)
Expand Down Expand Up @@ -219,3 +217,33 @@ const string = package_interface.Named(
package: "",
parameters: [],
)

fn type_to_string(t: package_interface.Type) -> String {
case t {
package_interface.Tuple(elements: elements) ->
"#(" <> type_list_to_string(elements) <> ")"
package_interface.Fn(parameters: parameters, return: return) ->
"fn("
<> type_list_to_string(parameters)
<> ") -> "
<> type_to_string(return)
package_interface.Variable(id: id) -> int.to_string(id)
package_interface.Named(
name: name,
package: _,
module: module,
parameters: parameters,
) ->
case parameters {
[] -> module <> "." <> name
_ ->
module <> "." <> name <> "(" <> type_list_to_string(parameters) <> ")"
}
}
}

fn type_list_to_string(lt: List(package_interface.Type)) -> String {
lt
|> list.map(type_to_string)
|> string.join(", ")
}

0 comments on commit 6931206

Please sign in to comment.