Skip to content

Commit c8f6ac5

Browse files
committed
minor: moon fmt
1 parent a788199 commit c8f6ac5

File tree

5 files changed

+110
-23
lines changed

5 files changed

+110
-23
lines changed

fs_sync/blackbox_test.mbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
1516
test "write_and_read" {
1617
let path_1 = "./fs_sync/test_1.txt"
1718
let path_2 = "./fs_sync/test_2.txt"
@@ -20,7 +21,6 @@ test "write_and_read" {
2021
@fs_sync.write_bytes_to_file!(path_1, content_1.to_bytes())
2122
let res_1 = @fs_sync.read_file_to_bytes!(path_1)
2223
inspect!(res_1.to_unchecked_string(), content=content_1)
23-
2424
@fs_sync.write_string_to_file!(path_2, content_2)
2525
let res_2 = @fs_sync.read_file_to_string!(path_2)
2626
inspect!(res_2, content=content_2)

fs_sync/fs_sync.mbt

+11-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub type! IOError String derive(Show)
2525
/// # Returns
2626
///
2727
/// - A `Bytes` representing the content of the file.
28-
pub fn read_file_to_bytes(path: String) -> Bytes! {
28+
pub fn read_file_to_bytes(path : String) -> Bytes! {
2929
read_file_to_bytes_internal!(path)
3030
}
3131

@@ -40,7 +40,10 @@ pub fn read_file_to_bytes(path: String) -> Bytes! {
4040
/// # Returns
4141
///
4242
/// - A `String` representing the content of the file.
43-
pub fn read_file_to_string(path: String, encoding~ : String = "utf8") -> String! {
43+
pub fn read_file_to_string(
44+
path : String,
45+
encoding~ : String = "utf8"
46+
) -> String! {
4447
read_file_to_string_internal!(path, encoding~)
4548
}
4649

@@ -50,7 +53,7 @@ pub fn read_file_to_string(path: String, encoding~ : String = "utf8") -> String!
5053
///
5154
/// - `path` : The path to the file where the bytes will be written.
5255
/// - `content` : A `Bytes` to be written to the file.
53-
pub fn write_bytes_to_file(path: String, content: Bytes) -> Unit! {
56+
pub fn write_bytes_to_file(path : String, content : Bytes) -> Unit! {
5457
write_bytes_to_file_internal!(path, content)
5558
}
5659

@@ -61,6 +64,10 @@ pub fn write_bytes_to_file(path: String, content: Bytes) -> Unit! {
6164
/// - `path` : The path to the file where the string will be written.
6265
/// - `content` : A `String` to be written to the file.
6366
/// - `encoding~` : The encoding of the file. Only support `utf8` for now.
64-
pub fn write_string_to_file(path: String, content: String, encoding~ : String = "utf8") -> Unit! {
67+
pub fn write_string_to_file(
68+
path : String,
69+
content : String,
70+
encoding~ : String = "utf8"
71+
) -> Unit! {
6572
write_string_to_file_internal!(path, content, encoding~)
6673
}

fs_sync/fs_sync_js.mbt

+21-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ fn read_file_to_bytes_internal(path : String) -> Bytes! {
6161
Bytes::from_iter(content.iter())
6262
}
6363

64-
fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
65-
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
64+
///|
65+
fn read_file_to_string_internal(
66+
path : String,
67+
encoding~ : String = "utf8"
68+
) -> String! {
69+
guard encoding == "utf8" else {
70+
raise IOError(
71+
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
72+
)
73+
}
6674
let bytes = read_file_to_bytes_internal!(path)
6775
utf8_bytes_to_mbt_string(bytes)
6876
}
@@ -75,8 +83,17 @@ fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
7583
}
7684
}
7785

78-
fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
79-
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
86+
///|
87+
fn write_string_to_file_internal(
88+
path : String,
89+
content : String,
90+
encoding~ : String = "utf8"
91+
) -> Unit! {
92+
guard encoding == "utf8" else {
93+
raise IOError(
94+
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
95+
)
96+
}
8097
let bytes = mbt_string_to_utf8_bytes(content, false)
8198
write_bytes_to_file_internal!(path, bytes)
8299
}

fs_sync/fs_sync_native.mbt

+37-7
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ fn fopen_ffi(path : Bytes, mode : Bytes) -> File_Handler = "$moonbit.fopen_ffi"
2222
fn is_null(ptr : File_Handler) -> Int = "$moonbit.is_null"
2323

2424
///|
25-
fn fread_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fread_ffi"
25+
fn fread_ffi(
26+
ptr : Bytes,
27+
size : Int,
28+
nitems : Int,
29+
stream : File_Handler
30+
) -> Int = "$moonbit.fread_ffi"
2631

2732
///|
28-
fn fwrite_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fwrite_ffi"
33+
fn fwrite_ffi(
34+
ptr : Bytes,
35+
size : Int,
36+
nitems : Int,
37+
stream : File_Handler
38+
) -> Int = "$moonbit.fwrite_ffi"
2939

3040
///|
3141
fn get_error_message_ffi() -> Bytes = "$moonbit.get_error_message"
@@ -62,8 +72,16 @@ fn read_file_to_bytes_internal(path : String) -> Bytes! {
6272
bytes
6373
}
6474

65-
fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
66-
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
75+
///|
76+
fn read_file_to_string_internal(
77+
path : String,
78+
encoding~ : String = "utf8"
79+
) -> String! {
80+
guard encoding == "utf8" else {
81+
raise IOError(
82+
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
83+
)
84+
}
6785
utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path))
6886
}
6987

@@ -72,13 +90,25 @@ fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
7290
let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"wb")
7391
guard is_null(file) == 0 else { raise IOError(get_error_message()) }
7492
let bytes_written = fwrite_ffi(content, 1, content.length(), file)
75-
guard bytes_written == content.length() else { raise IOError(get_error_message()) }
93+
guard bytes_written == content.length() else {
94+
raise IOError(get_error_message())
95+
}
7696
guard fflush_ffi(file) == 0 else { raise IOError(get_error_message()) }
7797
guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) }
98+
7899
}
79100

