diff --git a/rust/lib/voyageurs/src/listener.rs b/rust/lib/voyageurs/src/listener.rs index 78a4f4a..3f82095 100644 --- a/rust/lib/voyageurs/src/listener.rs +++ b/rust/lib/voyageurs/src/listener.rs @@ -9,6 +9,7 @@ use mammoth::thread::Thread; use mammoth::zion::ZError; #[repr(u8)] +#[allow(dead_code)] #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] enum Keycode { UnknownKeycode = 0x0, diff --git a/yunq/rust/src/codegen.rs b/yunq/rust/src/codegen.rs index 00d2a64..53560a6 100644 --- a/yunq/rust/src/codegen.rs +++ b/yunq/rust/src/codegen.rs @@ -413,33 +413,59 @@ fn generate_interface(interface: &Interface) -> TokenStream { } } +fn any_strings(ast: &Vec) -> bool { + ast.iter() + .filter_map(|decl| match decl { + Decl::Message(m) => Some(m), + _ => None, + }) + .flat_map(|message| message.fields.iter()) + .any(|field| field.field_type.inner_type == Type::String) +} + +fn any_interfaces(ast: &Vec) -> bool { + ast.iter().any(|decl| match decl { + Decl::Interface(_) => true, + _ => false, + }) +} + pub fn generate_code(ast: &Vec) -> String { + let str_imports = if any_strings(ast) { + quote! { + use alloc::string::String; + use alloc::string::ToString; + } + } else { + quote! {} + }; + + let interface_imports = if any_interfaces(ast) { + quote! { + use alloc::boxed::Box; + use core::ffi::c_void; + use mammoth::cap::Capability; + use mammoth::syscall; + use mammoth::thread; + use yunq::server::YunqServer; + } + } else { + quote! {} + }; + let prelude = quote! { extern crate alloc; - use alloc::string::String; - use alloc::string::ToString; use alloc::vec::Vec; use mammoth::zion::z_cap_t; use mammoth::zion::ZError; use yunq::ByteBuffer; use yunq::YunqMessage; + #str_imports - // Used only by modules with an interface. - #[allow(unused_imports)] - use alloc::boxed::Box; - #[allow(unused_imports)] - use core::ffi::c_void; - #[allow(unused_imports)] - use mammoth::cap::Capability; - #[allow(unused_imports)] - use mammoth::syscall; - #[allow(unused_imports)] - use mammoth::thread; - #[allow(unused_imports)] - use yunq::server::YunqServer; + #interface_imports }; diff --git a/yunq/rust/src/parser.rs b/yunq/rust/src/parser.rs index 07841f8..fc9ac19 100644 --- a/yunq/rust/src/parser.rs +++ b/yunq/rust/src/parser.rs @@ -6,7 +6,7 @@ use std::fmt::Display; use crate::lexer::Token; use crate::lexer::TokenType; -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum Type { U64, I64,