Std.FileSystem
FileNotFound
Section titled “FileNotFound”error FileNotFound(path: String)Nothing is at path, or its parent directory does not exist.
path: String
FileAccessDenied
Section titled “FileAccessDenied”error FileAccessDenied(path: String)The host does not allow the operation on path.
path: String
NotADirectory
Section titled “NotADirectory”error NotADirectory(path: String)path, or a path in the middle of it, is a file where a directory is needed.
path: String
IsADirectory
Section titled “IsADirectory”error IsADirectory(path: String)path is a directory where a file is needed.
path: String
DirectoryNotEmpty
Section titled “DirectoryNotEmpty”error DirectoryNotEmpty(path: String)The directory at path cannot be removed because it has entries.
path: String
InvalidText
Section titled “InvalidText”error InvalidText(path: String)The file is not valid UTF-8. It is never read with replacement characters, so that writing the text back does not change the file.
path: String
FileSystemFailure
Section titled “FileSystemFailure”error FileSystemFailure(path: String, message: String)Any other error of the host. message is the host’s description; on the WASM target it is
empty because the host provides no message.
path: Stringmessage: String
FileSystem
Section titled “FileSystem”effect FileSystemReads and writes files as text (UTF-8). Paths stay as String and are not normalized.
-
suspend fn read_text(path: String) -> String fails FileNotFound | FileAccessDenied | IsADirectory | InvalidText | FileSystemFailureThe text of the file. InvalidText if it is not valid UTF-8; a leading BOM is kept.
-
suspend fn write_text(path: String, text: String) -> () fails FileNotFound | FileAccessDenied | IsADirectory | FileSystemFailureCreates the file or replaces its text. FileNotFound if the parent directory does not exist.
-
suspend fn append_text(path: String, text: String) -> () fails FileNotFound | FileAccessDenied | IsADirectory | FileSystemFailureCreates the file if it does not exist. FileNotFound if the parent directory does not exist.
-
suspend fn exists(path: String) -> BoolFalse if the path cannot be seen, including for lack of permission.
-
suspend fn is_directory(path: String) -> BoolFalse if the path is not a directory or cannot be seen.
-
suspend fn list_directory(path: String) -> List[String] fails FileNotFound | FileAccessDenied | NotADirectory | FileSystemFailureThe names (not paths) of the entries, in name order.
-
suspend fn create_directory(path: String) -> () fails FileAccessDenied | NotADirectory | FileSystemFailureCreates the parents too. Does nothing if the directory exists. NotADirectory if a file is at the path or in the middle of it.
-
suspend fn remove(path: String) -> () fails FileNotFound | FileAccessDenied | DirectoryNotEmpty | FileSystemFailureRemoves a file or an empty directory.
SystemFileSystem
Section titled “SystemFileSystem”handler SystemFileSystem: FileSystemuses { Async, ProcessHost }The host’s file system (JS node:fs/promises). Stateless.
TestFiles
Section titled “TestFiles”type TestFiles(files: List[(String, String)], directories: List[String])The files and directories of TestFileSystem. files pairs each path with its text, in path
order. A directory exists if it is in directories, or is an ancestor of a path in files or
directories. Paths are compared as strings, so a/b and ./a/b are different paths.
files: List[(String, String)]directories: List[String]
TestFileSystem
Section titled “TestFileSystem”handler TestFileSystem: FileSystemuses { State[TestFiles] }The test handler. It keeps the files in State[TestFiles] and resumes at once, so use it
inside State.run:
start = TestFiles(files: [("notes.txt", "hi")], directories: [])(text, _) = State.run(start, || with TestFileSystem { fs <- FileSystem fs.read_text("notes.txt")})The errors follow the rules of SystemFileSystem. It never fails with FileAccessDenied, InvalidText or FileSystemFailure; to test those, write a small handler.