aboutsummaryrefslogtreecommitdiff
path: root/main.roc
blob: c90531fc7c31665ee702b22da3beb96205fa30e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
app "roctorrent"
    packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br" }
    imports [pf.Stdout]
    provides [main] to pf


bDecodeStr: List U8 -> Result (List U8) [Malformatted]
bDecodeStr = \i ->
  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 "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

bDecodeNum: List U8 -> Result I64 [Malformatted]
bDecodeNum = \i ->
  when i is
    ['i', .., 'e'] -> 
      ii = List.sublist i {start: 1, len: ((List.len i) - 2)}
      when (Str.fromUtf8 ii) is
        Ok iii ->
          when (Str.toI64 iii) is
            Ok res -> Ok res
            Err _err -> Err Malformatted
        Err _err -> Err Malformatted
    _ -> Err Malformatted

expect 
  res = bDecodeNum (Str.toUtf8 "i32e")
  res == Ok 32i64

expect 
  errCases = [
    "", 
    "foo", 
    "i32", 
    "32e", 
    "ifooe", 
    "ie", 
    "i", 
    "e", 
    "i522222222222222222343423432434322e",
  ]
  List.all errCases \t -> 
    res = bDecodeNum (Str.toUtf8 t)
    res == Err Malformatted

main = 
  Stdout.line "Hello, World"