Skip to content

Commit d510268

Browse files
committed
refactor: use enum for encoding argument
1 parent 34fa9f7 commit d510268

File tree

6 files changed

+62
-46
lines changed

6 files changed

+62
-46
lines changed

encoding/types.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(all) enum Encoding {
2424
UTF16 // alias for UTF16LE
2525
UTF16LE
2626
UTF16BE
27-
}
27+
} derive(Show)
2828

2929
// Decoder
3030

fs/fs.mbt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///| Re-export from `@encoding`
16+
pub typealias @encoding.Encoding
17+
1518
///|
1619
pub(all) suberror IOError String derive(Show)
1720

@@ -42,7 +45,7 @@ pub fn read_file_to_bytes(path : String) -> Bytes raise IOError {
4245
/// - A `String` representing the content of the file.
4346
pub fn read_file_to_string(
4447
path : String,
45-
encoding~ : String = "utf8"
48+
encoding~ : Encoding = UTF8
4649
) -> String raise IOError {
4750
read_file_to_string_internal(path, encoding~)
4851
}
@@ -70,7 +73,7 @@ pub fn write_bytes_to_file(
7073
pub fn write_string_to_file(
7174
path : String,
7275
content : String,
73-
encoding~ : String = "utf8"
76+
encoding~ : Encoding = UTF8
7477
) -> Unit raise IOError {
7578
write_string_to_file_internal(path, content, encoding~)
7679
}

fs/fs_js.mbt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
6969
///|
7070
fn read_file_to_string_internal(
7171
path : String,
72-
encoding~ : String = "utf8"
72+
encoding~ : Encoding = UTF8
7373
) -> String raise IOError {
74-
guard encoding == "utf8" else {
75-
raise IOError(
76-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
77-
)
74+
match encoding {
75+
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
76+
_ =>
77+
raise IOError(
78+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
79+
)
7880
}
79-
let bytes = read_file_to_bytes_internal(path)
80-
@ffi.utf8_bytes_to_mbt_string(bytes)
8181
}
8282

8383
///|
@@ -95,15 +95,19 @@ fn write_bytes_to_file_internal(
9595
fn write_string_to_file_internal(
9696
path : String,
9797
content : String,
98-
encoding~ : String = "utf8"
98+
encoding~ : Encoding = UTF8
9999
) -> Unit raise IOError {
100-
guard encoding == "utf8" else {
101-
raise IOError(
102-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
103-
)
100+
match encoding {
101+
UTF8 =>
102+
write_bytes_to_file_internal(
103+
path,
104+
@ffi.mbt_string_to_utf8_bytes(content, false),
105+
)
106+
_ =>
107+
raise IOError(
108+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
109+
)
104110
}
105-
let bytes = @ffi.mbt_string_to_utf8_bytes(content, false)
106-
write_bytes_to_file_internal(path, bytes)
107111
}
108112

109113
///|

fs/fs_native.mbt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
7676
///|
7777
fn read_file_to_string_internal(
7878
path : String,
79-
encoding~ : String = "utf8"
79+
encoding~ : Encoding = UTF8
8080
) -> String raise IOError {
81-
guard encoding == "utf8" else {
82-
raise IOError(
83-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
84-
)
81+
match encoding {
82+
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
83+
_ =>
84+
raise IOError(
85+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
86+
)
8587
}
86-
@ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
8788
}
8889

8990
///|
@@ -105,15 +106,19 @@ fn write_bytes_to_file_internal(
105106
fn write_string_to_file_internal(
106107
path : String,
107108
content : String,
108-
encoding~ : String = "utf8"
109+
encoding~ : Encoding = UTF8
109110
) -> Unit raise IOError {
110-
guard encoding == "utf8" else {
111-
raise IOError(
112-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
113-
)
111+
match encoding {
112+
UTF8 =>
113+
write_bytes_to_file_internal(
114+
path,
115+
@ffi.mbt_string_to_utf8_bytes(content, false),
116+
)
117+
_ =>
118+
raise IOError(
119+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
120+
)
114121
}
115-
let bytes = @ffi.mbt_string_to_utf8_bytes(content, false)
116-
write_bytes_to_file_internal(path, bytes)
117122
}
118123

119124
///|

fs/fs_wasm.mbt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
5454
///|
5555
fn read_file_to_string_internal(
5656
path : String,
57-
encoding~ : String = "utf8"
57+
encoding~ : Encoding = UTF8
5858
) -> String raise IOError {
59-
guard encoding == "utf8" else {
60-
raise IOError(
61-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
62-
)
59+
match encoding {
60+
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
61+
_ =>
62+
raise IOError(
63+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
64+
)
6365
}
64-
@ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
6566
}
6667

6768
///|
@@ -80,17 +81,19 @@ fn write_bytes_to_file_internal(
8081
fn write_string_to_file_internal(
8182
path : String,
8283
content : String,
83-
encoding~ : String = "utf8"
84+
encoding~ : Encoding = UTF8
8485
) -> Unit raise IOError {
85-
guard encoding == "utf8" else {
86-
raise IOError(
87-
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
88-
)
86+
match encoding {
87+
UTF8 =>
88+
write_bytes_to_file_internal(
89+
path,
90+
@ffi.mbt_string_to_utf8_bytes(content, false),
91+
)
92+
_ =>
93+
raise IOError(
94+
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
95+
)
8996
}
90-
write_bytes_to_file_internal(
91-
path,
92-
@ffi.mbt_string_to_utf8_bytes(content, false),
93-
)
9497
}
9598

9699
///|

fs/moon.pkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"import": [
3-
"moonbitlang/x/internal/ffi"
3+
"moonbitlang/x/internal/ffi",
4+
"moonbitlang/x/encoding"
45
],
56
"targets": {
67
"fs_wasm.mbt": ["wasm", "wasm-gc"],

0 commit comments

Comments
 (0)