Reference · reference

fs module

3 min read · 2026-09-02 · config-weave 0.1.0

use fs gives a script file input and output. It replaces the standalone fs module of wscript-std, which config-weave does not register. Every function that can fail returns Result[…, string], where the error string names the path and the operating system error. The predicates exists, is_file and is_dir return a plain bool. Path strings are passed through to the operating system unchanged; use the path module to build them.

read and read_bytes

rust
fs::read(path: string) -> Result[string, string]
fs::read_bytes(path: string) -> Result[List[int], string]

read returns the file as text. read_bytes returns the raw bytes as a list of integers.

write, write_bytes and append

rust
fs::write(path: string, content: string) -> Result[unit, string]
fs::write_bytes(path: string, bytes: List[int]) -> Result[unit, string]
fs::append(path: string, content: string) -> Result[unit, string]

write and write_bytes replace the file's contents. append adds text to the end of the file and creates the file when it is absent.

copy and move

rust
fs::copy(from: string, to: string) -> Result[unit, string]
fs::move(from: string, to: string) -> Result[unit, string]

copy copies one file. move renames a file or a directory.

delete and delete_dir

rust
fs::delete(path: string) -> Result[unit, string]
fs::delete_dir(path: string) -> Result[unit, string]

delete removes a file or a symlink. delete_dir removes a directory and everything in it.

mkdir

rust
fs::mkdir(path: string) -> Result[unit, string]

Creates a directory, including any missing parents.

exists, is_file and is_dir

rust
fs::exists(path: string) -> bool
fs::is_file(path: string) -> bool
fs::is_dir(path: string) -> bool

Each predicate answers one question about a path. A path that cannot be read answers false.

list_dir

rust
fs::list_dir(path: string) -> Result[List[string], string]

Returns the names of the entries in a directory, sorted.

metadata

rust
fs::metadata(path: string) -> Result[Value, string]

Returns a map describing the path. Read it with the accessors of the Value type.

KeyTypeMeaning
sizeintsize in bytes
modifiedintmodification time, seconds since the Unix epoch
readonlyboolthe read-only attribute
is_fileboolthe path is a regular file
is_dirboolthe path is a directory
is_symlinkboolthe path is a symlink
modeintUnix permission bits, 0 on other platforms

glob

rust
fs::glob(pattern: string) -> Result[List[string], string]

Expands a glob pattern to the matching paths, sorted.

temp_file and temp_dir

rust
fs::temp_file() -> Result[string, string]
fs::temp_dir() -> Result[string, string]

Each creates a fresh temporary file or directory and returns its path.

rust
fs::symlink(target: string, link: string) -> Result[unit, string]
fs::read_link(path: string) -> Result[string, string]

symlink creates a link at link that points to target. read_link returns the target a symlink points to.

rust
use fs
use hash

fn check(props: Value) -> Result[CheckResult, string] {
    let path = "/etc/app/app.conf"
    if !fs::is_file(path) { return Ok(CheckResult::NotConfigured) }
    let current = hash::sha256_file(path)?
    let wanted = hash::sha256(props.get("content").unwrap().as_string().unwrap())
    if current == wanted { Ok(CheckResult::AlreadyConfigured) } else { Ok(CheckResult::NotConfigured) }
}