Skip to content

Commit

Permalink
minor: moon fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Jan 24, 2025
1 parent e94c549 commit ac4aa27
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fs_sync/blackbox_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "write_and_read" {
let path_1 = "./fs_sync/test_1.txt"
let path_2 = "./fs_sync/test_2.txt"
Expand All @@ -20,7 +21,6 @@ test "write_and_read" {
@fs_sync.write_bytes_to_file!(path_1, content_1.to_bytes())
let res_1 = @fs_sync.read_file_to_bytes!(path_1)
inspect!(res_1.to_unchecked_string(), content=content_1)

@fs_sync.write_string_to_file!(path_2, content_2)
let res_2 = @fs_sync.read_file_to_string!(path_2)
inspect!(res_2, content=content_2)
Expand Down
15 changes: 11 additions & 4 deletions fs_sync/fs_sync.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub type! IOError String derive(Show)
/// # Returns
///
/// - A `Bytes` representing the content of the file.
pub fn read_file_to_bytes(path: String) -> Bytes! {
pub fn read_file_to_bytes(path : String) -> Bytes! {
read_file_to_bytes_internal!(path)
}

Expand All @@ -40,7 +40,10 @@ pub fn read_file_to_bytes(path: String) -> Bytes! {
/// # Returns
///
/// - A `String` representing the content of the file.
pub fn read_file_to_string(path: String, encoding~ : String = "utf8") -> String! {
pub fn read_file_to_string(
path : String,
encoding~ : String = "utf8"
) -> String! {
read_file_to_string_internal!(path, encoding~)
}

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

Expand All @@ -61,6 +64,10 @@ pub fn write_bytes_to_file(path: String, content: Bytes) -> Unit! {
/// - `path` : The path to the file where the string will be written.
/// - `content` : A `String` to be written to the file.
/// - `encoding~` : The encoding of the file. Only support `utf8` for now.
pub fn write_string_to_file(path: String, content: String, encoding~ : String = "utf8") -> Unit! {
pub fn write_string_to_file(
path : String,
content : String,
encoding~ : String = "utf8"
) -> Unit! {
write_string_to_file_internal!(path, content, encoding~)
}
25 changes: 21 additions & 4 deletions fs_sync/fs_sync_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ fn read_file_to_bytes_internal(path : String) -> Bytes! {
Bytes::from_iter(content.iter())
}

fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
) -> String! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
let bytes = read_file_to_bytes_internal!(path)
utf8_bytes_to_mbt_string(bytes)
}
Expand All @@ -75,8 +83,17 @@ fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
}
}

fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
) -> Unit! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
let bytes = mbt_string_to_utf8_bytes(content, false)
write_bytes_to_file_internal!(path, bytes)
}
44 changes: 37 additions & 7 deletions fs_sync/fs_sync_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ fn fopen_ffi(path : Bytes, mode : Bytes) -> File_Handler = "$moonbit.fopen_ffi"
fn is_null(ptr : File_Handler) -> Int = "$moonbit.is_null"

///|
fn fread_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fread_ffi"
fn fread_ffi(
ptr : Bytes,
size : Int,
nitems : Int,
stream : File_Handler
) -> Int = "$moonbit.fread_ffi"

///|
fn fwrite_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fwrite_ffi"
fn fwrite_ffi(
ptr : Bytes,
size : Int,
nitems : Int,
stream : File_Handler
) -> Int = "$moonbit.fwrite_ffi"

///|
fn get_error_message_ffi() -> Bytes = "$moonbit.get_error_message"
Expand Down Expand Up @@ -62,8 +72,16 @@ fn read_file_to_bytes_internal(path : String) -> Bytes! {
bytes
}

fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
) -> String! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path))
}

Expand All @@ -72,13 +90,25 @@ fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"wb")
guard is_null(file) == 0 else { raise IOError(get_error_message()) }
let bytes_written = fwrite_ffi(content, 1, content.length(), file)
guard bytes_written == content.length() else { raise IOError(get_error_message()) }
guard bytes_written == content.length() else {
raise IOError(get_error_message())
}
guard fflush_ffi(file) == 0 else { raise IOError(get_error_message()) }
guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) }

}

fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
) -> Unit! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
let bytes = mbt_string_to_utf8_bytes(content, false)
write_bytes_to_file_internal!(path, bytes)
}
47 changes: 40 additions & 7 deletions fs_sync/fs_sync_wasm.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,70 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn get_error_message_ffi() -> XExternString = "__moonbit_fs_unstable" "get_error_message"

///|
fn get_error_message() -> String {
string_from_extern(get_error_message_ffi())
}

///|
fn get_file_content_ffi() -> XExternByteArray = "__moonbit_fs_unstable" "get_file_content"

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

fn write_bytes_to_file_ffi(path : XExternString, content : XExternByteArray) -> Int = "__moonbit_fs_unstable" "write_bytes_to_file_new"
///|
fn write_bytes_to_file_ffi(
path : XExternString,
content : XExternByteArray
) -> Int = "__moonbit_fs_unstable" "write_bytes_to_file_new"

///|
fn read_file_to_bytes_internal(path : String) -> Bytes! {
let res = read_file_to_bytes_ffi(string_to_extern(path))
guard res != -1 else { raise IOError(get_error_message()) }
byte_array_from_extern(get_file_content_ffi())
}

fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn read_file_to_string_internal(
path : String,
encoding~ : String = "utf8"
) -> String! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path))
}

///|
fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
let res = write_bytes_to_file_ffi(string_to_extern(path), byte_array_to_extern(content))
let res = write_bytes_to_file_ffi(
string_to_extern(path),
byte_array_to_extern(content),
)
guard res != -1 else { raise IOError(get_error_message()) }

}

fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
///|
fn write_string_to_file_internal(
path : String,
content : String,
encoding~ : String = "utf8"
) -> Unit! {
guard encoding == "utf8" else {
raise IOError(
"Unsupported encoding: \{encoding}, only utf8 is supported for now",
)
}
write_bytes_to_file_internal!(path, mbt_string_to_utf8_bytes(content, false))
}


///|
priv type XStringCreateHandle

Expand Down Expand Up @@ -124,7 +154,9 @@ fn byte_array_from_extern(e : XExternByteArray) -> Bytes {
Bytes::from_array(buf)
}

///|
priv type XStringReadHandle

///|
fn begin_read_string(s : XExternString) -> XStringReadHandle = "__moonbit_fs_unstable" "begin_read_string"

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

///|
fn string_from_extern(e : XExternString) -> String {
let buf = StringBuilder::new()
let handle = begin_read_string(e)
Expand Down

0 comments on commit ac4aa27

Please sign in to comment.