aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.roc25
1 files changed, 20 insertions, 5 deletions
diff --git a/main.roc b/main.roc
index 94139c4..033ff1d 100644
--- a/main.roc
+++ b/main.roc
@@ -6,14 +6,29 @@ app "roctorrent"
bDecodeStr: List U8 -> Result (List U8) [Malformatted]
bDecodeStr = \i ->
- List.splitFirst i ':'
- |> Result.mapErr \_err -> Malformatted
- |> Result.map \{before, } -> before
+ when (List.splitFirst i ':') is
+ Ok {before, after} ->
+ when (Str.fromUtf8 before) is
+ Ok ls ->
+ when (Str.toU32 ls) is
+ Ok l ->
+ if Num.toU32 (List.len after) == l then
+ Ok after
+ else
+ Err Malformatted
+ Err _err -> Err Malformatted
+ Err _err -> Err Malformatted
+ Err _err -> Err Malformatted
expect
- res = bDecodeStr (Str.toUtf8 "foo")
- res == Err Malformatted
+ res = bDecodeStr (Str.toUtf8 "3:foo")
+ res == Ok (Str.toUtf8 "foo")
+expect
+ errCases = ["foo", "1:foo", "4:foo", ":", "foo:", "1:", "::"]
+ List.all errCases \t ->
+ res = bDecodeStr (Str.toUtf8 t)
+ res == Err Malformatted
main =
Stdout.line "Hello, World"