Skip to content

Commit

Permalink
Merge pull request #6883 from smores56/new-builder-syntax
Browse files Browse the repository at this point in the history
Implement new builder syntax alongside old one
  • Loading branch information
smores56 authored Jul 8, 2024
2 parents 48048ad + 74f05ec commit b9a17f4
Show file tree
Hide file tree
Showing 20 changed files with 1,226 additions and 214 deletions.
22 changes: 22 additions & 0 deletions crates/cli/tests/cli/combine-tasks.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" }

import pf.Stdout
import pf.Task exposing [Task]

main =
multipleIn =
{ sequential <-
a: Task.ok 123,
b: Task.ok "abc",
c: Task.ok [123],
d: Task.ok ["abc"],
e: Task.ok (Dict.single "a" "b"),
}!

Stdout.line! "For multiple tasks: $(Inspect.toStr multipleIn)"

sequential : Task a err, Task b err, (a, b -> c) -> Task c err
sequential = \firstTask, secondTask, mapper ->
first = firstTask!
second = secondTask!
Task.ok (mapper first second)
87 changes: 87 additions & 0 deletions crates/cli/tests/cli/parse-args.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
}

import pf.Arg
import pf.Stdout
import pf.Task exposing [Task]

main =
file = strParam { name: "file" }
argParser =
{ cliBuild <-
file,
count: numParam { name: "count" },
doubled: numParam { name: "doubled" }
|> cliMap \d -> d * 2,
}

args = ["parse-args", "file.txt", "5", "7"]
when argParser |> parseArgs args is
Ok data -> Stdout.line "Success: $(Inspect.toStr data)"
Err (FailedToParse message) -> Stdout.line "Failed: $(message)"

ArgParseErr : [NoMoreArgs, InvalidParam ParamConfig]

ParamConfig : {
name : Str,
type : [Num, Str],
}

ArgParser out : {
params : List ParamConfig,
parser : List Str -> Result (out, List Str) ArgParseErr,
}

strParam : { name : Str } -> ArgParser Str
strParam = \{ name } ->
parser = \args ->
when args is
[] -> Err NoMoreArgs
[first, .. as rest] -> Ok (first, rest)

{ params: [{ name, type: Str }], parser }

numParam : { name : Str } -> ArgParser U64
numParam = \{ name } ->
param = { name, type: Num }
parser = \args ->
when args is
[] -> Err NoMoreArgs
[first, .. as rest] ->
when Str.toU64 first is
Ok num -> Ok (num, rest)
Err InvalidNumStr -> Err (InvalidParam param)

{ params: [param], parser }

cliMap : ArgParser a, (a -> b) -> ArgParser b
cliMap = \{ params, parser }, mapper -> {
params,
parser: \args ->
(data, afterData) <- parser args
|> Result.try

Ok (mapper data, afterData),
}

cliBuild : ArgParser a, ArgParser b, (a, b -> c) -> ArgParser c
cliBuild = \firstWeaver, secondWeaver, combine ->
allParams = List.concat firstWeaver.params secondWeaver.params
combinedParser = \args ->
(firstValue, afterFirst) <- firstWeaver.parser args
|> Result.try
(secondValue, afterSecond) <- secondWeaver.parser afterFirst
|> Result.try

Ok (combine firstValue secondValue, afterSecond)

{ params: allParams, parser: combinedParser }

parseArgs : ArgParser a, List Str -> Result a [FailedToParse Str]
parseArgs = \{ params: _, parser }, args ->
when parser (List.dropFirst args 1) is
Ok (data, []) -> Ok data
Ok (_data, extraArgs) -> Err (FailedToParse "Got $(List.len extraArgs |> Inspect.toStr) extra args")
Err NoMoreArgs -> Err (FailedToParse "I needed more args")
Err (InvalidParam param) -> Err (FailedToParse "Parameter '$(param.name)' needed a $(Inspect.toStr param.type)")
32 changes: 32 additions & 0 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,38 @@ mod cli_run {
)
}

#[test]
#[serial(cli_platform)]
#[cfg_attr(windows, ignore)]
fn combine_tasks_with_record_builder() {
test_roc_app(
"crates/cli/tests/cli",
"combine-tasks.roc",
&[],
&[],
&[],
"For multiple tasks: {a: 123, b: \"abc\", c: [123], d: [\"abc\"], e: {\"a\": \"b\"}}\n",
UseValgrind::No,
TestCliCommands::Run,
)
}

#[test]
#[serial(cli_platform)]
#[cfg_attr(windows, ignore)]
fn parse_args_with_record_builder() {
test_roc_app(
"crates/cli/tests/cli",
"parse-args.roc",
&[],
&[],
&[],
"Success: {count: 5, doubled: 14, file: \"file.txt\"}\n",
UseValgrind::No,
TestCliCommands::Run,
)
}

#[test]
#[serial(cli_platform)]
#[cfg_attr(windows, ignore)]
Expand Down
Loading

0 comments on commit b9a17f4

Please sign in to comment.