-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6883 from smores56/new-builder-syntax
Implement new builder syntax alongside old one
- Loading branch information
Showing
20 changed files
with
1,226 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.