aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-11-23 21:52:20 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-11-23 21:52:20 +0000
commitcce8f3045f14aa9a2ab69852cd60f4f88550f722 (patch)
treef92223dd1dc08d114e80f842d06abacf0e0ab4c4
parent6e6a29bcda4c1360fa1d18a7dfdf6662e0686ab4 (diff)
downloadroctorrent-cce8f3045f14aa9a2ab69852cd60f4f88550f722.tar.gz
roctorrent-cce8f3045f14aa9a2ab69852cd60f4f88550f722.tar.bz2
roctorrent-cce8f3045f14aa9a2ab69852cd60f4f88550f722.tar.xz
roctorrent-cce8f3045f14aa9a2ab69852cd60f4f88550f722.zip
Bencode decoding string done...
-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"