From 3c53d0dceec06344d4b8451742d0c2cc3625406e Mon Sep 17 00:00:00 2001 From: Lenscas Date: Fri, 15 Nov 2024 10:27:57 +0100 Subject: [PATCH] fix not compiling without enabled features --- .github/workflows/ci.yml | 65 +++++++++++++++++++------------------- src/export_instance.rs | 4 ++- src/exported_function.rs | 11 ++++--- src/lib.rs | 57 ++++++++++++++++++++++----------- src/teal_multivalue.rs | 9 ++++-- src/type_generator.rs | 64 +++++++++++++++++++++++-------------- src/type_representation.rs | 32 ++++++++++++------- src/type_walker.rs | 28 ++++++++++------ 8 files changed, 166 insertions(+), 104 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8aae435..b3ee118 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,58 +3,59 @@ on: pull_request: paths: - "**/*.rs" - - 'Cargo.toml' - - 'tealr/Cargo.lock' - - 'tealr_derive/Cargo.toml' - - '.github/workflows/ci.yml' + - "Cargo.toml" + - "tealr/Cargo.lock" + - "tealr_derive/Cargo.toml" + - ".github/workflows/ci.yml" jobs: formatting: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: hecrj/setup-rust-action@v1 - with: - rust-version: stable - components: rustfmt - - name: Formatting check - run: cargo fmt --all -- --check + - uses: actions/checkout@v2 + - uses: hecrj/setup-rust-action@v1 + with: + rust-version: stable + components: rustfmt + - name: Formatting check + run: cargo fmt --all -- --check test: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macOS-latest] - tests : + tests: - "cargo test --features mlua,mlua_vendored,mlua_lua53,mlua_async,mlua_send" - "cargo test --features mlua,mlua_vendored,mlua_lua54,mlua_async,mlua_send" - "cargo test --features mlua,mlua_vendored,mlua_lua53,mlua_async --no-run" - "cargo test --features mlua,mlua_vendored,mlua_lua54,mlua_async --no-run" + - "cargo build" steps: - - uses: actions/checkout@master - - uses: hecrj/setup-rust-action@v1.3.4 - with: - rust-version: stable - - uses: leafo/gh-actions-lua@v8.0.0 - with: - luaVersion: "5.4" - - uses: leafo/gh-actions-luarocks@v4.0.0 - - name: Install dependencies - run: luarocks install tl - - name: run tests - run: ${{matrix.tests}} + - uses: actions/checkout@master + - uses: hecrj/setup-rust-action@v1.3.4 + with: + rust-version: stable + - uses: leafo/gh-actions-lua@v8.0.0 + with: + luaVersion: "5.4" + - uses: leafo/gh-actions-luarocks@v4.0.0 + - name: Install dependencies + run: luarocks install tl + - name: run tests + run: ${{matrix.tests}} clippy: runs-on: ubuntu-latest strategy: matrix: - tests : + tests: - "cargo clippy --features mlua,mlua_vendored,mlua_lua54,mlua_async,mlua_send -- -D warnings" - "cargo clippy --features mlua,mlua_vendored,mlua_lua53,mlua_async,mlua_send -- -D warnings" - "cargo clippy --features mlua,mlua_vendored,mlua_luau,mlua_async,mlua_send -- -D warnings" steps: - - uses: hecrj/setup-rust-action@v1.3.4 - with: - rust-version: stable - components: clippy - - uses: actions/checkout@master - - name: Clippy - run: ${{matrix.tests}} + - uses: hecrj/setup-rust-action@v1.3.4 + with: + rust-version: stable + components: clippy + - uses: actions/checkout@master + - name: Clippy + run: ${{matrix.tests}} diff --git a/src/export_instance.rs b/src/export_instance.rs index ec36f4b..14f595b 100644 --- a/src/export_instance.rs +++ b/src/export_instance.rs @@ -6,7 +6,7 @@ pub(crate) struct InstanceWalker { doc: String, pub(crate) instances: Vec, } - +#[cfg(feature = "mlua")] impl crate::mlu::InstanceCollector for InstanceWalker { fn add_instance(&mut self, global_name: P, _: F) -> Result<&mut Self, mlua::Error> where @@ -30,6 +30,7 @@ impl InstanceWalker { instances: Default::default(), } } + #[allow(dead_code)] fn add_instance(&mut self, name: Cow<'static, str>) { let teal_type = T::get_type_parts_as_global(); let z = T::get_type_kind(); @@ -43,6 +44,7 @@ impl InstanceWalker { ty: T::to_typename(), }); } + #[allow(dead_code)] fn document_instance(&mut self, doc: &'static str) { self.doc.push_str(doc); self.doc.push('\n'); diff --git a/src/exported_function.rs b/src/exported_function.rs index 87bd0b5..3897efb 100644 --- a/src/exported_function.rs +++ b/src/exported_function.rs @@ -5,14 +5,17 @@ use std::{ }; use crate::{get_generics, type_generator::NameContainer, FunctionParam, Name, NamePart, Type}; - +#[allow(dead_code)] type X = Vec; ///Contains the data needed to write down the type of a function #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "derive", feature = "mlua"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "derive", feature = "mlua"), tealr(tealr_name = crate) )] pub struct ExportedFunction { @@ -21,7 +24,7 @@ pub struct ExportedFunction { ///The full layout of the function based on teal's syntax #[deprecated] #[cfg_attr( - feature = "derive", + all(feature = "derive", feature = "mlua"), tealr(remote = X) )] pub signature: Cow<'static, [crate::NamePart]>, diff --git a/src/lib.rs b/src/lib.rs index 376e060..fe56875 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![doc = include_str!("../README.md")] ///traits and types specific to mlua +#[cfg(feature = "mlua")] pub mod mlu; mod export_instance; @@ -44,15 +45,18 @@ pub fn get_tealr_version() -> &'static str { } #[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///The name of a type pub struct Name( #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = String))] pub Cow<'static, str>, ); @@ -68,9 +72,12 @@ impl> From for Name { } } #[derive(Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///A singular type @@ -81,9 +88,12 @@ pub struct SingleType { pub kind: KindOfType, } #[derive(Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///A parameter for a function @@ -94,9 +104,12 @@ pub struct FunctionParam { pub ty: Type, } #[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///The representation of a function type @@ -107,31 +120,37 @@ pub struct FunctionRepresentation { pub returns: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///The representation of a Map type pub struct MapRepresentation { #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = Type))] ///The type of the key pub key: Box, #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = Type))] ///The type of the value pub value: Box, } - +#[allow(dead_code)] type NewTypeArray = Vec; #[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] ///A type @@ -145,14 +164,14 @@ pub enum Type { ///The type is a union (A | B) Or( #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = NewTypeArray))] Vec, ), ///The type is an array Array( #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = Type))] Box, ), @@ -161,7 +180,7 @@ pub enum Type { ///As it can _easily_ break things Tuple( #[cfg_attr( - feature = "derive", + all(feature = "mlua",feature = "derive"), tealr(remote = NewTypeArray))] Vec, ), diff --git a/src/teal_multivalue.rs b/src/teal_multivalue.rs index e7b089c..1f42b38 100644 --- a/src/teal_multivalue.rs +++ b/src/teal_multivalue.rs @@ -4,15 +4,18 @@ use crate::{type_representation::KindOfType, ToTypename, Type}; ///Represents a type #[derive(Debug, PartialEq, Eq, Clone, Hash, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - all(feature = "derive"), + all(feature = "derive", feature = "mlua"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua",feature = "derive"), tealr(tealr_name = crate) )] pub struct TealType { ///Name of the type #[cfg_attr( - all(feature = "derive"), + all(feature = "mlua", feature = "derive"), tealr(remote = String))] pub name: Cow<'static, str>, ///If the type is build in, a generic or from a library diff --git a/src/type_generator.rs b/src/type_generator.rs index afe7a3c..b279ca7 100644 --- a/src/type_generator.rs +++ b/src/type_generator.rs @@ -5,17 +5,17 @@ use std::{ string::FromUtf8Error, }; -use serde::{Deserialize, Serialize}; - +#[cfg(feature = "mlua")] use crate::mlu::{ - get_meta_name as get_meta_name_mlua, MaybeSend, TealData as TealDataM, TealDataFields, - TealDataMethods as TealDataMethodsM, -}; -use mlua::{ - FromLua as FromLuaM, FromLuaMulti as FromLuaMultiM, IntoLua as ToLuaM, - IntoLuaMulti as ToLuaMultiM, Lua, MetaMethod as MetaMethodM, Result as ResultM, - UserData as UserDataM, + get_meta_name as get_meta_name_mlua, + mlua::{ + FromLua as FromLuaM, FromLuaMulti as FromLuaMultiM, IntoLua as ToLuaM, + IntoLuaMulti as ToLuaMultiM, Lua, MetaMethod as MetaMethodM, Result as ResultM, + UserData as UserDataM, + }, + MaybeSend, TealData as TealDataM, TealDataFields, TealDataMethods as TealDataMethodsM, }; +use serde::{Deserialize, Serialize}; use crate::{ exported_function::ExportedFunction, type_parts_to_str, NamePart, ToTypename, Type, TypeName, @@ -41,6 +41,7 @@ impl ToTypename for NameContainer { } } +#[cfg(feature = "mlua")] impl FromLuaM for NameContainer { fn from_lua(lua_value: mlua::Value, lua: &Lua) -> ResultM { Ok(::from_lua(lua_value, lua)? @@ -48,7 +49,7 @@ impl FromLuaM for NameContainer { .into()) } } - +#[cfg(feature = "mlua")] impl ToLuaM for NameContainer { fn into_lua(self, lua: &Lua) -> ResultM { lua.create_string(self.0).and_then(|x| x.into_lua(lua)) @@ -84,7 +85,7 @@ impl From> for NameContainer { a.as_bytes().to_owned().into() } } - +#[allow(dead_code)] pub(crate) fn get_method_data>( name: S, is_meta_method: bool, @@ -94,16 +95,19 @@ pub(crate) fn get_method_data, ), @@ -119,20 +123,23 @@ impl TypeGenerator { } } } - +#[allow(dead_code)] type V = Vec; ///contains all the information needed to create a teal enum. #[derive(Clone, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] pub struct EnumGenerator { ///the name of this enum #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = V) )] pub name: Cow<'static, [NamePart]>, @@ -177,9 +184,12 @@ impl EnumGenerator { } #[derive(serde::Serialize, serde::Deserialize, Clone)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] @@ -190,7 +200,7 @@ pub struct Field { ///the type of the field, according to the old format #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = V) )] pub teal_type: Cow<'static, [NamePart]>, @@ -224,9 +234,12 @@ impl From for (NameContainer, Cow<'static, [NamePart]>) { ///contains all the information needed to create a record #[derive(Default, Clone, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] @@ -237,7 +250,7 @@ pub struct RecordGenerator { pub is_user_data: bool, ///The name of the type in teal #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = V) )] pub type_name: Cow<'static, [NamePart]>, @@ -502,6 +515,8 @@ impl RecordGenerator { } } +#[cfg(feature = "mlua")] + impl TealDataMethodsM for RecordGenerator where T: 'static + TealDataM + UserDataM + ToTypename, @@ -661,6 +676,7 @@ where } } +#[cfg(feature = "mlua")] impl TealDataFields for RecordGenerator where T: 'static + TealDataM + UserDataM + ToTypename, diff --git a/src/type_representation.rs b/src/type_representation.rs index db83b0a..c3aea65 100644 --- a/src/type_representation.rs +++ b/src/type_representation.rs @@ -1,4 +1,6 @@ +#[allow(unused_imports)] use crate::{FunctionParam, MapRepresentation, SingleType, ToTypename, Type}; +#[allow(unused_macros)] macro_rules! impl_type_name_life_time { ($teal_type:literal $current_type:ty) => { impl ToTypename for $current_type { @@ -31,9 +33,12 @@ macro_rules! impl_type_name { ///Keeps track of any special treatment a type needs to get #[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] pub enum KindOfType { @@ -118,9 +123,12 @@ macro_rules! new_type { }; } #[derive(Debug, Clone, PartialEq, Hash, Eq, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] ///The parts that a name consists of @@ -129,7 +137,7 @@ pub enum NamePart { ///An example could be the `function(` part inside `function(integer):string` Symbol( #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = String))] Cow<'static, str>, ), @@ -251,7 +259,7 @@ impl_type_name!("boolean" bool); impl_type_name!("string" String,std::ffi::CString,bstr::BString ,&str,&std::ffi::CStr,&bstr::BStr); impl_type_name!("number" f32,f64); impl_type_name!("integer" i8,u8,u16,i16,u32,i32,u64,i64,u128,i128,isize,usize); - +#[cfg(feature = "mlua")] impl_type_name_life_time!("thread" mlua::Thread); #[cfg(feature = "mlua_async")] @@ -260,11 +268,11 @@ impl ToTypename for mlua::AsyncThread { Type::new_single("thread", KindOfType::Builtin) } } - +#[cfg(feature = "mlua")] impl_type_name_life_time!("any" mlua::Value); - +#[cfg(feature = "mlua")] use mlua::{Table as TableM, Value as ValueM}; - +#[cfg(feature = "mlua")] impl ToTypename for TableM { fn to_typename() -> Type { Type::Map(crate::MapRepresentation { @@ -273,11 +281,11 @@ impl ToTypename for TableM { }) } } - +#[cfg(feature = "mlua")] impl_type_name_life_time!("string" mlua::String); - +#[cfg(feature = "mlua")] use mlua::Function as FunctionM; - +#[cfg(feature = "mlua")] impl ToTypename for FunctionM { fn to_typename() -> Type { Type::Function(crate::FunctionRepresentation { diff --git a/src/type_walker.rs b/src/type_walker.rs index fdc4641..8a6a822 100644 --- a/src/type_walker.rs +++ b/src/type_walker.rs @@ -1,25 +1,28 @@ use std::{borrow::Cow, string::FromUtf8Error}; use crate::{type_parts_to_str, NamePart, ToTypename, Type, TypeBody, TypeGenerator}; - +#[allow(dead_code)] type V = Vec; #[derive(Clone, serde::Serialize, serde::Deserialize)] ///Used to document what global instances get made by the module -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] pub struct GlobalInstance { ///name of the global #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = String))] pub name: Cow<'static, str>, ///the type according to the old format #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), tealr(remote = V))] pub teal_type: Cow<'static, [NamePart]>, ///the type @@ -32,9 +35,12 @@ pub struct GlobalInstance { #[derive(Clone, serde::Serialize, serde::Deserialize)] ///Used to document what global instances get made by the module -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] pub struct ExtraPage { @@ -46,9 +52,12 @@ pub struct ExtraPage { ///This generates the .d.tl files #[derive(Clone, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "derive", derive(crate::mlu::FromToLua, crate::ToTypename))] #[cfg_attr( - feature = "derive", + all(feature = "mlua", feature = "derive"), + derive(crate::mlu::FromToLua, crate::ToTypename) +)] +#[cfg_attr( + all(feature = "mlua", feature = "derive"), tealr(tealr_name = crate) )] pub struct TypeWalker { @@ -220,6 +229,7 @@ impl TypeWalker { } impl TypeWalker { + #[cfg(feature = "mlua")] ///collect every instance that is getting shared with lua pub fn document_global_instance( mut self,