From cce8f3045f14aa9a2ab69852cd60f4f88550f722 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Thu, 23 Nov 2023 21:52:20 +0000 Subject: Bencode decoding string done... --- main.roc | 25 ++++++++++++++++++++----- 1 file 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" -- cgit v1.2.3-ZIG