Skip to content

Commit 4295698

Browse files
committed
Generate Vector*Axis via decl-macro
1 parent bf23c38 commit 4295698

File tree

1 file changed

+42
-130
lines changed

1 file changed

+42
-130
lines changed

godot-core/src/builtin/vectors/vector_axis.rs

Lines changed: 42 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -73,148 +73,60 @@ pub trait ToVector: Sized {
7373
fn to_vector(self) -> Self::Output;
7474
}
7575

76-
/// Enumerates the axes in a [`Vector2`].
77-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
78-
#[repr(i32)]
79-
pub enum Vector2Axis {
80-
/// The X axis.
81-
X,
82-
83-
/// The Y axis.
84-
Y,
85-
}
86-
87-
impl EngineEnum for Vector2Axis {
88-
fn try_from_ord(ord: i32) -> Option<Self> {
89-
match ord {
90-
0 => Some(Self::X),
91-
1 => Some(Self::Y),
92-
_ => None,
76+
macro_rules! impl_vector_axis_enum {
77+
($Vector:ident, $AxisEnum:ident, ($($axis:ident),+)) => {
78+
#[doc = concat!("Enumerates the axes in a [`", stringify!($Vector), "`].")]
79+
///
80+
#[doc = concat!("`", stringify!($Vector), "` implements `Index<", stringify!($AxisEnum), ">` and `IndexMut<", stringify!($AxisEnum), ">`")]
81+
#[doc = ", so you can use this type to access a vector component as `vec[axis]`."]
82+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
83+
#[repr(i32)]
84+
pub enum $AxisEnum {
85+
$(
86+
#[doc = concat!("The ", stringify!($axis), " axis.")]
87+
$axis,
88+
)+
9389
}
94-
}
95-
96-
fn ord(self) -> i32 {
97-
self as i32
98-
}
99-
}
100-
101-
impl GodotConvert for Vector2Axis {
102-
type Via = i32;
103-
}
104-
105-
impl ToGodot for Vector2Axis {
106-
fn to_godot(&self) -> Self::Via {
107-
self.ord()
108-
}
109-
}
110-
111-
impl FromGodot for Vector2Axis {
112-
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
113-
Self::try_from_ord(via).ok_or_else(|| FromGodotError::InvalidEnum.into_error(via))
114-
}
115-
}
116-
117-
// ----------------------------------------------------------------------------------------------------------------------------------------------
11890

119-
/// Enumerates the axes in a [`Vector3`].
120-
// TODO auto-generate this, alongside all the other builtin type's enums
121-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
122-
#[repr(i32)]
123-
pub enum Vector3Axis {
124-
/// The X axis.
125-
X,
126-
127-
/// The Y axis.
128-
Y,
129-
130-
/// The Z axis.
131-
Z,
132-
}
133-
134-
impl EngineEnum for Vector3Axis {
135-
fn try_from_ord(ord: i32) -> Option<Self> {
136-
match ord {
137-
0 => Some(Self::X),
138-
1 => Some(Self::Y),
139-
2 => Some(Self::Z),
140-
_ => None,
91+
impl EngineEnum for $AxisEnum {
92+
fn try_from_ord(ord: i32) -> Option<Self> {
93+
match ord {
94+
$(
95+
x if x == Self::$axis as i32 => Some(Self::$axis),
96+
)+
97+
_ => None,
98+
}
99+
}
100+
101+
fn ord(self) -> i32 {
102+
self as i32
103+
}
141104
}
142-
}
143-
144-
fn ord(self) -> i32 {
145-
self as i32
146-
}
147-
}
148105

149-
impl GodotConvert for Vector3Axis {
150-
type Via = i32;
151-
}
152-
153-
impl ToGodot for Vector3Axis {
154-
fn to_godot(&self) -> Self::Via {
155-
self.ord()
156-
}
157-
}
158-
159-
impl FromGodot for Vector3Axis {
160-
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
161-
Self::try_from_ord(via).ok_or_else(|| FromGodotError::InvalidEnum.into_error(via))
162-
}
163-
}
164-
165-
// ----------------------------------------------------------------------------------------------------------------------------------------------
166-
167-
/// Enumerates the axes in a [`Vector4`].
168-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
169-
#[repr(i32)]
170-
pub enum Vector4Axis {
171-
/// The X axis.
172-
X,
173-
174-
/// The Y axis.
175-
Y,
176-
177-
/// The Z axis.
178-
Z,
179-
180-
/// The W axis.
181-
W,
182-
}
183-
184-
impl EngineEnum for Vector4Axis {
185-
fn try_from_ord(ord: i32) -> Option<Self> {
186-
match ord {
187-
0 => Some(Self::X),
188-
1 => Some(Self::Y),
189-
2 => Some(Self::Z),
190-
3 => Some(Self::W),
191-
_ => None,
106+
impl GodotConvert for $AxisEnum {
107+
type Via = i32;
192108
}
193-
}
194-
195-
fn ord(self) -> i32 {
196-
self as i32
197-
}
198-
}
199-
200-
impl GodotConvert for Vector4Axis {
201-
type Via = i32;
202-
}
203109

204-
impl ToGodot for Vector4Axis {
205-
fn to_godot(&self) -> Self::Via {
206-
self.ord()
207-
}
208-
}
110+
impl ToGodot for $AxisEnum {
111+
fn to_godot(&self) -> Self::Via {
112+
self.ord()
113+
}
114+
}
209115

210-
impl FromGodot for Vector4Axis {
211-
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
212-
Self::try_from_ord(via).ok_or_else(|| FromGodotError::InvalidEnum.into_error(via))
116+
impl FromGodot for $AxisEnum {
117+
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
118+
Self::try_from_ord(via).ok_or_else(|| FromGodotError::InvalidEnum.into_error(via))
119+
}
120+
}
213121
}
214122
}
215123

216124
// ----------------------------------------------------------------------------------------------------------------------------------------------
217125

126+
impl_vector_axis_enum!(Vector2, Vector2Axis, (X, Y));
127+
impl_vector_axis_enum!(Vector3, Vector3Axis, (X, Y, Z));
128+
impl_vector_axis_enum!(Vector4, Vector4Axis, (X, Y, Z, W));
129+
218130
impl_vector_index!(Vector2, real, (x, y), Vector2Axis, (X, Y));
219131
impl_vector_index!(Vector2i, i32, (x, y), Vector2Axis, (X, Y));
220132

0 commit comments

Comments
 (0)