Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new builder syntax alongside old one #6883

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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