Skip to content

Std.FileSystem

error FileNotFound(path: String)

Nothing is at path, or its parent directory does not exist.

  • path: String
error FileAccessDenied(path: String)

The host does not allow the operation on path.

  • path: String
error NotADirectory(path: String)

path, or a path in the middle of it, is a file where a directory is needed.

  • path: String
error IsADirectory(path: String)

path is a directory where a file is needed.

  • path: String
error DirectoryNotEmpty(path: String)

The directory at path cannot be removed because it has entries.

  • path: String
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
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: String
  • message: String
effect FileSystem

Reads 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 | FileSystemFailure

    The 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 | FileSystemFailure

    Creates 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 | FileSystemFailure

    Creates the file if it does not exist. FileNotFound if the parent directory does not exist.

  • suspend fn exists(path: String) -> Bool

    False if the path cannot be seen, including for lack of permission.

  • suspend fn is_directory(path: String) -> Bool

    False if the path is not a directory or cannot be seen.

  • suspend fn list_directory(path: String) -> List[String] fails FileNotFound | FileAccessDenied | NotADirectory | FileSystemFailure

    The names (not paths) of the entries, in name order.

  • suspend fn create_directory(path: String) -> () fails FileAccessDenied | NotADirectory | FileSystemFailure

    Creates 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 | FileSystemFailure

    Removes a file or an empty directory.

handler SystemFileSystem: FileSystem
uses { Async, ProcessHost }

The host’s file system (JS node:fs/promises). Stateless.

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]
handler TestFileSystem: FileSystem
uses { 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.