Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feather/codegen/src/lib.rs at plugins · feather-rs/feather · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
feather-rs
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
142
Star
2.7k
Code
Issues
66
Pull requests
38
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
plugins
Breadcrumbs
feather
/
codegen
/
src
/
lib.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
295 lines (247 loc) · 8.15 KB
plugins
Breadcrumbs
feather
/
codegen
/
src
/
lib.rs
Copy path
Top
File metadata and controls
Code
Blame
295 lines (247 loc) · 8.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
extern
crate
proc_macro
;
#
[
macro_use
]
extern
crate
strum_macros
;
mod
entity_metadata
;
use
heck
::
SnakeCase
;
use
lazy_static
::
lazy_static
;
use
proc_macro
::
TokenStream
;
use
quote
::
quote
;
use
std
::
collections
::
HashMap
;
use
syn
::
export
::
Span
;
use
syn
::
Data
;
use
syn
::
Ident
;
use
syn
::
Type
;
use
syn
::
{
parse_macro_input
,
DeriveInput
}
;
#
[
proc_macro_derive
(
AsAny
)
]
pub
fn
derive_as_any
(
_item
:
TokenStream
)
->
TokenStream
{
let
parsed
:
DeriveInput
=
parse_macro_input
!
(
_item
as
DeriveInput
)
;
let
name =
&
parsed
.
ident
;
let
result =
quote
!
{
impl
AsAny
for
#name
{
fn
as_any
(
&
self
)
->
&
Any
{
self
}
}
}
;
result
.
into
(
)
}
#
[
derive
(
Clone
,
Copy
,
Debug
,
Eq
,
PartialEq
,
Hash
)
]
enum
PacketParameterType
{
Varint
,
Varlong
,
String
,
U64
,
U32
,
U16
,
U8
,
I64
,
I32
,
I16
,
I8
,
Position
,
Boolean
,
F32
,
F64
,
Uuid
,
Nbt
,
Slot
,
EntityMetadata
,
}
lazy_static
!
{
static
ref
PARAMETER_MAPPINGS
:
HashMap
<
&
'
static
str
,
PacketParameterType
> =
{
let
mut
m =
HashMap
::
new
(
)
;
m
.
insert
(
"VarInt"
,
PacketParameterType
::
Varint
)
;
m
.
insert
(
"VarLong"
,
PacketParameterType
::
Varlong
)
;
m
.
insert
(
"String"
,
PacketParameterType
::
String
)
;
m
.
insert
(
"u64"
,
PacketParameterType
::
U64
)
;
m
.
insert
(
"u32"
,
PacketParameterType
::
U32
)
;
m
.
insert
(
"u16"
,
PacketParameterType
::
U16
)
;
m
.
insert
(
"u8"
,
PacketParameterType
::
U8
)
;
m
.
insert
(
"i64"
,
PacketParameterType
::
I64
)
;
m
.
insert
(
"i32"
,
PacketParameterType
::
I32
)
;
m
.
insert
(
"i16"
,
PacketParameterType
::
I16
)
;
m
.
insert
(
"i8"
,
PacketParameterType
::
I8
)
;
m
.
insert
(
"BlockPosition"
,
PacketParameterType
::
Position
)
;
m
.
insert
(
"bool"
,
PacketParameterType
::
Boolean
)
;
m
.
insert
(
"f32"
,
PacketParameterType
::
F32
)
;
m
.
insert
(
"f64"
,
PacketParameterType
::
F64
)
;
m
.
insert
(
"Uuid"
,
PacketParameterType
::
Uuid
)
;
m
.
insert
(
"NbtTag"
,
PacketParameterType
::
Nbt
)
;
m
.
insert
(
"Slot"
,
PacketParameterType
::
Slot
)
;
m
.
insert
(
"EntityMetadata"
,
PacketParameterType
::
EntityMetadata
)
;
m
}
;
static
ref
FUNCTION_MAPPINGS
:
HashMap
<
PacketParameterType
,
&
'
static
str
> =
{
let
mut
m =
HashMap
::
new
(
)
;
m
.
insert
(
"var_int"
,
PacketParameterType
::
Varint
)
;
m
.
insert
(
"var_long"
,
PacketParameterType
::
Varlong
)
;
m
.
insert
(
"string"
,
PacketParameterType
::
String
)
;
m
.
insert
(
"u64"
,
PacketParameterType
::
U64
)
;
m
.
insert
(
"u32"
,
PacketParameterType
::
U32
)
;
m
.
insert
(
"u16"
,
PacketParameterType
::
U16
)
;
m
.
insert
(
"u8"
,
PacketParameterType
::
U8
)
;
m
.
insert
(
"i64"
,
PacketParameterType
::
I64
)
;
m
.
insert
(
"i32"
,
PacketParameterType
::
I32
)
;
m
.
insert
(
"i16"
,
PacketParameterType
::
I16
)
;
m
.
insert
(
"i8"
,
PacketParameterType
::
I8
)
;
m
.
insert
(
"position"
,
PacketParameterType
::
Position
)
;
m
.
insert
(
"bool"
,
PacketParameterType
::
Boolean
)
;
m
.
insert
(
"f32"
,
PacketParameterType
::
F32
)
;
m
.
insert
(
"f64"
,
PacketParameterType
::
F64
)
;
m
.
insert
(
"uuid"
,
PacketParameterType
::
Uuid
)
;
m
.
insert
(
"nbt"
,
PacketParameterType
::
Nbt
)
;
m
.
insert
(
"slot"
,
PacketParameterType
::
Slot
)
;
m
.
insert
(
"metadata"
,
PacketParameterType
::
EntityMetadata
)
;
// I wrote them in the wrong order, so I'm just going to reverse
// the map.
let
mut
reversed =
HashMap
::
new
(
)
;
for
(
key
,
value
)
in m
{
reversed
.
insert
(
value
,
key
)
;
}
reversed
}
;
}
#
[
proc_macro_derive
(
Packet
)
]
pub
fn
derive_packet
(
_item
:
TokenStream
)
->
TokenStream
{
let
item
:
DeriveInput
=
parse_macro_input
!
(
_item
as
DeriveInput
)
;
let
ident = item
.
ident
.
clone
(
)
;
let
fields =
match
&
item
.
data
{
Data
::
Struct
(
st
)
=>
&
st
.
fields
,
_ =>
panic
!
(
"Not a struct"
)
,
}
;
let
mut
write_code =
vec
!
[
]
;
let
mut
read_code =
vec
!
[
]
;
for
field
in
fields
{
let
field_name = field
.
ident
.
as_ref
(
)
.
unwrap
(
)
;
let
ty =
match
&
field
.
ty
{
Type
::
Path
(
path
)
=>
&
path
.
path
.
segments
,
_ =>
panic
!
(
"Not a path field"
)
,
}
;
if
ty
.
len
(
)
!=
1
{
panic
!
(
"Must not use paths"
)
;
}
let
ty_ident =
&
ty
.
first
(
)
.
unwrap
(
)
.
ident
;
let
parameter_type =
PARAMETER_MAPPINGS
.
get
(
ty_ident
.
to_string
(
)
.
as_str
(
)
)
.
unwrap_or_else
(
||
{
panic
!
(
"Couldn't find packet parameter type corresponding to {}"
,
ty_ident
)
}
)
;
let
function_ident =
Ident
::
new
(
FUNCTION_MAPPINGS
.
get
(
parameter_type
)
.
unwrap
(
)
,
Span
::
call_site
(
)
,
)
;
let
write_fn_ident =
Ident
::
new
(
&
format
!
(
"push_{}"
,
function_ident
)
,
Span
::
call_site
(
)
)
;
let
read_fn_ident =
Ident
::
new
(
&
format
!
(
"try_get_{}"
,
function_ident
)
,
Span
::
call_site
(
)
)
;
let
use_ref =
{
vec
!
[
PacketParameterType
::
Position
,
PacketParameterType
::
String
,
PacketParameterType
::
Uuid
,
PacketParameterType
::
Nbt
,
PacketParameterType
::
EntityMetadata
,
]
.
contains
(
parameter_type
)
}
;
let
write
;
if
use_ref
{
write =
quote
!
{
buf
.
#write_fn_ident
(
&
self
.
#field_name
)
;
}
;
}
else
{
write =
quote
!
{
buf
.
#write_fn_ident
(
self
.
#field_name
)
;
}
}
let
read
;
read =
quote
!
{
self
.
#field_name = buf
.
#read_fn_ident
(
)
?
;
}
;
write_code
.
push
(
write
)
;
read_code
.
push
(
read
)
;
}
let
r =
quote
!
{
impl
Packet
for
#ident
{
fn
read_from
(
&
mut
self
,
mut
buf
:
&
mut
Cursor
<
&
[
u8
]
>
)
-> anyhow
::
Result
<
(
)
>
{
#
(
#read_code
)
*
Ok
(
(
)
)
}
fn
write_to
(
&
self
,
buf
:
&
mut
BytesMut
)
{
#
(
#write_code
)
*
}
fn
ty
(
&
self
)
->
PacketType
{
PacketType
::
#ident
}
fn
ty_sized
(
)
->
PacketType
where
Self
:
Sized
{
PacketType
::
#ident
}
fn
box_clone
(
&
self
)
->
Box
<dyn
Packet
>
{
Box
::
new
(
(
*
self
)
.
clone
(
)
)
}
}
}
;
r
.
into
(
)
}
#
[
proc_macro_derive
(
FromSnakeCase
)
]
pub
fn
derive_from_snake_case
(
input
:
TokenStream
)
->
TokenStream
{
let
input
:
syn
::
DeriveInput
= syn
::
parse
(
input
)
.
unwrap
(
)
;
let
name =
&
input
.
ident
;
let
mut
match_arms =
vec
!
[
]
;
match
&
input
.
data
{
syn
::
Data
::
Enum
(
en
)
=>
{
for
variant
in
&
en
.
variants
{
let
snake_case = variant
.
ident
.
to_string
(
)
.
to_snake_case
(
)
;
let
ident =
&
variant
.
ident
;
match_arms
.
push
(
quote
!
{
#snake_case =>
Some
(
#name
::
#ident
)
}
)
;
}
}
_ =>
panic
!
(
"Can only derive `FromSnakeCase` on enums"
)
,
}
let
result =
quote
!
{
impl
FromSnakeCase
for
#name
{
fn
from_snake_case
(
val
:
&
str
)
->
Option
<
Self
>
{
match
val
{
#
(
#match_arms
,
)
*
_ =>
None
,
}
}
}
}
;
result
.
into
(
)
}
#
[
proc_macro_derive
(
ToSnakeCase
)
]
pub
fn
derive_to_snake_case
(
input
:
TokenStream
)
->
TokenStream
{
let
input
:
syn
::
DeriveInput
= syn
::
parse
(
input
)
.
unwrap
(
)
;
let
name =
&
input
.
ident
;
let
mut
match_arms =
vec
!
[
]
;
match
&
input
.
data
{
syn
::
Data
::
Enum
(
en
)
=>
{
for
variant
in
&
en
.
variants
{
let
snake_case = variant
.
ident
.
to_string
(
)
.
to_snake_case
(
)
;
let
ident =
&
variant
.
ident
;
match_arms
.
push
(
quote
!
{
#name
::
#ident => #snake_case
.
to_string
(
)
}
)
;
}
}
_ =>
panic
!
(
"Can only derive `ToSnakeCase` on enums"
)
,
}
let
result =
quote
!
{
impl
ToSnakeCase
for
#name
{
fn
to_snake_case
(
&
self
)
->
String
{
match
self
{
#
(
#match_arms
)
,
*
}
}
}
}
;
result
.
into
(
)
}
#
[
proc_macro
]
pub
fn
entity_metadata
(
input
:
TokenStream
)
->
TokenStream
{
entity_metadata
::
entity_metadata
(
input
)
}
You can’t perform that action at this time.