Skip to content

Commit

Permalink
Merge pull request #402 from moonbitlang/bytes-hex-literal
Browse files Browse the repository at this point in the history
Bytes hex int literal
  • Loading branch information
lijunchen authored Oct 18, 2024
2 parents e833bec + d2cf764 commit 212c606
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
15 changes: 11 additions & 4 deletions crates/moon/src/cli/tool/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,25 @@ pub fn run_embed_bin(cmd: Embed) -> anyhow::Result<i32> {
let name = cmd.name.unwrap_or_else(|| "resource".to_string());
let mut content = format!(
"// Generated by `moon tool embed --binary`{}, do not edit.\n\n\
let {} : Bytes = b\"",
let {} : Bytes = Bytes::of([\n",
if cmd.timestamp {
format!(" on {}", chrono::Local::now().format("%Y-%m-%d %H:%M:%S"))
} else {
String::new()
},
name
);
for byte in input.iter() {
content.push_str(&format!("\\x{:02x}", byte));

for (i, byte) in input.iter().enumerate() {
if i % 12 == 0 {
if i > 0 {
content.push('\n');
}
content.push_str(" ");
}
content.push_str(&format!("0x{:02x}, ", byte));
}
content.push_str("\"\n");
content.push_str("\n])\n");
std::fs::write(cmd.output, content).context("write output file")?;
Ok(0)
}
Expand Down
37 changes: 34 additions & 3 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6865,6 +6865,11 @@ fn no_main_just_init() {
#[test]
fn test_pre_build() {
let dir = TestDir::new("pre_build.in");

// replace CRLF with LF on Windows
let b_txt_path = dir.join("src/lib/b.txt");
std::fs::write(&b_txt_path, read(&b_txt_path)).unwrap();

check(
get_stderr(&dir, ["check"]),
expect![[r#"
Expand Down Expand Up @@ -6905,9 +6910,35 @@ fn test_pre_build() {
"#]],
);
let content = read(dir.join("src/lib/b.mbt"));
assert!(content.contains("// Generated by `moon tool embed --binary`, do not edit."));
assert!(content.contains(r#"let _b : Bytes = b"\x6d\x6f\x6f\x6e"#));
assert!(content.contains(r#"\x0a\x67\x65\x6e\x65\x72\x61\x74\x65"#));
check(
content,
expect![[r#"
// Generated by `moon tool embed --binary`, do not edit.
let _b : Bytes = Bytes::of([
0x4d, 0x6f, 0x6f, 0x6e, 0x42, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61,
0x6e, 0x20, 0x65, 0x6e, 0x64, 0x2d, 0x74, 0x6f, 0x2d, 0x65, 0x6e, 0x64,
0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67,
0x20, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x20, 0x74, 0x6f,
0x6f, 0x6c, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20,
0x63, 0x6c, 0x6f, 0x75, 0x64, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x65, 0x64,
0x67, 0x65, 0x0a, 0x63, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x69, 0x6e, 0x67,
0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x57, 0x65, 0x62, 0x41, 0x73,
0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20,
0x49, 0x44, 0x45, 0x20, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d,
0x65, 0x6e, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x76, 0x61, 0x69, 0x6c,
0x61, 0x62, 0x6c, 0x65, 0x20, 0x61, 0x74, 0x0a, 0x68, 0x74, 0x74, 0x70,
0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x72, 0x79, 0x2e, 0x6d, 0x6f, 0x6f, 0x6e,
0x62, 0x69, 0x74, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x20,
0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x61, 0x6e, 0x79, 0x20,
0x69, 0x6e, 0x73, 0x74, 0x61, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x3b, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6f, 0x65, 0x73, 0x20, 0x6e, 0x6f,
0x74, 0x20, 0x72, 0x65, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x6e,
0x79, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x65, 0x69, 0x74,
0x68, 0x65, 0x72, 0x2e,
])
"#]],
);
check(
read(dir.join("src/lib/c.mbt")),
expect![[r#"
Expand Down
6 changes: 4 additions & 2 deletions crates/moon/tests/test_cases/pre_build.in/src/lib/b.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
moon
generate
MoonBit is an end-to-end programming language toolchain for cloud and edge
computing using WebAssembly. The IDE environment is available at
https://try.moonbitlang.com without any installation; it does not rely on any
server either.

0 comments on commit 212c606

Please sign in to comment.