1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//! Execute JavaScript at compile time to generate Rust code. Both evaluating expressions and //! custom derives are supported. //! //! ## eval //! //! ```rust //! use ctjs::eval; //! //! const X: f64 = eval! { //! // This is JavaScript //! const x = 5; //! String(x * Math.PI) //! }; //! //! assert!(X > 15.0); //! assert!(X < 16.0); //! ``` //! //! ## Custom Derive //! //! ```rust //! use ctjs::JsMacro; //! //! #[derive(Debug, JsMacro)] //! #[js_macro = "fruit_derive"] //! enum Fruit { //! #[js(name = "granny smith")] //! Apple, //! Orange, //! Pear, //! } //! //! fruit_derive! { //! js!( //! let output = "const _: () = {\n"; //! output += "use std::fmt::{self, Write};\n"; //! // name is "Fruit" //! output += "impl fmt::Display for " + name + "{\n"; //! output += "fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {\n"; //! output += "write!(f, \"{}\", match self {\n"; //! // ident is "Apple" or "Orange" or "Pear" //! for (const { ident, attrs } of item.variants) { //! let string = '"' + ident.toLowerCase() + '"'; //! const kv = ctjs.parse_attrs(attrs); //! if (kv.name) { //! string = kv.name; //! } //! //! output += "Self::" + ident + " => " + string + ",\n"; //! } //! output += "})\n"; //! output += "}\n}\n};\n"; //! output //! ) //! } //! //! let fruits = vec![Fruit::Apple, Fruit::Orange, Fruit::Pear]; //! for fruit in &fruits { //! println!("Debug: {:?}, Display: {}", fruit, fruit); //! } //! //! assert_eq!(&fruits[0].to_string(), "granny smith"); //! assert_eq!(&fruits[1].to_string(), "orange"); //! assert_eq!(&fruits[2].to_string(), "pear"); //! ``` pub use ctjs_macros::*; #[cfg(test)] mod tests { use ctjs_macros::eval; use ctjs_macros::JsMacro; #[test] fn it_works() { let y = eval! { let x = 3; x * 3.5 }; assert_eq!(y, 10.5); } #[test] fn it_can_generate_sin_table() { let nums: Vec<f64> = eval! { const values = Array.from({ length: 30 }, (x, i) => Math.sin(i / (Math.PI * 2))); "vec![" + values.map(value => value % 1 === 0 ? value + ".0" : value) + "]" }; assert_eq!(nums.len(), 30); } #[test] fn it_can_generate_sin_table_with_helpers() { let nums: Vec<f64> = eval! { const values = ctjs.range(0, 30).map(x => Math.sin(x / (Math.PI * 2))); ctjs.vec(values.map(ctjs.float)) }; assert_eq!(nums.len(), 30); // println!("Nums: {:#?}", nums); // assert_eq!("making test fail to see stdout", ""); } #[test] fn it_can_generate_sin_table_with_raw_string() { let nums: Vec<f64> = eval! {r#" const values = Array.from({ length: 30 }, (x, i) => Math.sin(i / (Math.PI * 2))); `vec![${values.map(value => value % 1 === 0 ? value + ".0" : value)}]` "#}; assert_eq!(nums.len(), 30); // println!("Nums: {:#?}", nums); // assert_eq!("making test fail to see stdout", ""); } #[test] fn it_handles_multiple_strings() { let foo: u8 = eval! { concat!( r#" let x = 42; "#, r#" x + '_u8' "# ) }; assert_eq!(foo, 42); } #[test] fn it_can_derive_simple() { #[derive(Debug, JsMacro)] #[js_macro = "simple"] struct TestStruct { pub s: String, } simple! { js!("static NAME: &str = " + ctjs.str(name) + ";") } assert_eq!(NAME, "TestStruct"); } } // fn example_test() { // #[derive(Debug, JsMacro)] // #[js_macro = "example"] // enum TestStruct { // Foo(String), // #[js_macro(example = "val")] // Bar { // something: &'static str, // }, // } // example! { js!("static NAME: &str = " + ctjs.str(name) + ";") } // example! { js!("static JSON: &str = " + ctjs.str(ctjs.json(item)) + ";" ) } // // assert_eq!(NAME, "TestStruct"); // }