-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from eval-exec/feat/customize-union-id
[Feature] Add custom union ID syntax
- Loading branch information
Showing
24 changed files
with
644 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "tests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
[dev-dependencies] | ||
codegen-0_7_3 = {package = "molecule-codegen", version = "0.7.3", features = ["compiler-plugin"]} | ||
codegen-dev = {package = "molecule-codegen", path = "../tools/codegen", features = ["compiler-plugin"]} | ||
molecule = "0.7.3" | ||
|
||
[build-dependencies] | ||
codegen-0_7_3 = {package = "molecule-codegen", version = "0.7.3", features = ["compiler-plugin"]} | ||
codegen-dev = {package = "molecule-codegen", path = "../tools/codegen", features = ["compiler-plugin"]} | ||
molecule = "0.7.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
fn compile_schema_0_7_3(schema: &str) { | ||
let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("0_7_3"); | ||
std::fs::create_dir_all(&out_dir).unwrap(); | ||
|
||
let mut compiler = codegen_0_7_3::Compiler::new(); | ||
compiler | ||
.input_schema_file(schema) | ||
.generate_code(codegen_0_7_3::Language::Rust) | ||
.output_dir(out_dir) | ||
.run() | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", schema); | ||
} | ||
|
||
fn compile_schema_dev(schema: &str) { | ||
let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("dev"); | ||
std::fs::create_dir_all(&out_dir).unwrap(); | ||
|
||
let mut compiler = codegen_dev::Compiler::new(); | ||
compiler | ||
.input_schema_file(schema) | ||
.generate_code(codegen_dev::Language::Rust) | ||
.output_dir(out_dir) | ||
.run() | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", schema); | ||
} | ||
|
||
fn compile_intermediate_0_7_3(schema: &str) { | ||
let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("0_7_3"); | ||
std::fs::create_dir_all(&out_dir).unwrap(); | ||
|
||
let mut compiler = codegen_0_7_3::Compiler::new(); | ||
compiler | ||
.input_schema_file(schema) | ||
.generate_intermediate(codegen_0_7_3::IntermediateFormat::JSON) | ||
.output_dir(out_dir) | ||
.run() | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", schema); | ||
} | ||
|
||
fn compile_intermediate_dev(schema: &str) { | ||
let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("dev"); | ||
std::fs::create_dir_all(&out_dir).unwrap(); | ||
|
||
let mut compiler = codegen_dev::Compiler::new(); | ||
compiler | ||
.input_schema_file(schema) | ||
.generate_intermediate(codegen_dev::IntermediateFormat::JSON) | ||
.output_dir(out_dir) | ||
.run() | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", schema); | ||
} | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=./union_foo_0_7_3.mol"); | ||
println!("cargo:rerun-if-changed=./union_foo_with_custom_id.mol"); | ||
|
||
compile_intermediate_0_7_3("./union_foo_0_7_3.mol"); | ||
compile_intermediate_dev("./union_foo_with_custom_id.mol"); | ||
|
||
compile_schema_0_7_3("./union_foo_0_7_3.mol"); | ||
compile_schema_dev("./union_foo_with_custom_id.mol"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod union_compatibility_test; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use molecule::prelude::*; | ||
|
||
static UNION_FOO_0_7_3_JSON_INTERMEDIATE: &str = | ||
include_str!(concat!(env!("OUT_DIR"), "/0_7_3/union_foo_0_7_3.json")); | ||
|
||
static UNION_FOO_DEV_JSON_INTERMEDIATE: &str = include_str!(concat!( | ||
env!("OUT_DIR"), | ||
"/dev/union_foo_with_custom_id.json" | ||
)); | ||
|
||
#[test] | ||
fn test_recover_0_7_3_intermediate_by_current_ir_recover() { | ||
let format = codegen_dev::IntermediateFormat::JSON; | ||
let ast_result = format.recover(UNION_FOO_0_7_3_JSON_INTERMEDIATE.as_bytes()); | ||
assert!(ast_result.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_recover_ir() { | ||
let format = codegen_dev::IntermediateFormat::JSON; | ||
let ast_result = format.recover(UNION_FOO_DEV_JSON_INTERMEDIATE.as_bytes()); | ||
assert!(ast_result.is_ok()); | ||
} | ||
|
||
mod union_foo_0_7_3 { | ||
#![allow(clippy::all, dead_code)] | ||
include!(concat!(env!("OUT_DIR"), "/0_7_3/union_foo_0_7_3.rs")); | ||
} | ||
|
||
mod union_foo_dev { | ||
#![allow(clippy::all, dead_code)] | ||
include!(concat!(env!("OUT_DIR"), "/dev/union_foo_with_custom_id.rs")); | ||
} | ||
|
||
#[test] | ||
fn test_decode_0_7_3_generated_rust_bytes_by_current_version() { | ||
let a2_0_7_3 = union_foo_0_7_3::A2::new_builder() | ||
.nth0(Byte::from(17)) | ||
.build(); | ||
|
||
let foo_0_7_3 = union_foo_0_7_3::Foo::new_builder() | ||
.set(a2_0_7_3.clone()) | ||
.build(); | ||
let foo_0_7_3_slice = foo_0_7_3.as_slice(); | ||
|
||
let foo_dev_result = union_foo_dev::FooOnlyReserveA2AndA3::from_slice(foo_0_7_3_slice); | ||
assert!(foo_dev_result.is_ok()); | ||
let foo_dev = foo_dev_result.unwrap(); | ||
|
||
let foo_union_dev = foo_dev.to_enum(); | ||
|
||
if let union_foo_dev::FooOnlyReserveA2AndA3Union::A2(a2_dev) = foo_union_dev { | ||
assert_eq!(a2_0_7_3.as_slice(), a2_dev.as_slice()); | ||
} else { | ||
panic!("foo_union_dev should be A2"); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_decode_0_7_3_generated_deprecated_rust_bytes_by_current_version() { | ||
let a0_0_7_3 = union_foo_0_7_3::A0::new_builder() | ||
.nth0(Byte::from(133)) | ||
.build(); | ||
|
||
let foo_0_7_3 = union_foo_0_7_3::Foo::new_builder().set(a0_0_7_3).build(); | ||
let foo_0_7_3_slice = foo_0_7_3.as_slice(); | ||
|
||
let foo_dev_result = union_foo_dev::FooOnlyReserveA2AndA3::from_slice(foo_0_7_3_slice); | ||
assert!(foo_dev_result.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
array a0 [byte;1]; | ||
array a1 [byte;2]; | ||
array a2 [byte;3]; | ||
array a3 [byte;4]; | ||
|
||
union Foo { | ||
a0, | ||
a1, | ||
a2, | ||
a3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
array a2 [byte;3]; | ||
array a3 [byte;4]; | ||
|
||
union Foo_Only_Reserve_a2_and_a3{ | ||
a2 : 2, | ||
a3 : 3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.