tempfile

    Temporary file and directory management for MoonBit

    tempfile
    temporary
    filesystem
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    7 months ago
    Downloads
    150K

    Dependencies

    #mizchi/tempfile

    Temporary file and directory management for MoonBit. Similar to Rust's tempfile crate.

    #Features

    • Create temporary files with automatic cleanup
    • Create temporary directories with recursive cleanup
    • Builder pattern for customization (prefix, suffix, temp directory)
    • Cross-platform support (js, wasm, native)

    #Installation

    Add to your moon.mod.json:

    { "deps": { "mizchi/tempfile": "0.1.0" } }

    #Quick Start

    fn main {
    // Create a temporary file
    let tmp = @tempfile.tempfile()

    tmp.write_string("Hello, tempfile!")
    println(tmp.path()) // /tmp/.tmpXXXXXXXXXX

    tmp.cleanup()

    // Create a temporary directory
    let tmpdir = @tempfile.tempdir()
    println(tmpdir.path()) // /tmp/.tmpXXXXXXXXXX
    tmpdir.cleanup()
    }

    #API

    See src/README.mbt.md for detailed API documentation with executable examples.

    #Types

    • NamedTempFile - A temporary file with path access
    • TempDir - A temporary directory with recursive cleanup
    • Builder - Builder for customizing temporary file/directory creation

    #Functions

    • tempfile() -> NamedTempFile - Create a temporary file
    • tempdir() -> TempDir - Create a temporary directory

    #Requirements

    • Dependencies: moonbitlang/x

    #License

    Apache-2.0

    #mizchi/tempfile

    Temporary file and directory management for MoonBit. Similar to Rust's tempfile crate.

    #Features

    • Create temporary files with automatic cleanup
    • Create temporary directories with recursive cleanup
    • Builder pattern for customization (prefix, suffix, temp directory)
    • Cross-platform support (js, wasm, native)

    #Installation

    Add to your moon.mod.json:

    { "deps": { "mizchi/tempfile": "0.1.0" } }

    #Usage

    #Create a temporary file

    ///|
    fn example() {
    let tmp = @tempfile.tempfile()
    tmp.write_string("Hello!")
    println(tmp.path()) // /tmp/.tmpXXXXXXXXXX
    tmp.cleanup()
    }

    #Create a temporary directory

    ///|
    fn example_dir() {
    let tmpdir = @tempfile.tempdir()

    // Create files inside
    @fs.write_string_to_file(tmpdir.path() + "/data.txt", "content")

    // Recursive cleanup for non-empty directories
    tmpdir.cleanup_recursive()
    }

    #Builder pattern

    ///|
    fn custom_tempfile() {
    let tmp = @tempfile.Builder::new()
    .prefix("myapp_")
    .suffix(".log")
    .temp_dir("/var/tmp")
    .create_temp_file()
    println(tmp.path()) // /var/tmp/myapp_XXXXXXXXXX.log
    tmp.cleanup()
    }

    #API

    #Functions

    ///|
    test {
    // Builder creates customizable temp file/dir
    let builder = Builder::new()
    inspect(builder.prefix, content=".tmp")
    inspect(builder.suffix, content="")
    inspect(builder.random_len, content="10")
    inspect(builder.temp_dir, content="/tmp")
    }

    ///|
    test {
    // Builder methods return new Builder (immutable)
    let b1 = Builder::new()
    let b2 = b1.prefix("test_")
    inspect(b1.prefix, content=".tmp")
    inspect(b2.prefix, content="test_")
    }

    ///|
    test {
    // Builder can chain methods
    let builder = Builder::new()
    .prefix("app_")
    .suffix(".tmp")
    .random_len(8)
    .temp_dir("/var/tmp")
    inspect(builder.prefix, content="app_")
    inspect(builder.suffix, content=".tmp")
    inspect(builder.random_len, content="8")
    inspect(builder.temp_dir, content="/var/tmp")
    }

    #Types

    • NamedTempFile - A temporary file with path access
    • TempDir - A temporary directory with recursive cleanup
    • Builder - Builder for customizing temporary file/directory creation

    #NamedTempFile Methods

    • path() -> String - Get the file path
    • write_string(String) - Write string content
    • write_bytes(Bytes) - Write bytes content
    • read_string() -> String - Read as string
    • read_bytes() -> Bytes - Read as bytes
    • cleanup() - Delete the file
    • keep() -> String - Keep without deleting, returns path

    #TempDir Methods

    • path() -> String - Get the directory path
    • cleanup() - Delete empty directory
    • cleanup_recursive() - Recursively delete directory and contents
    • keep() -> String - Keep without deleting, returns path

    #Builder Methods

    • Builder::new() -> Builder - Create a new builder
    • prefix(String) -> Builder - Set name prefix (default: ".tmp")
    • suffix(String) -> Builder - Set name suffix (default: "")
    • random_len(Int) -> Builder - Set random portion length (default: 10)
    • temp_dir(String) -> Builder - Set temp directory (default: "/tmp")
    • create_temp_file() -> NamedTempFile - Create a temporary file
    • create_temp_dir() -> TempDir - Create a temporary directory

    #Requirements

    • Dependencies: moonbitlang/x

    #License

    Apache-2.0

    Builder

    pub struct Builder {
    prefix : String
    suffix : String
    random_len : Int
    temp_dir : String
    }

    Builder for creating temporary files and directories with custom options.

    Builder::create_temp_dir

    fn Builder::create_temp_dir(self : Builder) -> TempDir raise
    IOError

    Create a temporary directory using the builder options.

    Builder::create_temp_file

    fn Builder::create_temp_file(self : Builder) -> NamedTempFile raise
    IOError

    Create a named temporary file using the builder options.

    Builder::new

    fn Builder::new() -> Builder

    Create a new Builder with default options.

    Builder::prefix

    fn Builder::prefix(self : Builder, prefix : String) -> Builder

    Set the prefix for the temporary file/directory name.

    Builder::random_len

    fn Builder::random_len(self : Builder, len : Int) -> Builder

    Set the length of the random portion of the name.

    Builder::suffix

    fn Builder::suffix(self : Builder, suffix : String) -> Builder

    Set the suffix for the temporary file/directory name.

    Builder::temp_dir

    fn Builder::temp_dir(self : Builder, dir : String) -> Builder

    Set the directory where temporary files/directories will be created.

    NamedTempFile

    pub struct NamedTempFile {
    path : String
    }

    A named temporary file that can be accessed via its path. Call cleanup() to delete the file when done.

    NamedTempFile::cleanup

    Delete the file from the filesystem.

    NamedTempFile::keep

    fn NamedTempFile::keep(self : NamedTempFile) -> String

    Keep the file without deleting it. Returns the path.

    NamedTempFile::path

    fn NamedTempFile::path(self : NamedTempFile) -> String

    Get the path of the temporary file.

    NamedTempFile::read_bytes

    fn NamedTempFile::read_bytes(self : NamedTempFile) -> Bytes raise
    IOError

    Read the file content as bytes.

    NamedTempFile::read_string

    fn NamedTempFile::read_string(self : NamedTempFile) -> String raise
    IOError

    Read the file content as string.

    NamedTempFile::write_bytes

    fn NamedTempFile::write_bytes(self : NamedTempFile, content : Bytes) -> Unit raise
    IOError

    Write bytes content to the temporary file.

    NamedTempFile::write_string

    fn NamedTempFile::write_string(self : NamedTempFile, content : String) -> Unit raise
    IOError

    Write string content to the temporary file.

    TempDir

    pub struct TempDir {
    path : String
    }

    A temporary directory that can be accessed via its path. Call cleanup() to delete the directory and its contents when done.

    TempDir::cleanup

    fn TempDir::cleanup(self : TempDir) -> Unit raise
    IOError

    Delete the directory. Note: directory must be empty. Use cleanup_recursive for non-empty directories.

    TempDir::cleanup_recursive

    fn TempDir::cleanup_recursive(self : TempDir) -> Unit raise
    IOError

    Recursively delete the directory and all its contents.

    TempDir::keep

    fn TempDir::keep(self : TempDir) -> String

    Keep the directory without deleting it. Returns the path.

    TempDir::path

    fn TempDir::path(self : TempDir) -> String

    Get the path of the temporary directory.

    tempdir

    Create a temporary directory with default options.

    tempfile

    Create a named temporary file with default options.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io