Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion encoding/types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(all) enum Encoding {
UTF16 // alias for UTF16LE
UTF16LE
UTF16BE
}
} derive(Show)

// Decoder

Expand Down
7 changes: 5 additions & 2 deletions fs/fs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///| Re-export from `@encoding`
pub typealias @encoding.Encoding

///|
pub(all) suberror IOError String derive(Show)

Expand Down Expand Up @@ -42,7 +45,7 @@ pub fn read_file_to_bytes(path : String) -> Bytes raise IOError {
/// - A `String` representing the content of the file.
pub fn read_file_to_string(
path : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> String raise IOError {
read_file_to_string_internal(path, encoding~)
}
Expand Down Expand Up @@ -70,7 +73,7 @@ pub fn write_bytes_to_file(
pub fn write_string_to_file(
path : String,
content : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> Unit raise IOError {
write_string_to_file_internal(path, content, encoding~)
}
Expand Down
32 changes: 18 additions & 14 deletions fs/fs_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> String raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
let bytes = read_file_to_bytes_internal(path)
@ffi.utf8_bytes_to_mbt_string(bytes)
}

///|
Expand All @@ -95,15 +95,19 @@ fn write_bytes_to_file_internal(
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> Unit raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 =>
write_bytes_to_file_internal(
path,
@ffi.mbt_string_to_utf8_bytes(content, false),
)
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
let bytes = @ffi.mbt_string_to_utf8_bytes(content, false)
write_bytes_to_file_internal(path, bytes)
}

///|
Expand Down
31 changes: 18 additions & 13 deletions fs/fs_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> String raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
@ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
}

///|
Expand All @@ -105,15 +106,19 @@ fn write_bytes_to_file_internal(
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> Unit raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 =>
write_bytes_to_file_internal(
path,
@ffi.mbt_string_to_utf8_bytes(content, false),
)
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
let bytes = @ffi.mbt_string_to_utf8_bytes(content, false)
write_bytes_to_file_internal(path, bytes)
}

///|
Expand Down
33 changes: 18 additions & 15 deletions fs/fs_wasm.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ fn read_file_to_bytes_internal(path : String) -> Bytes raise IOError {
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> String raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 => @ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
@ffi.utf8_bytes_to_mbt_string(read_file_to_bytes_internal(path))
}

///|
Expand All @@ -80,17 +81,19 @@ fn write_bytes_to_file_internal(
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
encoding~ : Encoding = UTF8
) -> Unit raise IOError {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
match encoding {
UTF8 =>
write_bytes_to_file_internal(
path,
@ffi.mbt_string_to_utf8_bytes(content, false),
)
_ =>
raise IOError(
"Unsupported encoding: \{encoding}, only \{Encoding::UTF8} is supported for now",
)
}
write_bytes_to_file_internal(
path,
@ffi.mbt_string_to_utf8_bytes(content, false),
)
}

///|
Expand Down
3 changes: 2 additions & 1 deletion fs/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"import": [
"moonbitlang/x/internal/ffi"
"moonbitlang/x/internal/ffi",
"moonbitlang/x/encoding"
],
"targets": {
"fs_wasm.mbt": ["wasm", "wasm-gc"],
Expand Down
Loading