1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! A Koto language module for working with temporary files

use koto_runtime::{
    core::io::{make_file_map, File},
    external_error, make_external_value, Value, ValueMap,
};

pub fn make_module() -> ValueMap {
    use Value::*;

    let mut result = ValueMap::new();

    result.add_fn("temp_path", {
        |_, _| match tempfile::NamedTempFile::new() {
            Ok(file) => match file.keep() {
                Ok((_temp_file, path)) => Ok(Str(path.to_string_lossy().as_ref().into())),
                Err(e) => external_error!("io.temp_file: Error while making temp path: {}", e),
            },
            Err(e) => external_error!("io.temp_file: Error while making temp path: {}", e),
        }
    });

    result.add_fn("temp_file", {
        move |_, _| {
            let (temp_file, path) = match tempfile::NamedTempFile::new() {
                Ok(file) => match file.keep() {
                    Ok((temp_file, path)) => (temp_file, path),
                    Err(e) => {
                        return external_error!(
                            "io.temp_file: Error while creating temp file: {}",
                            e,
                        );
                    }
                },
                Err(e) => {
                    return external_error!("io.temp_file: Error while creating temp file: {}", e);
                }
            };

            let mut file_map = make_file_map();

            file_map.insert(
                Value::ExternalDataId,
                make_external_value(File {
                    file: temp_file,
                    path,
                    temporary: true,
                }),
            );

            Ok(Map(file_map))
        }
    });

    result
}