koto_tempfile/
lib.rs

1//! A Koto language module for working with temporary files
2
3use koto_runtime::{
4    core_lib::io::{map_io_err, File},
5    unexpected_args, KMap,
6};
7use tempfile::NamedTempFile;
8
9pub fn make_module() -> KMap {
10    let result = KMap::with_type("temp_file");
11
12    result.add_fn("temp_file", {
13        |ctx| match ctx.args() {
14            [] => match NamedTempFile::new().map_err(map_io_err) {
15                Ok(file) => {
16                    let path = file.path().to_path_buf();
17                    Ok(File::system_file(file, path))
18                }
19                Err(e) => Err(e),
20            },
21            unexpected => unexpected_args("||", unexpected),
22        }
23    });
24
25    result
26}