Skip to content

Commit 43b74af

Browse files
committed
refactor(ast_tools): move StructOrEnum into schema module (#13948)
Pure refactor. Move `StructOrEnum` into `schema` module.
1 parent f8feed1 commit 43b74af

File tree

2 files changed

+59
-61
lines changed

2 files changed

+59
-61
lines changed

tasks/ast_tools/src/derives/mod.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
Codegen, Result, Runner,
1111
output::{Output, output_path},
1212
parse::attr::{AttrLocation, AttrPart, AttrPositions, attr_positions},
13-
schema::{Def, Derives, EnumDef, FileId, Schema, StructDef, TypeDef, TypeId},
13+
schema::{Def, FileId, Schema, StructOrEnum, TypeDef},
1414
utils::format_cow,
1515
};
1616

@@ -265,63 +265,3 @@ macro_rules! define_derive {
265265
};
266266
}
267267
pub(crate) use define_derive;
268-
269-
/// Reference to a [`StructDef`] or [`EnumDef`].
270-
///
271-
/// This type is what's passed to [`Derive::derive`] method.
272-
#[derive(Clone, Copy)]
273-
pub enum StructOrEnum<'d> {
274-
Struct(&'d StructDef),
275-
Enum(&'d EnumDef),
276-
}
277-
278-
impl Def for StructOrEnum<'_> {
279-
/// Get [`TypeId`] for type.
280-
fn id(&self) -> TypeId {
281-
match self {
282-
Self::Struct(struct_def) => struct_def.id(),
283-
Self::Enum(enum_def) => enum_def.id(),
284-
}
285-
}
286-
287-
/// Get type name.
288-
fn name(&self) -> &str {
289-
match self {
290-
Self::Struct(struct_def) => struct_def.name(),
291-
Self::Enum(enum_def) => enum_def.name(),
292-
}
293-
}
294-
295-
/// Get all traits which have derives generated for this type.
296-
fn generated_derives(&self) -> Derives {
297-
match self {
298-
Self::Struct(struct_def) => struct_def.generated_derives(),
299-
Self::Enum(enum_def) => enum_def.generated_derives(),
300-
}
301-
}
302-
303-
/// Get if type has a lifetime.
304-
fn has_lifetime(&self, schema: &Schema) -> bool {
305-
match self {
306-
Self::Struct(struct_def) => struct_def.has_lifetime(schema),
307-
Self::Enum(enum_def) => enum_def.has_lifetime(schema),
308-
}
309-
}
310-
311-
/// Get type signature (including lifetimes).
312-
/// Lifetimes are anonymous (`'_`) if `anon` is true.
313-
fn ty_with_lifetime(&self, schema: &Schema, anon: bool) -> TokenStream {
314-
match self {
315-
Self::Struct(struct_def) => struct_def.ty_with_lifetime(schema, anon),
316-
Self::Enum(enum_def) => enum_def.ty_with_lifetime(schema, anon),
317-
}
318-
}
319-
320-
/// Get inner type, if type has one.
321-
///
322-
/// Structs and enums don't have a single inner type, so returns `None`.
323-
#[expect(unused_variables)]
324-
fn maybe_inner_type<'s>(&self, schema: &'s Schema) -> Option<&'s TypeDef> {
325-
None
326-
}
327-
}

tasks/ast_tools/src/schema/defs/mod.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,61 @@ pub enum Visibility {
144144
Restricted,
145145
Private,
146146
}
147+
148+
/// Reference to a [`StructDef`] or [`EnumDef`].
149+
#[derive(Clone, Copy)]
150+
pub enum StructOrEnum<'s> {
151+
Struct(&'s StructDef),
152+
Enum(&'s EnumDef),
153+
}
154+
155+
impl Def for StructOrEnum<'_> {
156+
/// Get [`TypeId`] for type.
157+
fn id(&self) -> TypeId {
158+
match self {
159+
Self::Struct(struct_def) => struct_def.id(),
160+
Self::Enum(enum_def) => enum_def.id(),
161+
}
162+
}
163+
164+
/// Get type name.
165+
fn name(&self) -> &str {
166+
match self {
167+
Self::Struct(struct_def) => struct_def.name(),
168+
Self::Enum(enum_def) => enum_def.name(),
169+
}
170+
}
171+
172+
/// Get all traits which have derives generated for this type.
173+
fn generated_derives(&self) -> Derives {
174+
match self {
175+
Self::Struct(struct_def) => struct_def.generated_derives(),
176+
Self::Enum(enum_def) => enum_def.generated_derives(),
177+
}
178+
}
179+
180+
/// Get if type has a lifetime.
181+
fn has_lifetime(&self, schema: &Schema) -> bool {
182+
match self {
183+
Self::Struct(struct_def) => struct_def.has_lifetime(schema),
184+
Self::Enum(enum_def) => enum_def.has_lifetime(schema),
185+
}
186+
}
187+
188+
/// Get type signature (including lifetimes).
189+
/// Lifetimes are anonymous (`'_`) if `anon` is true.
190+
fn ty_with_lifetime(&self, schema: &Schema, anon: bool) -> TokenStream {
191+
match self {
192+
Self::Struct(struct_def) => struct_def.ty_with_lifetime(schema, anon),
193+
Self::Enum(enum_def) => enum_def.ty_with_lifetime(schema, anon),
194+
}
195+
}
196+
197+
/// Get inner type, if type has one.
198+
///
199+
/// Structs and enums don't have a single inner type, so returns `None`.
200+
#[expect(unused_variables)]
201+
fn maybe_inner_type<'s>(&self, schema: &'s Schema) -> Option<&'s TypeDef> {
202+
None
203+
}
204+
}

0 commit comments

Comments
 (0)