Temporary file and directory management for MoonBit
Dependencies
{
"deps": {
"mizchi/tempfile": "0.1.0"
}
}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!()
}{
"deps": {
"mizchi/tempfile": "0.1.0"
}
}///|
fn example() {
let tmp = @tempfile.tempfile()
tmp.write_string("Hello!")
println(tmp.path()) // /tmp/.tmpXXXXXXXXXX
tmp.cleanup()
}///|
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()
}///|
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()
}///|
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")
}pub struct Builder {
prefix : String
suffix : String
random_len : Int
temp_dir : String
}pub struct NamedTempFile {
path : String
}pub struct TempDir {
path : String
}Temporary file and directory management for MoonBit
Dependencies