src/step.bend
ezsys@0.1.0.0 · 61 lines · 9 definitions
1
2
3
4import Base
5
6
7type Read is Type:
8 ReadMore{text: String}
9 ReadStop{answer: Result<&1, &1, U32 & String, String>}
10
11
12type Save is Type:
13 SaveMove{}
14 SaveStop{error: U32 & String}
15
16
17def read.chunk(size: U32) -> U32:
18 U32.max(size, 65536)
19
20
21def read.more(empty: Bool, text: String, got: String) -> Read:
22 match empty:
23 case True{}:
24 ReadStop{Done{got}}
25 case False{}:
26 ReadMore{got ++ text}
27
28
29def read.step(res: Result<&1, &1, U32 & String, String>, got: String) -> Read:
30 match res:
31 case Fail{error}:
32 ReadStop{Fail{error}}
33 case Done{+text}:
34 read.more(String.is_empty(text), text, got)
35
36
37def read.spent() -> Result<&1, &1, U32 & String, String>:
38 Fail{(27, "ezsys: file.read: the file did not end")}
39
40
41def save.tmp(path: String) -> String:
42 path ++ ".tmp"
43
44
45def save.step(res: Result<&1, &1, U32 & String, Unit>) -> Save:
46 match res:
47 case Fail{error}:
48 SaveStop{error}
49 case Done{_u}:
50 SaveMove{}
51
52
53def env.pick(
54 res: Result<&1, &1, U32 & String, String>,
55 fallback: String
56) -> String:
57 match res:
58 case Fail{_error}:
59 fallback
60 case Done{value}:
61 value