gesha_rust_types/
enum_variant.rs1use crate::{DataType, EnumVariantName};
2use std::fmt::{Display, Formatter};
3
4#[derive(Clone, Debug, Hash, Eq, PartialEq)]
6pub struct EnumVariant {
7 pub name: EnumVariantName,
8 pub attributes: Vec<EnumVariantAttribute>,
9 pub case: EnumCase,
10 _hide_default_constructor: bool,
11}
12
13impl EnumVariant {
14 pub fn unit(
15 name: EnumVariantName,
16 constant: EnumConstant,
17 attributes: Vec<EnumVariantAttribute>,
18 ) -> Self {
19 EnumVariant {
20 name,
21 attributes,
22 case: EnumCase::Unit(constant),
23 _hide_default_constructor: true,
24 }
25 }
26 pub fn tuple(
27 name: EnumVariantName,
28 types: Vec<DataType>,
29 attributes: Vec<EnumVariantAttribute>,
30 ) -> Self {
31 EnumVariant {
32 name,
33 attributes,
34 case: EnumCase::Tuple(types),
35 _hide_default_constructor: true,
36 }
37 }
38}
39
40#[derive(Clone, Debug, Hash, Eq, PartialEq)]
41pub enum EnumCase {
42 Unit(EnumConstant),
43 Tuple(Vec<DataType>),
44}
45
46#[derive(Clone, Debug, Hash, Eq, PartialEq)]
47pub enum EnumConstant {
48 U64(u64),
49 I64(i64),
50 Str(String),
51}
52
53impl Display for EnumConstant {
54 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55 match self {
56 EnumConstant::U64(x) => Display::fmt(x, f),
57 EnumConstant::I64(x) => Display::fmt(x, f),
58 EnumConstant::Str(x) => Display::fmt(&format!(r#""{x}""#), f),
59 }
60 }
61}
62
63#[derive(Clone, Debug, Hash, Eq, PartialEq)]
64pub struct EnumVariantAttribute(String);
65
66impl EnumVariantAttribute {
67 pub fn new<A: Into<String>>(a: A) -> Self {
68 Self(a.into())
69 }
70}
71
72impl Display for EnumVariantAttribute {
73 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74 Display::fmt(&self.0, f)
75 }
76}