inspect.bend source
inspect.bend on the hub · documented module
# Read-only current-format journal replay, before crash recovery.import Baseimport ./model.bend as Modelimport bend-kit-bytes@0.3.3.1/bytes.bend as Bytesimport bend-kit-files@0.4.0.1/files.bend as Filesimport bend-kit-journal@0.4.0.0/pure.bend as Jtype Inspection<-A: Data> is Data: Inspection{state: Model.State<A>, tail: J.Tail, end: U32}def Out(-A: Type) -> Type: Result<&1, &1, U32 & String, A>def finish(~A: Data, r: J.Replay<Maybe<&2, Model.State<A>>, J.Cur>) -> Out(Inspection<A>): match r: case J.Replay{acc, J.Corrupt{why}, J.Cur{b, +pos}}: Fail{(22, "runner inspect: corrupt frame at offset " ++ U32.show(pos) ++ ": " ++ why)} case J.Replay{None{}, J.Clean{}, J.Cur{b, pos}}: Fail{(22, "runner inspect: undecodable record")} case J.Replay{None{}, J.Torn{}, J.Cur{b, pos}}: Fail{(22, "runner inspect: undecodable record before torn suffix")} case J.Replay{Some{s}, J.Clean{}, J.Cur{b, pos}}: Done{Inspection{s, J.Clean{}, pos}} case J.Replay{Some{s}, J.Torn{}, J.Cur{b, pos}}: Done{Inspection{s, J.Torn{}, pos}}def bytes(~A: Data, ~pdec: Bytes.Bytes -> Maybe<&2, A>, max: U32, b: Bytes.Bytes) -> Out(Inspection<A>): finish(~A, J.replay.bytes(~Maybe<&2, Model.State<A>>, ~(s => p => Model.replay.step(~A, ~pdec, s, p)), max, b, Some{Model.new(A)}))def read.more(over: Bool, f: File, +left: U32, b: Bytes.Bytes, acc: Bytes.Bytes, next: File -> U32 -> Bytes.Bytes -> IO(Out(Bytes.Bytes))) -> IO(Out(Bytes.Bytes)): match over: case True{}: do IO<Out(Bytes.Bytes)>: File.close(f) return Fail{(27, "runner inspect: file exceeds limit")} case False{}: Bytes.Bytes{+len, words} = b next(f, (left - len : U32), Bytes.append(acc, Bytes.Bytes{len, words}))def read.got(r: File & Out(Bytes.Bytes), +left: U32, acc: Bytes.Bytes, next: File -> U32 -> Bytes.Bytes -> IO(Out(Bytes.Bytes))) -> IO(Out(Bytes.Bytes)): match r: case (f, Fail{e}): do IO<Out(Bytes.Bytes)>: File.close(f) return Fail{e} case (f, Done{Bytes.Bytes{0, words}}): do IO<Out(Bytes.Bytes)>: File.close(f) return Done{acc} case (f, Done{Bytes.Bytes{+len, words}}): read.more(U32.is_gt(len, left), f, left, Bytes.Bytes{len, words}, acc, next)def read(n: Nat, f: File, +left: U32, acc: Bytes.Bytes) -> IO(Out(Bytes.Bytes)): match n: case 0n: do IO<Out(Bytes.Bytes)>: File.close(f) return Fail{(27, "runner inspect: file exceeds limit")} case 1n+p: do IO<Out(Bytes.Bytes)>: r : File & Out(Bytes.Bytes) <- Files.read_bytes(f, U32.min(1048576, Bool.pick(U32, U32.is_eq(left, 0), 1, left))) read.got(r, left, acc, f => left => b => read(p, f, left, b))def opened(x: Out(File), +limit: U32) -> IO(Out(Bytes.Bytes)): match x: case Fail{e}: IO.pure(Out(Bytes.Bytes), Fail{e}) case Done{f}: read(2n+U32.to_nat(limit), f, limit, Bytes.new(0))def file.got(~A: Data, ~pdec: Bytes.Bytes -> Maybe<&2, A>, max: U32, r: Out(Bytes.Bytes)) -> Out(Inspection<A>): match r: case Fail{e}: Fail{e} case Done{b}: bytes(~A, ~pdec, max, b)def file(~A: Data, ~pdec: Bytes.Bytes -> Maybe<&2, A>, path: String, max: U32, limit: U32) -> IO(Out(Inspection<A>)): do IO<Out(Inspection<A>)>: f : Out(File) <- File.open(path, "r") r : Out(Bytes.Bytes) <- opened(f, limit) return file.got(~A, ~pdec, max, r)