Skip to content

Commit

Permalink
wip use package interface
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Mar 3, 2024
1 parent 5bc3405 commit 122d4f1
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 123 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = "0.6.2"
repository = { type = "github", user = "TanklesXL", repo = "gladvent" }
description = "An Advent Of Code runner for gleam"
licences = ["Apache-2.0"]
internal_modules = ["gladvent/internal/*", "aoc_*"]
internal_modules = ["gladvent/internal/*"]
gleam = "~> 0.34 or ~> 1.0"

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages = [
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_package_interface", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "52A721BCA972C8099BB881195D821AAA64B9F2655BECC102165D5A1097731F01" },
{ name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "glearray", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "908154F695D330E06A37FAB2C04119E8F315D643206F8F32B6A6C14A8709FFF4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "glint", version = "0.16.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "61B7E85CBB0CCD2FD8A9C7AE06CA97A80BF6537716F34362A39DF9C74967BBBC" },
Expand All @@ -33,4 +33,4 @@ glint = { version = " ~> 0.16.0" }
shellout = { version = "~> 1.6" }
simplifile = { version = "~> 0.3" }
snag = { version = "~> 0.2" }
spinner = { version = "~> 1.1"}
spinner = { version = "~> 1.1" }
4 changes: 2 additions & 2 deletions src/gladvent/internal/cmd/new.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ fn do(ctx: Context) -> String {
}

const gleam_starter = "pub fn pt_1(input: String) {
todo
todo as \"part 1 not implemented\"
}
pub fn pt_2(input: String) {
todo
todo as \"part 2 not implemented\"
}
"

Expand Down
67 changes: 43 additions & 24 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import gladvent/internal/cmd.{Ending, Endless}
import glint
import glint/flag
import gleam
import gladvent/internal/runners.{type RunnerMap}
import gladvent/internal/runners
import gleam/dynamic.{type Dynamic}
import gleam/option.{type Option, None}
import gleam/package_interface

type AsyncResult =
gleam.Result(RunResult, String)

type RunErr {
FailedToReadInput(String)
FailedToParseInput(String)
FailedToGetRunner(snag.Snag)
Unregistered(Day)
Other(String)
}
Expand All @@ -41,12 +43,13 @@ type SolveResult =
fn run_err_to_snag(err: RunErr) -> Snag {
case err {
Unregistered(day) ->
"day" <> " " <> int.to_string(day) <> " " <> "unregistered"
FailedToReadInput(input_path) -> "failed to read input file: " <> input_path
FailedToParseInput(err) -> "failed to parse input: " <> err
Other(s) -> s
snag.new("day" <> " " <> int.to_string(day) <> " " <> "unregistered")
FailedToReadInput(input_path) ->
snag.new("failed to read input file: " <> input_path)
FailedToParseInput(err) -> snag.new("failed to parse input: " <> err)
FailedToGetRunner(s) -> snag.layer(s, "failed to get runner")
Other(s) -> snag.new(s)
}
|> snag.new
}

type Direction {
Expand All @@ -62,11 +65,15 @@ fn string_trim(s: String, dir: Direction, sub: String) -> String {
@external(erlang, "string", "trim")
fn do_trim(a: String, b: Direction, c: Charlist) -> String

fn do(year: Int, day: Day, runners: RunnerMap, allow_crash: Bool) -> RunResult {
fn do(
year: Int,
day: Day,
package: package_interface.Package,
allow_crash: Bool,
) -> RunResult {
use #(pt_1, pt_2, parse) <- result.try(
runners
|> map.get(day)
|> result.replace_error(Unregistered(day)),
runners.get_day(package, year, day)
|> result.map_error(FailedToGetRunner),
)

let input_path =
Expand Down Expand Up @@ -251,14 +258,18 @@ pub fn run_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
use runners <- result.then(runners.build_from_days_dir(year))
use allow_crash <- result.try(flag.get_bool(input.flags, allow_crash))
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)

use days <- result.then(parse.days(input.args))
use package <- result.then(
runners.pkg_interface()
|> snag.context("failed to generate package interface"),
)

days
|> cmd.exec(
timing(input.flags),
do(year, _, runners, allow_crash),
do(year, _, package, allow_crash),
collect_async(year, _),
)
|> Ok
Expand All @@ -272,15 +283,29 @@ pub fn run_command() -> glint.Command(Result(List(String))) {
pub fn run_all_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
use allow_crash <- result.then(flag.get_bool(input.flags, allow_crash))
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
use runners <- result.then(runners.build_from_days_dir(year))

runners
|> all_days
use package <- result.then(
runners.pkg_interface()
|> snag.context("failed to generate package interface"),
)

package.modules
|> map.keys
|> list.filter_map(fn(k) {
k
|> string.split_once("aoc_" <> int.to_string(year) <> "/day_")
|> result.try(fn(day) {
day.1
|> parse.day
|> result.replace_error(Nil)
})
})
|> list.sort(int.compare)
|> cmd.exec(
timing(input.flags),
do(year, _, runners, allow_crash),
do(year, _, package, allow_crash),
collect_async(year, _),
)
|> Ok
Expand All @@ -295,9 +320,3 @@ fn timing(flags: flag.Map) {
|> result.map(Ending)
|> result.unwrap(Endless)
}

fn all_days(runners) {
runners
|> map.keys()
|> list.sort(by: int.compare)
}
2 changes: 1 addition & 1 deletion src/gladvent/internal/file.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import gleam/result
pub type IODevice

@external(erlang, "gladvent_ffi", "open_file_exclusive")
pub fn open_file_exclusive(s s: String) -> Result(IODevice, FileError)
pub fn open_file_exclusive(s: String) -> Result(IODevice, FileError)

@external(erlang, "gladvent_ffi", "write")
fn do_write(a: IODevice, b: String) -> Result(Nil, FileError)
Expand Down
Loading

0 comments on commit 122d4f1

Please sign in to comment.