@@ -187,12 +187,31 @@ fn generate_typescript_enum_encoders(item: ItemEnum, output: &mut String) {
187187 output. push_str ( "}\n " ) ;
188188}
189189
190+ /// Extract the PacketID from struct attributes and format it for TypeScript
191+ fn get_packet_id ( attrs : & [ Attribute ] ) -> Option < String > {
192+ for attr in attrs {
193+ if attr. path ( ) . is_ident ( "bufferfish" ) {
194+ if let Meta :: List ( list) = & attr. meta {
195+ let tokens = list. tokens . to_string ( ) ;
196+
197+ let cleaned = tokens. trim ( ) . replace ( " " , "" ) . replace ( "::" , "." ) ;
198+
199+ return Some ( cleaned) ;
200+ }
201+ }
202+ }
203+ None
204+ }
205+
206+ /// Generate TypeScript encoders for structs including PacketID if specified
190207fn generate_typescript_struct_encoders ( item : ItemStruct , output : & mut String ) {
191208 let struct_name = item. ident . to_string ( ) ;
209+ let packet_id = get_packet_id ( & item. attrs ) ;
192210
193211 match & item. fields {
194212 Fields :: Named ( fields_named) => {
195- if fields_named. named . is_empty ( ) {
213+ // For empty structs, only generate encoder if we have a PacketID
214+ if fields_named. named . is_empty ( ) && packet_id. is_none ( ) {
196215 return ;
197216 }
198217
@@ -204,6 +223,11 @@ fn generate_typescript_struct_encoders(item: ItemStruct, output: &mut String) {
204223 . as_str ( ) ,
205224 ) ;
206225
226+ if let Some ( id) = & packet_id {
227+ output. push_str ( format ! ( " encodePacketId(bf, {})\n " , id) . as_str ( ) ) ;
228+ }
229+
230+ // Write struct fields
207231 for field in & fields_named. named {
208232 if let Some ( field_name) = & field. ident {
209233 let field_ts_name = snake_to_camel_case ( field_name. to_string ( ) ) ;
@@ -223,7 +247,8 @@ fn generate_typescript_struct_encoders(item: ItemStruct, output: &mut String) {
223247 output. push_str ( "}\n " ) ;
224248 }
225249 Fields :: Unnamed ( fields_unnamed) => {
226- if fields_unnamed. unnamed . is_empty ( ) {
250+ // For empty tuple structs, only generate encoder if we have a PacketID
251+ if fields_unnamed. unnamed . is_empty ( ) && packet_id. is_none ( ) {
227252 return ;
228253 }
229254
@@ -235,6 +260,11 @@ fn generate_typescript_struct_encoders(item: ItemStruct, output: &mut String) {
235260 . as_str ( ) ,
236261 ) ;
237262
263+ if let Some ( id) = & packet_id {
264+ output. push_str ( format ! ( " encodePacketId(bf, {})\n " , id) . as_str ( ) ) ;
265+ }
266+
267+ // Write tuple fields
238268 for ( i, field) in fields_unnamed. unnamed . iter ( ) . enumerate ( ) {
239269 output. push_str (
240270 format ! (
@@ -247,7 +277,20 @@ fn generate_typescript_struct_encoders(item: ItemStruct, output: &mut String) {
247277
248278 output. push_str ( "}\n " ) ;
249279 }
250- Fields :: Unit => { }
280+ Fields :: Unit => {
281+ // For unit structs, only generate encoder if we have a PacketID
282+ if let Some ( id) = packet_id {
283+ output. push_str (
284+ format ! (
285+ "\n export function encode{}(bf: Bufferfish, _value: {}): void {{\n " ,
286+ struct_name, struct_name
287+ )
288+ . as_str ( ) ,
289+ ) ;
290+ output. push_str ( format ! ( " encodePacketId(bf, {})\n " , id) . as_str ( ) ) ;
291+ output. push_str ( "}\n " ) ;
292+ }
293+ }
251294 }
252295}
253296
@@ -639,10 +682,15 @@ export function decodeJoinPacket(bf: Bufferfish): JoinPacket {
639682}
640683
641684export function encodeJoinPacket(bf: Bufferfish, value: JoinPacket): void {
685+ encodePacketId(bf, PacketId.Join)
642686 bf.writeUint8(value.id)
643687 bf.writeString(value.username)
644688}
645689
690+ export function encodeLeavePacket(bf: Bufferfish, _value: LeavePacket): void {
691+ encodePacketId(bf, PacketId.Leave)
692+ }
693+
646694export type UnknownPacket = [number, number]
647695
648696export function decodeUnknownPacket(bf: Bufferfish): UnknownPacket {
@@ -653,6 +701,7 @@ export function decodeUnknownPacket(bf: Bufferfish): UnknownPacket {
653701}
654702
655703export function encodeUnknownPacket(bf: Bufferfish, value: UnknownPacket): void {
704+ encodePacketId(bf, PacketId.Unknown)
656705 bf.writeUint8(value[0])
657706 bf.writeUint16(value[1])
658707}
0 commit comments