Skip to content

Commit b696044

Browse files
committed
syn: add EnumKind type and Items<Variants>::enum_kind method
1 parent f7b1fcb commit b696044

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

syn/src/data.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ pub enum FieldKind {
5454
Unnamed,
5555
}
5656

57+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
58+
pub enum EnumKind {
59+
Primitive,
60+
Associated,
61+
}
62+
5763
impl Fields {
5864
pub fn is_unit(&self) -> bool { matches!(self, Fields::Unit) }
5965

@@ -90,6 +96,16 @@ impl<'a, E: Element> IntoIterator for &'a Items<E> {
9096
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
9197
}
9298

99+
impl Items<Variant> {
100+
pub fn enum_kind(&self) -> EnumKind {
101+
if self.iter().all(|var| var.fields.is_unit()) {
102+
EnumKind::Primitive
103+
} else {
104+
EnumKind::Associated
105+
}
106+
}
107+
}
108+
93109
#[derive(Clone)]
94110
pub struct NamedField {
95111
pub name: Ident,

syn/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ mod data;
5151
pub use attr::{Attr, ExtractAttr, ParametrizedAttr, SingularAttr};
5252
pub use cls::{LiteralClass, TypeClass, ValueClass};
5353
pub use data::{
54-
DataInner, DataType, DeriveInner, Element, Field, FieldKind, Fields, Items, NamedField, Scope,
55-
Variant, Vis,
54+
DataInner, DataType, DeriveInner, Element, EnumKind, Field, FieldKind, Fields, Items,
55+
NamedField, Scope, Variant, Vis,
5656
};
5757
pub use error::Error;
5858
pub use parsers::{MetaArg, MetaArgList, MetaArgNameValue};

0 commit comments

Comments
 (0)