~/bend-docscommunity

impl.bend source

impl.bend on the hub · documented module

# THIS MODULE IS PROJECT-AGNOSTIC. KEEP IT THAT WAY.# A started program that we talk to in lines, over pipes: send lines, then read lines until one# starts with a given text. It knows no protocol.import Baseimport bend-stdio-pipes@0.1.0.0/impl.bend as Stdio# A running program: its process id, the File that writes its stdin, the File that reads its stdout,# and the most bytes in one of its lines, with its line end.type PipeSession is Type:  PipeSession{pid: U32, input: File, out: File, line_cap: U32}# A line of the program, as PipeSession.ask sorts it.type Heard is Type:  Line{text: String}  Last{text: String}  Ended{}  Failed{error: U32 & String}def PipeSession.pid(session: PipeSession) -> PipeSession & U32:  match session:    case PipeSession{+pid, input, out, line_cap}:      (PipeSession{pid, input, out, line_cap}, pid)def PipeSession.started(  opened: Result<&1, &1, U32 & String, U32 & (File & File)>,  line_cap: U32) -> Result<&1, &1, U32 & String, PipeSession>:  match opened:    case Fail{error}:      Fail{error}    case Done{(pid, (input, out))}:      Done{PipeSession{pid, input, out, line_cap}}# Starts program with args. A longer line of it fails with E2BIG (7).def PipeSession.start(  program: String,  args: List<String>,  line_cap: U32) -> IO(Result<&1, &1, U32 & String, PipeSession>):  do IO<Result<&1, &1, U32 & String, PipeSession>>:    opened: Result<&1, &1, U32 & String, U32 & (File & File)> <- Stdio.open(program, args)    return PipeSession.started(opened, line_cap)# The code of the error that PipeSession.ask gives when the program ended before its answer:# EPIPE.def PipeSession.ended() -> U32:  32def Heard.read(  read: File & Result<&1, &1, U32 & String, Maybe<String>>,  +until: String) -> File  & Heard:  match read:    case (out, Fail{error}):      (out, Failed{error})    case (out, Done{None{}}):      (out, Ended{})    case (out, Done{Some{line}}):      +text = String.trim_end(line)      (out, Bool.pick(Heard, String.starts_with(text, until), Last{text}, Line{text}))# said holds the lines so far, the last first; fuel bounds the lines, far beyond any answer.def PipeSession.ask.go(  fuel: Nat,  at: File & Heard,  +until: String,  +line_cap: U32,  said: List<&2, String>) -> IO(File & Result<&1, &1, U32 & String, List<&2, String>>):  match fuel:    case 0n:      match at:        case (out, _):          IO.pure(            File & Result<&1, &1, U32 & String, List<&2, String>>,            (out, Fail{(5, "the program said too many lines before " ++ until)})          )    case 1n+rest:      match at:        case (out, Failed{error}):          IO.pure(File & Result<&1, &1, U32 & String, List<&2, String>>, (out, Fail{error}))        case (out, Ended{}):          IO.pure(            File & Result<&1, &1, U32 & String, List<&2, String>>,            (out, Fail{(PipeSession.ended(), "the program ended before it said " ++ until)})          )        case (out, Last{text}):          IO.pure(            File & Result<&1, &1, U32 & String, List<&2, String>>,            (out, Done{List.reverse(&2, String, text <> said)})          )        case (out, Line{text}):          do IO<File & Result<&1, &1, U32 & String, List<&2, String>>>:            read: File              & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap)            PipeSession.ask.go(rest, Heard.read(read, until), until, line_cap, text <> said)def PipeSession.asked(  pid: U32,  input: File,  line_cap: U32,  answer: File & Result<&1, &1, U32 & String, List<&2, String>>) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>):  match answer:    case (out, result):      IO.pure(        PipeSession & Result<&1, &1, U32 & String, List<&2, String>>,        (PipeSession{pid, input, out, line_cap}, result)      )def PipeSession.wrote(  pid: U32,  wrote: File & Result<&1, &1, U32 & String, Unit>,  out: File,  +line_cap: U32,  +until: String) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>):  match wrote:    case (input, Fail{error}):      IO.pure(        PipeSession & Result<&1, &1, U32 & String, List<&2, String>>,        (PipeSession{pid, input, out, line_cap}, Fail{error})      )    case (input, Done{_}):      do IO<PipeSession & Result<&1, &1, U32 & String, List<&2, String>>>:        read: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap)        answer: File & Result<&1, &1, U32 & String, List<&2, String>> <-          PipeSession.ask.go(4294967295n, Heard.read(read, until), until, line_cap, [])        PipeSession.asked(pid, input, line_cap, answer)# Sends lines, then answers the program's lines up to the first that starts with until.def PipeSession.ask(  session: PipeSession,  lines: List<String>,  +until: String) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>):  match session:    case PipeSession{pid, input, out, line_cap}:      do IO<PipeSession & Result<&1, &1, U32 & String, List<&2, String>>>:        wrote: File & Result<&1, &1, U32 & String, Unit> <- Stdio.write_lines(input, lines)        PipeSession.wrote(pid, wrote, out, line_cap, until)def PipeSession.end.go(  fuel: Nat,  read: File & Result<&1, &1, U32 & String, Maybe<String>>,  +line_cap: U32,  said: List<&2, String>) -> IO(Result<&1, &1, U32 & String, List<&2, String>>):  match fuel read:    case 0n Tuple{out, _}:      do IO<Result<&1, &1, U32 & String, List<&2, String>>>:        File.close(out)        return Fail{(5, "the program said too many lines after its input ended")}    case 1n+rest Tuple{out, Fail{error}}:      do IO<Result<&1, &1, U32 & String, List<&2, String>>>:        File.close(out)        return Fail{error}    case 1n+rest Tuple{out, Done{None{}}}:      do IO<Result<&1, &1, U32 & String, List<&2, String>>>:        File.close(out)        return Done{List.reverse(&2, String, said)}    case 1n+rest Tuple{out, Done{Some{line}}}:      do IO<Result<&1, &1, U32 & String, List<&2, String>>>:        next: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap)        PipeSession.end.go(rest, next, line_cap, String.trim_end(line) <> said)# Closes the program's stdin, then answers what it said until it ended.def PipeSession.end(session: PipeSession) -> IO(Result<&1, &1, U32 & String, List<&2, String>>):  match session:    case PipeSession{_, input, out, +line_cap}:      do IO<Result<&1, &1, U32 & String, List<&2, String>>>:        File.close(input)        read: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap)        PipeSession.end.go(4294967295n, read, line_cap, [])