src/fmt/run.bend source
src/fmt/run.bend on the hub · documented module
# src/fmt/run: the interpreter for `bolt format`. It fills a World as the lint# does (src/lint.bend's loop: the same questions, answered by the same IO),# then carries out the plan src/fmt/plan.bend makes of it: each edit written# over its file, the lines printed, and the exit status. It decides nothing:# which files change and what they become are the planner's. That it writes# and prints faithfully is trusted, not proved, as with every interpreter# here; a file that cannot be opened or written ends the run at once, exit 1.# Effect: CPU event loop only.import Baseimport ../lint.bend as Lintimport ../status.bend as Statusimport ../lint/world.bend as Wimport ./plan.bend as Fp# a write that failed ends the rundef write.closed(r: Result<&1, &1, U32 & String, Unit>, path: String) -> IO(Unit): match r: case Fail{_e}: IO.die(Unit, 1, "bolt: cannot write " ++ path) case Done{_u}: IO.pure(Unit, Unit{})# the file closed, then the write's result readdef write.wrote(got: File & Result<&1, &1, U32 & String, Unit>, path: String) -> IO(Unit): (file, r) = got do IO<Unit>: File.close(file) write.closed(r, path)# the text written to an opened file; one that did not open ends the rundef write.opened(r: Result<&1, &1, U32 & String, File>, path: String, text: String) -> IO(Unit): match r: case Fail{_e}: IO.die(Unit, 1, "bolt: cannot write " ++ path) case Done{file}: do IO<Unit>: got : File & Result<&1, &1, U32 & String, Unit> <- File.write(file, text) write.wrote(got, path)# a file's text replaceddef write(+path: String, text: String) -> IO(Unit): # noqa: L001 IO driver do IO<Unit>: r : Result<&1, &1, U32 & String, File> <- File.open(path, "w") write.opened(r, path, text)# each edit written, in orderdef write.all(es: List<&2, Fp.Edit>) -> IO(Unit): match es: case Nil{}: IO.pure(Unit, Unit{}) case Con{Fp.Edit{path, text}, rest}: do IO<Unit>: write(path, text) write.all(rest)# a plan, carried out: its edits written, its lines printed, then its exit# statusdef exec(pl: Fp.Plan) -> IO(Unit): # noqa: L001 IO driver Fp.Plan{edits, lines, exit} = pl do IO<Unit>: write.all(edits) Lint.print_all(lines) Status.stop(U32.is_gt(exit, 0))# `bolt format` over the files given, or the tree below; check: list the files# that would change and write nonedef run(check: Bool, paths: List<&2, String>) -> IO(Unit): # noqa: L001 IO driver do IO<Unit>: world : W.World <- Lint.fill(paths) exec(Fp.plan(check, world))