intuicio_frontend_simpleton/library/
fs.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{library::bytes::Bytes, Array, Reference, Text};
use intuicio_core::registry::Registry;
use intuicio_derive::intuicio_function;
use std::path::{Path, PathBuf};

#[intuicio_function(module_name = "fs", use_registry)]
pub fn exists(registry: &Registry, path: Reference) -> Reference {
    Reference::new_boolean(
        Path::new(path.read::<Text>().unwrap().as_str()).exists(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn is_file(registry: &Registry, path: Reference) -> Reference {
    Reference::new_boolean(
        Path::new(path.read::<Text>().unwrap().as_str()).is_file(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn is_dir(registry: &Registry, path: Reference) -> Reference {
    Reference::new_boolean(
        Path::new(path.read::<Text>().unwrap().as_str()).is_dir(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn make_dir(registry: &Registry, path: Reference) -> Reference {
    Reference::new_boolean(
        std::fs::create_dir_all(path.read::<Text>().unwrap().as_str()).is_ok(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn scan_dir(registry: &Registry, path: Reference) -> Reference {
    let result = std::fs::read_dir(path.read::<Text>().unwrap().as_str())
        .unwrap()
        .map(|entry| {
            Reference::new_text(
                entry.unwrap().path().to_string_lossy().as_ref().to_owned(),
                registry,
            )
        })
        .collect::<Array>();
    Reference::new_array(result, registry)
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn read_file(registry: &Registry, path: Reference) -> Reference {
    Reference::new(
        Bytes::new_raw(std::fs::read(path.read::<Text>().unwrap().as_str()).unwrap()),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn write_file(registry: &Registry, path: Reference, bytes: Reference) -> Reference {
    Reference::new_boolean(
        std::fs::write(
            path.read::<Text>().unwrap().as_str(),
            bytes.read::<Bytes>().unwrap().get_ref(),
        )
        .is_ok(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn delete(registry: &Registry, path: Reference) -> Reference {
    let path = PathBuf::from(path.read::<Text>().unwrap().as_str());
    if path.is_file() {
        Reference::new_boolean(std::fs::remove_file(path).is_ok(), registry)
    } else if path.is_dir() {
        Reference::new_boolean(std::fs::remove_dir_all(path).is_ok(), registry)
    } else {
        Reference::new_boolean(false, registry)
    }
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn get_current_dir(registry: &Registry) -> Reference {
    Reference::new_text(
        std::env::current_dir()
            .unwrap()
            .to_string_lossy()
            .to_string(),
        registry,
    )
}

#[intuicio_function(module_name = "fs", use_registry)]
pub fn set_current_dir(registry: &Registry, path: Reference) -> Reference {
    Reference::new_boolean(
        std::env::set_current_dir(path.read::<Text>().unwrap().as_str()).is_ok(),
        registry,
    )
}

pub fn install(registry: &mut Registry) {
    registry.add_function(exists::define_function(registry));
    registry.add_function(is_file::define_function(registry));
    registry.add_function(is_dir::define_function(registry));
    registry.add_function(make_dir::define_function(registry));
    registry.add_function(scan_dir::define_function(registry));
    registry.add_function(read_file::define_function(registry));
    registry.add_function(write_file::define_function(registry));
    registry.add_function(delete::define_function(registry));
    registry.add_function(get_current_dir::define_function(registry));
    registry.add_function(set_current_dir::define_function(registry));
}