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 32b9b8c
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions src/gladvent/internal/runners.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ pub fn runner_retrieval_error_to_snag(e: RunnerRetrievalErr) -> snag.Snag {
"function '"
<> f
<> "' has parameter(s) "
<> string.inspect(g)
<> {
g
|> list.map(type_to_string)
|> string.join(", ")
}
<> ", but should only have one parameter and it must be of type "
<> e
}
Expand All @@ -126,7 +130,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 @@ -139,8 +142,8 @@ pub fn get_day(

use runner_param_type <- result.try(case parse {
Error(Nil) -> Ok(string)
Ok(package_interface.Function(parameters: [param], return: return, ..)) if param.type_
== string -> Ok(return)
Ok(package_interface.Function(parameters: [param], return: return, ..)) if param.type_ == string ->
Ok(return)
_ ->
Error(ParseFunctionInvalid(
"parse function must have 1 input parameter of type String",
Expand All @@ -153,17 +156,16 @@ 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(#(
pt_1,
pt_2,
parse
|> result.replace(parse_function(module_name))
|> option.from_result,
|> result.replace(parse_function(module_name))
|> option.from_result,
))
}

Expand All @@ -185,7 +187,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 +221,33 @@ const string = package_interface.Named(
package: "",
parameters: [],
)

fn type_to_string(t: package_interface.Type) -> String {
let stringify_type_list = fn(elements) {
elements
|> list.map(type_to_string)
|> string.join(", ")
}

case t {
package_interface.Tuple(elements: elements) ->
"#(" <> stringify_type_list(elements) <> ")"
package_interface.Fn(parameters: parameters, return: return) ->
"fn("
<> stringify_type_list(elements)
<> ") -> "
<> 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 <> "(" <> stringify_type_list(elements) <> ")"
}
}
}

0 comments on commit 32b9b8c

Please sign in to comment.