File System

Types and functions for reading and writing files. A File value is obtained via file() and carries the path, format, and an internal reference. Capability groups — the fs/env split a sandbox profile grants via `fs#read` / `fs#update` / `env#read`, the host clock (`time#read`) and the embedding host's message channel (`host#read` / `host#append`); the functions below link in their signature.

pub struct EnvVariable {
  pub name: text,
  pub value: text,
}

One entry of the process environment, as env_variables() answers them: the variable's name and its value, both as plain text.

pub enum Format {
  TextFile,
  LittleEndian,
  BigEndian,
  Directory,
  NotExists,
}

Describes how a file is opened. TextFile: read or write as UTF-8 text (default). LittleEndian: binary mode, least-significant byte first. BigEndian: binary mode, most-significant byte first. Directory: represents a directory path. NotExists: no file or directory exist.

pub enum FileResult {
  Ok,
  NotFound,
  PermissionDenied,
  IsDirectory,
  NotEmpty,
  Other,
}

Result of a filesystem-mutating operation (delete, move, mkdir). Use ok() to get a simple boolean, or match on specific variants for detailed error handling. Every variant can actually be produced: Ok on success, NotFound for a missing / out-of-project path, PermissionDenied when the OS refuses access, IsDirectory when a file op targets a directory (e.g. deleting a directory with delete()), NotEmpty when rmdir() is given a directory that still holds entries, and Other for anything else. (--native and --interpret classify from the OS error; the wasm host reports only Ok / Other, and NotFound still comes from the loft-level existence check.)

pub fn ok(self: FileResult) -> boolean

True when the result is FileResult.Ok. Use to test a file op for success.

pub struct File {
  pub path: text,

  pub size: integer,

  // A fresh File reads as `NotExists` until `file()` has determined the real format for
  // the path, so this default is a placeholder rather than a claim about the path — it is
  // overwritten before any read.
  pub format: Format = NotExists,

  ref: i32?,

  current: integer,

  next: integer,
}

A handle to a filesystem entry. Fields: path (full path), size (file size in bytes), format (open mode), current (byte position after last read), next (byte position to read next).

pub fn content(self: const File) -> text?fs#read

Reads the entire file as a UTF-8 text value. Use for small configuration files or scripts.

pub fn lines(self: const File) -> vector<text> fs#read

Reads the file and splits it into lines. Strips trailing '\r' so CRLF files (Windows) and LF files (Unix) produce identical results. Use when processing line-by-line (logs, CSV, etc.).