Skip to content

Commit 0dff44c

Browse files
committed
- fix: mapping C++ types to rust types
- remove: unused code
1 parent 12f8e16 commit 0dff44c

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

generator/src/codegen/rust/codegen_source.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use ::flat_ast::*;
22
use std::io::{Result, Write};
33
use ::heck::*;
4-
use std::collections::HashSet;
4+
use std::collections::HashMap;
5+
use schema::ast::Occurs::Unbounded;
56

67
pub (crate) struct CodeSourceGenerator<'a, W: Write + 'a> {
78
writer: &'a mut ::writer::Writer<W>,
@@ -40,39 +41,36 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
4041
cg!(self, r#"#[derive(Debug, Encode, Decode)]"#);
4142

4243
let iserialize = packet.contents().iter().filter_map(|elem| {
43-
if PacketContent::is_type(elem) {
44-
PacketContent::type_from_name(elem)
45-
} else {
46-
match elem {
47-
PacketContent::Element(ref e) => {
48-
match e.type_().as_ref() {
49-
"int8_t" => Some("i8".to_string()),
50-
"uint8_t" => Some("u8".to_string()),
51-
"int16_t" => Some("i16".to_string()),
52-
"uint16_t" => Some("u16".to_string()),
53-
"int32_t" => Some("i32".to_string()),
54-
"uint32_t" => Some("u32".to_string()),
55-
"int64_t" => Some("i64".to_string()),
56-
"uint64_t" => Some("u64".to_string()),
57-
"char" => Some("u8".to_string()),
58-
"float" => Some("f32".to_string()),
59-
"double" => Some("f64".to_string()),
60-
"std::string" => Some("String".to_string()),
61-
_ => Some(e.type_().to_string())
62-
}
63-
},
64-
_ => None
44+
match elem {
45+
PacketContent::Element(ref e) => {
46+
let rust_type = match e.type_().as_ref() {
47+
"int8_t" => "i8",
48+
"uint8_t" => "u8",
49+
"int16_t" => "i16",
50+
"uint16_t" => "u16",
51+
"int32_t" => "i32",
52+
"uint32_t" => "u32",
53+
"int64_t" => "i64",
54+
"uint64_t" => "u64",
55+
"char" => "u8",
56+
"float" => "f32",
57+
"double" => "f64",
58+
"std::string" => "String",
59+
_ => e.type_().as_str(),
60+
};
61+
Some((e.type_().to_string(), rust_type.to_string())) // Map key and value
6562
}
63+
_ => None,
6664
}
67-
}).collect::<::std::collections::HashSet<String>>();
65+
}).collect::<HashMap<String, String>>(); // Collect into a HashMap
6866

6967
// Need to drop out the struct
7068
cg!(self, "pub struct {} {{", packet.class_name());
7169
self.indent();
7270
for content in packet.contents() {
7371
use self::PacketContent::*;
7472
match content {
75-
Element(ref elem) => self.element(elem)?,
73+
Element(ref elem) => self.element(elem, &iserialize)?,
7674
_ => {}
7775
};
7876
}
@@ -100,7 +98,7 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
10098
Ok(())
10199
}
102100

103-
fn element(&mut self, elem: &Element) -> Result<()> {
101+
fn element(&mut self, elem: &Element, iserialize: &HashMap<String, String>) -> Result<()> {
104102
self.doc(elem.doc())?;
105103

106104
if let Some(bitset) = elem.bitset() {
@@ -110,16 +108,18 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
110108
return Ok(());
111109
}
112110

111+
let Some(rust_type) = iserialize.get(elem.type_()) else { warn!(r#"Type "{}" not found"#, elem.type_()); return Ok(()) };
112+
113113
let (type_, bits) = if let Some(ref o) = elem.occurs() {
114114
use ::flat_ast::Occurs::*;
115115
let type_ = match o {
116-
Unbounded => format!("Vec<{}>", elem.type_()),
117-
Num(n) => format!("[{}; {}]", elem.type_(), n)
116+
Unbounded => format!("Vec<{}>", rust_type),
117+
Num(n) => format!("[{}; {}]", rust_type, n)
118118
};
119119
(type_, "".to_string())
120120
} else {
121121
let bits = elem.bits().map_or_else(|| "".to_string(), |b| format!(" : {}", b));
122-
(elem.type_().to_owned(), bits)
122+
(rust_type.to_owned(), bits)
123123
};
124124
let default = match elem.init() {
125125
self::ElementInitValue::Default(d) => " = ".to_string() + d,
@@ -129,11 +129,3 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
129129
Ok(())
130130
}
131131
}
132-
133-
fn clean_base(base: &str) -> String {
134-
if base.contains("::") {
135-
base.split("::").skip(1).collect()
136-
} else {
137-
base.to_string()
138-
}
139-
}

0 commit comments

Comments
 (0)