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 c21c6d2
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 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 @@ -139,8 +138,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 +152,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 +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 c21c6d2

Please sign in to comment.