├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── awful-idea ├── .gitignore ├── Cargo.toml └── src │ ├── main.rs │ └── tests │ ├── macros.rs │ └── mod.rs ├── lua-macro-impl ├── .gitignore ├── Cargo.toml └── src │ └── lib.rs ├── lua-macro ├── .gitignore ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ ├── lib.rs │ └── run.rs └── rust-toolchain.toml /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "lua-macro", 4 | "lua-macro-impl", 5 | "awful-idea" 6 | ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Rin 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # inline-lua 2 | [![crates.io](https://img.shields.io/crates/v/lua-macro?style=for-the-badge)](https://crates.io/crates/lua-macro) 3 | Have you ever wanted to embed Lua inline into your Rust code? 4 | *...No?* Huh, you can now though 5 | 6 | ## How to use 7 | Import `lua_macro::lua` and then simply use `lua!` to embed your code. -------------------------------------------------------------------------------- /awful-idea/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /awful-idea/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "awful-idea" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | lua-macro = {path="../lua-macro"} -------------------------------------------------------------------------------- /awful-idea/src/main.rs: -------------------------------------------------------------------------------- 1 | use lua_macro::{lua, lua_eval}; 2 | 3 | #[cfg(test)] 4 | mod tests; 5 | 6 | fn main() { 7 | println!("hi from rust"); 8 | lua! { 9 | function say_hi() 10 | print("and hi from lua!") 11 | end 12 | 13 | say_hi() 14 | }; 15 | 16 | let a: i32 = lua_eval! { 17 | 2 + 2 18 | }; 19 | 20 | println!("{a}"); 21 | } -------------------------------------------------------------------------------- /awful-idea/src/tests/macros.rs: -------------------------------------------------------------------------------- 1 | macro_rules! it_must { 2 | ($func_name:ident $code:block) => { 3 | #[test] 4 | fn $func_name() { 5 | assert!($code) 6 | } 7 | }; 8 | 9 | ($($fn:ident $code:block)+) => { 10 | $( 11 | it_must! { 12 | $fn $code 13 | } 14 | )+ 15 | } 16 | } 17 | 18 | pub(crate) use it_must; -------------------------------------------------------------------------------- /awful-idea/src/tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod macros; 2 | 3 | use lua_macro::{lua, lua_eval}; 4 | use macros::it_must; 5 | 6 | it_must! { 7 | run_some_lua_code { 8 | lua! { 9 | local a = 2 + 2 10 | assert(a == 4) 11 | } 12 | true 13 | } 14 | 15 | do_math_correctly { 16 | let a: i32 = lua_eval! { 17 | 2 + 2 18 | }; 19 | a == 4 20 | } 21 | } -------------------------------------------------------------------------------- /lua-macro-impl/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /lua-macro-impl/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lua-macro-impl" 3 | version = "0.1.0" 4 | edition = "2021" 5 | description = "Proc macro used by lua-macro" 6 | repository = "https://github.com/ry00001/inline-lua" 7 | license = "MIT" 8 | keywords = [] 9 | categories = [] 10 | 11 | [lib] 12 | proc-macro = true 13 | 14 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 15 | 16 | [dependencies] 17 | quote = "1.0.15" -------------------------------------------------------------------------------- /lua-macro-impl/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(proc_macro_span)] 2 | #![feature(proc_macro_hygiene)] 3 | 4 | extern crate proc_macro; 5 | use proc_macro::TokenStream; 6 | 7 | #[macro_use] 8 | extern crate quote; 9 | 10 | #[proc_macro] 11 | pub fn lua(input: TokenStream) -> TokenStream { 12 | let mut tokens = input.into_iter(); 13 | let mut span = tokens.next().unwrap().span(); 14 | 15 | while let Some(tk) = tokens.next() { 16 | span = span.join(tk.span()).unwrap(); 17 | } 18 | 19 | let src = span.source_text().unwrap(); 20 | 21 | quote!( lua_macro::run_lua::<()>(#src); ).into() 22 | } 23 | 24 | #[proc_macro] 25 | pub fn lua_eval(input: TokenStream) -> TokenStream { 26 | let mut tokens = input.into_iter(); 27 | let mut span = tokens.next().unwrap().span(); 28 | 29 | while let Some(tk) = tokens.next() { 30 | span = span.join(tk.span()).unwrap(); 31 | } 32 | 33 | let src = span.source_text().unwrap(); 34 | 35 | quote!( lua_macro::run_lua::<_>(#src) ).into() 36 | } -------------------------------------------------------------------------------- /lua-macro/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /lua-macro/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lua-macro" 3 | version = "0.1.1" 4 | edition = "2021" 5 | description = "Embed Lua code directly into your Rust, for some reason" 6 | repository = "https://github.com/ry00001/inline-lua" 7 | license = "MIT" 8 | keywords = ["cursed", "lua", "embed", "another-language"] 9 | categories = ["development-tools", "development-tools::ffi", "compilers"] 10 | 11 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 12 | 13 | [dependencies] 14 | mlua = { version = "0.7.4", features = ["lua54"] } 15 | quote = "1.0.15" 16 | lua-macro-impl = { path = "../lua-macro-impl", version = "^0.1.0" } 17 | -------------------------------------------------------------------------------- /lua-macro/README.md: -------------------------------------------------------------------------------- 1 | # lua-macro 2 | Have you ever wanted to embed Lua code straight into Rust? 3 | 4 | No? 5 | 6 | Well, you can now lol 7 | 8 | ## How to use 9 | Import `lua_macro::lua` and use `lua! { your code }` to insert some Lua. 10 | To evaluate an expression, use `lua_macro::lua_eval!`. -------------------------------------------------------------------------------- /lua-macro/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" -------------------------------------------------------------------------------- /lua-macro/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod run; 2 | pub use lua_macro_impl::lua; 3 | pub use lua_macro_impl::lua_eval; 4 | pub use run::run_lua; -------------------------------------------------------------------------------- /lua-macro/src/run.rs: -------------------------------------------------------------------------------- 1 | use mlua::prelude::*; 2 | 3 | #[allow(dead_code)] 4 | pub fn run_lua<'a, R>(code: &str) -> R 5 | where R: for<'local> FromLuaMulti<'local> + Clone 6 | { 7 | let lua = Lua::new(); 8 | let res: Result = lua.load(code).eval(); 9 | res.unwrap().clone() 10 | } -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" --------------------------------------------------------------------------------