Skip to content

Commit

Permalink
feat: support get cli args & env var (#73)
Browse files Browse the repository at this point in the history
* split into internal/ffi

* feat: support get cli args & env var

* get env args in Map[String, String]

* moon fmt

* bump version

* moon info
  • Loading branch information
Young-Flash authored Nov 15, 2024
1 parent d12cee1 commit 56986f4
Show file tree
Hide file tree
Showing 51 changed files with 458 additions and 219 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
- main
pull_request:

env:
# just for testing env var
MOON_TEST: "yes"

jobs:
build:
strategy:
Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub fn Task::new(name : String, f : () -> Unit, ~count : Int = 10) -> Task {
pub fn Task::new(name : String, f : () -> Unit, count~ : Int = 10) -> Task {
{ name, f, count }
}

Expand Down
10 changes: 5 additions & 5 deletions benchmark/benchmark.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ impl Criterion {

type Task
impl Task {
new(String, () -> Unit, ~count : Int = ..) -> Self
new(String, () -> Unit, count~ : Int = ..) -> Self
run(Self) -> TaskResult
}

pub(readonly) struct TaskResult {
pub(readonly) task : Task
pub(readonly) average : Double
pub(readonly) max : Double
pub(readonly) min : Double
task : Task
average : Double
max : Double
min : Double
}
impl TaskResult {
output(Self, Logger) -> Unit
Expand Down
6 changes: 3 additions & 3 deletions crypto/chacha.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub fn chacha8(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
~nonce : UInt = 0
nonce~ : UInt = 0
) -> Bytes!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
Expand All @@ -313,7 +313,7 @@ pub fn chacha12(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
~nonce : UInt = 0
nonce~ : UInt = 0
) -> Bytes!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
Expand All @@ -331,7 +331,7 @@ pub fn chacha20(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
~nonce : UInt = 0
nonce~ : UInt = 0
) -> Bytes!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
Expand Down
8 changes: 4 additions & 4 deletions crypto/crypto.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package moonbitlang/x/crypto
// Values
fn bytes_to_hex_string(Bytes) -> String

fn chacha12(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!
fn chacha12(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!

fn chacha20(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!
fn chacha20(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!

fn chacha8(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!
fn chacha8(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!

fn md5(Bytes) -> Bytes

Expand Down Expand Up @@ -46,7 +46,7 @@ impl SM3Context {
type Sha256Context
impl Sha256Context {
finalize(Self) -> Bytes
new(~reg : FixedArray[UInt] = ..) -> Self
new(reg~ : FixedArray[UInt] = ..) -> Self
update(Self, Bytes) -> Unit
update_from_iter(Self, Iter[Byte]) -> Unit
}
Expand Down
6 changes: 3 additions & 3 deletions crypto/sha256.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Sha256Context {
/// Instantiate a Sha256 context
/// `reg` is the initial hash value. Defaults to Sha256's.
pub fn Sha256Context::new(
~reg : FixedArray[UInt] = [
reg~ : FixedArray[UInt] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
]
Expand Down Expand Up @@ -71,7 +71,7 @@ fn pad(self : Sha256Context) -> Bytes {
fn transform(
self : Sha256Context,
data : Bytes,
~offset : Int = 0
offset~ : Int = 0
) -> FixedArray[UInt] {
let w = FixedArray::make(64, 0U)
let mut a = self.reg[0]
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn update(self : Sha256Context, data : Bytes) -> Unit {

fn sha256_compute(
self : Sha256Context,
~data : Iter[Byte] = Iter::empty()
data~ : Iter[Byte] = Iter::empty()
) -> FixedArray[UInt] {
self.update_from_iter(data)
let msg = self.pad()
Expand Down
4 changes: 2 additions & 2 deletions crypto/sm3.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn pad(self : SM3Context) -> Bytes {
fn transform(
self : SM3Context,
data : Bytes,
~offset : Int = 0
offset~ : Int = 0
) -> FixedArray[UInt] {
let w_0 = FixedArray::make(68, 0U)
let w_1 = FixedArray::make(64, 0U)
Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn update(self : SM3Context, data : Bytes) -> Unit {

fn sm3_compute(
self : SM3Context,
~data : Iter[Byte] = Iter::empty()
data~ : Iter[Byte] = Iter::empty()
) -> FixedArray[UInt] {
self.update_from_iter(data)
let msg = self.pad()
Expand Down
4 changes: 2 additions & 2 deletions crypto/utils.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn uint32(x : Byte) -> UInt {
/// convert 4 bytes a byte sequence to a UInt in little endian
/// - `x` : A byte sequence consisting of 4 or more bytes
/// - `i` : An offset. e.g. i = 4 will convert `x[4~7]` to a UInt.
fn u8_to_u32le(x : Bytes, ~i : Int = 0) -> UInt {
fn u8_to_u32le(x : Bytes, i~ : Int = 0) -> UInt {
uint32(x[i]) |
(uint32(x[i + 1]) << 8) |
(uint32(x[i + 2]) << 16) |
Expand All @@ -81,7 +81,7 @@ fn u8_to_u32le(x : Bytes, ~i : Int = 0) -> UInt {
// )
// }

fn bytes_u8_to_u32be(x : Bytes, ~i : Int = 0) -> UInt {
fn bytes_u8_to_u32be(x : Bytes, i~ : Int = 0) -> UInt {
(uint32(x[i]) << 24) |
(uint32(x[i + 1]) << 16) |
(uint32(x[i + 2]) << 8) |
Expand Down
38 changes: 19 additions & 19 deletions fs/fs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub type! IOError {
pub(all) type! IOError {
NotFound(String)
}

Expand All @@ -31,7 +31,7 @@ fn IOError::to_string(self : IOError) -> String {
/// # Parameters
/// - `path`: A `String` representing the file path.
/// - `content`: A `String` containing the content to be written to the file.
pub fn write_string_to_file(~path : String, ~content : String) -> Unit {
pub fn write_string_to_file(path~ : String, content~ : String) -> Unit {
@ffi.write_string_to_file(path, content)
}

Expand All @@ -41,7 +41,7 @@ pub fn write_string_to_file(~path : String, ~content : String) -> Unit {
///
/// - `path` : The path to the file where the bytes will be written.
/// - `content` : An array of 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 {
@ffi.write_bytes_to_file(path, content)
}

Expand All @@ -52,7 +52,7 @@ pub fn write_bytes_to_file(~path : String, ~content : Bytes) -> Unit {
///
/// # Returns
/// A boolean indicating whether the path exists.
pub fn path_exists(~path : String) -> Bool {
pub fn path_exists(path~ : String) -> Bool {
@ffi.path_exists(path)
}

Expand All @@ -63,8 +63,8 @@ pub fn path_exists(~path : String) -> Bool {
///
/// # Returns
/// A `String` containing the file contents if the file exists, otherwise raises an error.
pub fn read_file_to_string(~path : String) -> String! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn read_file_to_string(path~ : String) -> String! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.read_file_to_string(path)
}

Expand All @@ -78,8 +78,8 @@ pub fn read_file_to_string(~path : String) -> String! {
/// # Returns
///
/// - An array of bytes representing the content of the file.
pub fn read_file_to_bytes(~path : String) -> Bytes! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn read_file_to_bytes(path~ : String) -> Bytes! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.read_file_to_bytes(path)
}

Expand All @@ -92,8 +92,8 @@ pub fn read_file_to_bytes(~path : String) -> Bytes! {
/// # Returns
///
/// - An array of strings representing the file name and directory name in the directory.
pub fn read_dir(~path : String) -> Array[String]! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn read_dir(path~ : String) -> Array[String]! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.read_dir(path)
}

Expand All @@ -103,7 +103,7 @@ pub fn read_dir(~path : String) -> Array[String]! {
/// # Parameters
///
/// - `path` : The path where the directory should be created.
pub fn create_dir(~path : String) -> Unit {
pub fn create_dir(path~ : String) -> Unit {
@ffi.create_dir(path)
}

Expand All @@ -116,8 +116,8 @@ pub fn create_dir(~path : String) -> Unit {
/// # Returns
///
/// - `Bool` : `true` if the path is a directory, `false` otherwise.
pub fn is_dir(~path : String) -> Bool! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn is_dir(path~ : String) -> Bool! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.is_dir(path)
}

Expand All @@ -130,8 +130,8 @@ pub fn is_dir(~path : String) -> Bool! {
/// # Returns
///
/// - `Bool` : `true` if the path points to a file, `false` otherwise.
pub fn is_file(~path : String) -> Bool! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn is_file(path~ : String) -> Bool! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.is_file(path)
}

Expand All @@ -140,8 +140,8 @@ pub fn is_file(~path : String) -> Bool! {
/// # Parameters
///
/// - `path` : The string path to the directory that needs to be removed.
pub fn remove_dir(~path : String) -> Unit! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn remove_dir(path~ : String) -> Unit! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.remove_dir(path)
}

Expand All @@ -150,7 +150,7 @@ pub fn remove_dir(~path : String) -> Unit! {
/// # Parameters
///
/// - `path` : The path to the file that needs to be removed.
pub fn remove_file(~path : String) -> Unit! {
guard path_exists(~path) else { raise IOError::NotFound(path) }
pub fn remove_file(path~ : String) -> Unit! {
guard path_exists(path~) else { raise IOError::NotFound(path) }
@ffi.remove_file(path)
}
24 changes: 12 additions & 12 deletions fs/fs.mbti
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package moonbitlang/x/fs

// Values
fn create_dir(~path : String) -> Unit
fn create_dir(path~ : String) -> Unit

fn is_dir(~path : String) -> Bool!
fn is_dir(path~ : String) -> Bool!

fn is_file(~path : String) -> Bool!
fn is_file(path~ : String) -> Bool!

fn path_exists(~path : String) -> Bool
fn path_exists(path~ : String) -> Bool

fn read_dir(~path : String) -> Array[String]!
fn read_dir(path~ : String) -> Array[String]!

fn read_file_to_bytes(~path : String) -> Bytes!
fn read_file_to_bytes(path~ : String) -> Bytes!

fn read_file_to_string(~path : String) -> String!
fn read_file_to_string(path~ : String) -> String!

fn remove_dir(~path : String) -> Unit!
fn remove_dir(path~ : String) -> Unit!

fn remove_file(~path : String) -> Unit!
fn remove_file(path~ : String) -> Unit!

fn write_bytes_to_file(~path : String, ~content : Bytes) -> Unit
fn write_bytes_to_file(path~ : String, content~ : Bytes) -> Unit

fn write_string_to_file(~path : String, ~content : String) -> Unit
fn write_string_to_file(path~ : String, content~ : String) -> Unit

// Types and methods
pub type! IOError {
pub(all) type! IOError {
NotFound(String)
}
impl Show for IOError
Expand Down
22 changes: 11 additions & 11 deletions fs/fs_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ test "write_and_read" {
#|
,
)
assert_true!(@fs.path_exists(~path))
let byte = @fs.read_file_to_bytes!(~path)
assert_true!(@fs.path_exists(path~))
let byte = @fs.read_file_to_bytes!(path~)
inspect!(
byte,
content=
#|b"\x74\x61\x72\x67\x65\x74\x2f\x0a\x2e\x6d\x6f\x6f\x6e\x63\x61\x6b\x65\x73\x2f\x0a"
,
)
@fs.remove_file!(~path)
assert_false!(@fs.path_exists(~path))
@fs.remove_file!(path~)
assert_false!(@fs.path_exists(path~))
try {
@fs.read_file_to_string!(~path) |> ignore
@fs.read_file_to_string!(path~) |> ignore
} catch {
@fs.IOError::NotFound(_) as e =>
inspect!(e, content="`1.txt` does not exist")
_ => return
}
let bytes = Bytes::from_array([65, 97].map(fn(x) { x.to_byte() }))
@fs.write_bytes_to_file(~path, content=bytes)
assert_true!(@fs.path_exists(~path))
let content = @fs.read_file_to_string!(~path)
@fs.write_bytes_to_file(path~, content=bytes)
assert_true!(@fs.path_exists(path~))
let content = @fs.read_file_to_string!(path~)
inspect!(content, content="Aa")
@fs.remove_file!(~path)
assert_false!(@fs.path_exists(~path))
@fs.remove_file!(path~)
assert_false!(@fs.path_exists(path~))
try {
@fs.remove_file!(~path) |> ignore
@fs.remove_file!(path~) |> ignore
} catch {
@fs.IOError::NotFound(_) as e =>
inspect!(e, content="`1.txt` does not exist")
Expand Down
Loading

0 comments on commit 56986f4

Please sign in to comment.