80-
fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
81-
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
101+
///|
102+
fn write_string_to_file_internal(
103+
path : String,
104+
content : String,
105+
encoding~ : String = "utf8"
106+
) -> Unit! {
107+
guard encoding == "utf8" else {
108+
raise IOError(
109+
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
110+
)
111+
}
82112
let bytes = mbt_string_to_utf8_bytes(content, false)
83113
write_bytes_to_file_internal!(path, bytes)
84114
}

fs_sync/fs_sync_wasm.mbt

+40-7
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,70 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
1516
fn get_error_message_ffi() -> XExternString = "__moonbit_fs_unstable" "get_error_message"
1617

18+
///|
1719
fn get_error_message() -> String {
1820
string_from_extern(get_error_message_ffi())
1921
}
2022

23+
///|
2124
fn get_file_content_ffi() -> XExternByteArray = "__moonbit_fs_unstable" "get_file_content"
2225

26+
///|
2327
fn read_file_to_bytes_ffi(path : XExternString) -> Int = "__moonbit_fs_unstable" "read_file_to_bytes_new"
2428

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"
2634

35+
///|
2736
fn read_file_to_bytes_internal(path : String) -> Bytes! {
2837
let res = read_file_to_bytes_ffi(string_to_extern(path))
2938
guard res != -1 else { raise IOError(get_error_message()) }
3039
byte_array_from_extern(get_file_content_ffi())
3140
}
3241

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+
}
3552
utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path))
3653
}
3754

55+
///|
3856
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+
)
4061
guard res != -1 else { raise IOError(get_error_message()) }
62+
4163
}
4264

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+
}
4576
write_bytes_to_file_internal!(path, mbt_string_to_utf8_bytes(content, false))
4677
}
4778

48-
4979
///|
5080
priv type XStringCreateHandle
5181

@@ -124,7 +154,9 @@ fn byte_array_from_extern(e : XExternByteArray) -> Bytes {
124154
Bytes::from_array(buf)
125155
}
126156

157+
///|
127158
priv type XStringReadHandle
159+
128160
///|
129161
fn begin_read_string(s : XExternString) -> XStringReadHandle = "__moonbit_fs_unstable" "begin_read_string"
130162

@@ -135,6 +167,7 @@ fn string_read_char(handle : XStringReadHandle) -> Int = "__moonbit_fs_unstable"
135167
///|
136168
fn finish_read_string(handle : XStringReadHandle) = "__moonbit_fs_unstable" "finish_read_string"
137169

170+
///|
138171
fn string_from_extern(e : XExternString) -> String {
139172
let buf = StringBuilder::new()
140173
let handle = begin_read_string(e)

0 commit comments

Comments
 (0)