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
extern crate dirs;
use std::io::Read;
use std::io::Write;
#[derive(Debug)]
pub struct FileSystem {
base_dir: String
}
impl FileSystem {
pub fn init() -> FileSystem {
let home_dir = dirs::home_dir();
let home_dir_string: String = home_dir.unwrap().to_str().unwrap().into();
FileSystem{base_dir: home_dir_string}
}
pub fn create_application(&self, _bytecode_wasm: &str) -> String {
let uuid = uuid::Uuid::new_v4().to_string();
let mut path = std::path::PathBuf::from(&self.base_dir);
path.push(&uuid);
std::fs::create_dir_all(path.as_path()).unwrap();
path.push("bytecode.wasm");
let mut file = std::fs::File::create(path.as_path()).unwrap();
file.write_all(_bytecode_wasm.as_bytes());
uuid
}
pub fn read_application(&self, _application_uuid: &str) -> String {
let mut reading_path = std::path::PathBuf::from(&self.base_dir);
reading_path.push(&_application_uuid);
reading_path.push("bytecode.wasm");
let mut file_to_read = std::fs::File::open(reading_path.as_path()).unwrap();
let mut buffer = Vec::new();
let bytecode_wasm_string = file_to_read.read_to_end(&mut buffer).unwrap();
bytecode_wasm_string.to_string()
}
pub fn update_application(&self, _application_uuid: &str, _bytecode_wasm: &str) -> String {
let mut update_path = std::path::PathBuf::from(&self.base_dir);
update_path.push(&_application_uuid);
update_path.push("bytecode.wasm");
let _file_to_update = std::fs::remove_file(update_path.as_path()).unwrap();
let mut file_to_write = std::fs::File::create(update_path.as_path()).unwrap();
file_to_write.write_all(_bytecode_wasm.as_bytes());
_application_uuid.to_string()
}
pub fn delete_application(&self, _application_uuid: &str) -> String {
let mut path = std::path::PathBuf::from(&self.base_dir);
path.push(&_application_uuid);
std::fs::remove_dir_all(path.as_path()).unwrap();
_application_uuid.to_string()
}
}