shapely_core/shape/enum_shape.rs
1use super::Field;
2
3/// Describes a variant of an enum
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct Variant {
6 /// Name of the variant
7 pub name: &'static str,
8
9 /// Discriminant value (if available)
10 pub discriminant: Option<i64>,
11
12 /// Kind of variant (unit, tuple, or struct)
13 pub kind: VariantKind,
14}
15
16/// Represents the different kinds of variants that can exist in a Rust enum
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum VariantKind {
19 /// Unit variant (e.g., `None` in Option)
20 Unit,
21
22 /// Tuple variant with unnamed fields (e.g., `Some(T)` in Option)
23 Tuple {
24 /// List of fields contained in the tuple variant
25 fields: &'static [Field],
26 },
27
28 /// Struct variant with named fields (e.g., `Struct { field: T }`)
29 Struct {
30 /// List of fields contained in the struct variant
31 fields: &'static [Field],
32 },
33}
34
35/// All possible representations for Rust enums
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub enum EnumRepr {
38 /// Default representation (compiler-dependent)
39 Default,
40 /// u8 representation (#[repr(u8)])
41 U8,
42 /// u16 representation (#[repr(u16)])
43 U16,
44 /// u32 representation (#[repr(u32)])
45 U32,
46 /// u64 representation (#[repr(u64)])
47 U64,
48 /// usize representation (#[repr(usize)])
49 USize,
50 /// i8 representation (#[repr(i8)])
51 I8,
52 /// i16 representation (#[repr(i16)])
53 I16,
54 /// i32 representation (#[repr(i32)])
55 I32,
56 /// i64 representation (#[repr(i64)])
57 I64,
58 /// isize representation (#[repr(isize)])
59 ISize,
60}
61
62impl Default for EnumRepr {
63 fn default() -> Self {
64 Self::Default
65 }
66}
67
68/// All possible errors when getting a variant by index or by name
69#[derive(Debug, Copy, Clone, PartialEq, Eq)]
70pub enum VariantError {
71 /// `variant_by_index` was called with an index that is out of bounds.
72 IndexOutOfBounds,
73
74 /// `variant_by_name` or `variant_by_index` was called on a non-enum type.
75 NotAnEnum,
76
77 /// `variant_by_name` was called with a name that doesn't match any variant.
78 NoSuchVariant,
79}
80
81impl std::error::Error for VariantError {}
82
83impl std::fmt::Display for VariantError {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 match self {
86 VariantError::IndexOutOfBounds => write!(f, "Variant index out of bounds"),
87 VariantError::NotAnEnum => write!(f, "Not an enum"),
88 VariantError::NoSuchVariant => write!(f, "No such variant"),
89 }
90 }
91}