12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ ///|
15
16
fn get_error_message_ffi () -> XExternString = "__moonbit_fs_unstable" "get_error_message"
16
17
18
+ ///|
17
19
fn get_error_message () -> String {
18
20
string_from_extern (get_error_message_ffi ())
19
21
}
20
22
23
+ ///|
21
24
fn get_file_content_ffi () -> XExternByteArray = "__moonbit_fs_unstable" "get_file_content"
22
25
26
+ ///|
23
27
fn read_file_to_bytes_ffi (path : XExternString ) -> Int = "__moonbit_fs_unstable" "read_file_to_bytes_new"
24
28
25
- fn write_bytes_to_file_ffi (path : XExternString , content : XExternByteArray ) -> Int = "__moonbit_fs_unstable" "write_bytes_to_file_new"
29
+ ///|
30
+ fn write_bytes_to_file_ffi (
31
+ path : XExternString ,
32
+ content : XExternByteArray
33
+ ) -> Int = "__moonbit_fs_unstable" "write_bytes_to_file_new"
26
34
35
+ ///|
27
36
fn read_file_to_bytes_internal (path : String ) -> Bytes ! {
28
37
let res = read_file_to_bytes_ffi (string_to_extern (path ))
29
38
guard res != - 1 else { raise IOError (get_error_message ()) }
30
39
byte_array_from_extern (get_file_content_ffi ())
31
40
}
32
41
33
- fn read_file_to_string_internal (path : String , encoding ~ : String = "utf8" ) -> String ! {
34
- guard encoding == "utf8" else { raise IOError ("Unsupported encoding: \{ encoding } , only utf8 is supported for now" ) }
42
+ ///|
43
+ fn read_file_to_string_internal (
44
+ path : String ,
45
+ encoding ~ : String = "utf8"
46
+ ) -> String ! {
47
+ guard encoding == "utf8" else {
48
+ raise IOError (
49
+ "Unsupported encoding: \{ encoding } , only utf8 is supported for now" ,
50
+ )
51
+ }
35
52
utf8_bytes_to_mbt_string (read_file_to_bytes_internal! (path ))
36
53
}
37
54
55
+ ///|
38
56
fn write_bytes_to_file_internal (path : String , content : Bytes ) -> Unit ! {
39
- let res = write_bytes_to_file_ffi (string_to_extern (path ), byte_array_to_extern (content ))
57
+ let res = write_bytes_to_file_ffi (
58
+ string_to_extern (path ),
59
+ byte_array_to_extern (content ),
60
+ )
40
61
guard res != - 1 else { raise IOError (get_error_message ()) }
62
+
41
63
}
42
64
43
- fn write_string_to_file_internal (path : String , content : String , encoding ~ : String = "utf8" ) -> Unit ! {
44
- guard encoding == "utf8" else { raise IOError ("Unsupported encoding: \{ encoding } , only utf8 is supported for now" ) }
65
+ ///|
66
+ fn write_string_to_file_internal (
67
+ path : String ,
68
+ content : String ,
69
+ encoding ~ : String = "utf8"
70
+ ) -> Unit ! {
71
+ guard encoding == "utf8" else {
72
+ raise IOError (
73
+ "Unsupported encoding: \{ encoding } , only utf8 is supported for now" ,
74
+ )
75
+ }
45
76
write_bytes_to_file_internal! (path , mbt_string_to_utf8_bytes (content , false ))
46
77
}
47
78
48
-
49
79
///|
50
80
priv type XStringCreateHandle
51
81
@@ -124,7 +154,9 @@ fn byte_array_from_extern(e : XExternByteArray) -> Bytes {
124
154
Bytes ::from_array (buf )
125
155
}
126
156
157
+ ///|
127
158
priv type XStringReadHandle
159
+
128
160
///|
129
161
fn begin_read_string (s : XExternString ) -> XStringReadHandle = "__moonbit_fs_unstable" "begin_read_string"
130
162
@@ -135,6 +167,7 @@ fn string_read_char(handle : XStringReadHandle) -> Int = "__moonbit_fs_unstable"
135
167
///|
136
168
fn finish_read_string (handle : XStringReadHandle ) = "__moonbit_fs_unstable" "finish_read_string"
137
169
170
+ ///|
138
171
fn string_from_extern (e : XExternString ) -> String {
139
172
let buf = StringBuilder ::new ()
140
173
let handle = begin_read_string (e )
0 commit comments