From 31432ecd7ca5fc63c35b5270a6f9ce74ee1a6733 Mon Sep 17 00:00:00 2001 From: raven <7156279+RavenX8@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:55:35 -0500 Subject: [PATCH] - add: NullTerminatedString as a type for std::string unless it's a length restricted string --- generator/src/codegen/rust/codegen_source.rs | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/generator/src/codegen/rust/codegen_source.rs b/generator/src/codegen/rust/codegen_source.rs index c10104d..be0e4ef 100644 --- a/generator/src/codegen/rust/codegen_source.rs +++ b/generator/src/codegen/rust/codegen_source.rs @@ -37,6 +37,7 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> { cg!(self, r#"use bincode::de::read::Reader;"#); cg!(self, r#"use bincode::enc::write::Writer;"#); cg!(self, r#"use crate::packet::PacketPayload;"#); + cg!(self, r#"use crate::handlers::null_string::NullTerminatedString;"#); let mut iserialize: HashMap = HashMap::new(); iserialize.insert("int8_t".to_string(), "i8".to_string()); @@ -52,7 +53,7 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> { iserialize.insert("unsigned int".to_string(), "u32".to_string()); iserialize.insert("float".to_string(), "f32".to_string()); iserialize.insert("double".to_string(), "f64".to_string()); - iserialize.insert("std::string".to_string(), "String".to_string()); + iserialize.insert("std::string".to_string(), "NullTerminatedString".to_string()); for content in packet.contents() { @@ -289,11 +290,15 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> { }).is_some(); self.doc(restrict.doc())?; let base = restrict.base().trim().to_string(); - let rust_type = iserialize.get(restrict.base().trim()).unwrap_or_else(|| { + let mut rust_type = iserialize.get(restrict.base().trim()).map(|s| s.to_string()).unwrap_or_else(|| { debug!(r#"Type "{}" not found, outputting anyway"#, base); - &base + base.clone() }); + if "NullTerminatedString" == rust_type { + rust_type = "String".to_string(); + } + if is_enum { cg!(self, r#"#[repr({})]"#, rust_type); cg!(self, r#"#[derive(Debug, Clone)]"#); @@ -328,11 +333,15 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> { _ => false }).is_some(); let trimmed_type = restrict.base().trim().to_string(); - let rust_type = iserialize.get(restrict.base().trim()).unwrap_or_else(|| { + let mut rust_type = iserialize.get(restrict.base().trim()).map(|s| s.to_string()).unwrap_or_else(|| { debug!(r#"Type "{}" not found, outputting anyway"#, restrict.base()); - &trimmed_type + trimmed_type.clone() }); + if "NullTerminatedString" == rust_type { + rust_type = "String".to_string(); + } + cg!(self, "impl Encode for {} {{", name.to_upper_camel_case()); self.indent(); cg!(self, "fn encode(&self, encoder: &mut E) -> std::result::Result<(), bincode::error::EncodeError> {{"); @@ -380,11 +389,15 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> { _ => false }).is_some(); let trimmed_type = restrict.base().trim().to_string(); - let rust_type = iserialize.get(restrict.base().trim()).unwrap_or_else(|| { + let mut rust_type = iserialize.get(restrict.base().trim()).map(|s| s.to_string()).unwrap_or_else(|| { debug!(r#"Type "{}" not found, outputting anyway"#, restrict.base()); - &trimmed_type + trimmed_type.clone() }); + if "NullTerminatedString" == rust_type { + rust_type = "String".to_string(); + } + cg!(self, "impl Decode for {} {{", name.to_upper_camel_case()); self.indent(); cg!(self, "fn decode(decoder: &mut D) -> std::result::Result {{");