go_away/types.rs
1//! Defines the type model for go-away - a set of structs that describe
2//! types and can be used to generate copies of them in other languages.
3
4/// A struct.
5///
6/// This will be serialized as a JSON object.
7#[derive(Debug)]
8pub struct Struct {
9 /// The name of the struct in Rust
10 pub name: String,
11
12 /// The structs fields.
13 pub fields: Vec<Field>,
14}
15
16/// A field within a struct
17#[derive(Debug)]
18pub struct Field {
19 /// The name of the field in rust. If the field is un-named this will
20 /// be a number.
21 pub name: String,
22 // TODO: Need both serialize & deserialize apparently
23 /// The name of the field when serialized.
24 pub serialized_name: String,
25 /// The type of the field
26 pub ty: FieldType,
27}
28
29/// A newtype struct (e.g. `struct SomeId(String)`)
30///
31/// These are usually represented as their inner type when serialized.
32#[derive(Debug)]
33pub struct NewType {
34 /// The name of the struct in rust.
35 pub name: String,
36
37 /// The type contained within the newtype.
38 pub inner: FieldType,
39}
40
41/// A type alias (e.g. `type SomeType = HashMap<String, String>;`)
42///
43/// These are usually represented as their inner type when serialized.
44#[derive(Debug)]
45pub struct Alias {
46 /// The name of the type alias in rust.
47 pub name: String,
48
49 /// The type that is being aliased.
50 pub inner: FieldType,
51}
52
53/// An enum - note that in go-away these do not contain data.
54///
55/// A Rust enum that's variants contain values will go to a `UnionType`
56#[derive(Debug)]
57pub struct Enum {
58 /// The name of the enum
59 pub name: String,
60 /// The enums variants
61 pub variants: Vec<EnumVariant>,
62}
63
64/// An enum variant - note that these are just names and are serialized
65/// as strings.
66#[derive(Debug)]
67pub struct EnumVariant {
68 /// The name of the variant in code.
69 pub name: String,
70 /// The name of the variant when serialized.
71 pub serialized_name: String,
72}
73
74/// A union type - any rust enum that's variants contain data.
75///
76/// These will be serialzied differently depending on the UnionRepresentation.
77#[derive(Debug)]
78pub struct Union {
79 /// The name of the union
80 pub name: String,
81 /// The representation of the union to use on the wire.
82 pub representation: UnionRepresentation,
83 /// The unions variants.
84 pub variants: Vec<UnionVariant>,
85}
86
87#[derive(Debug, PartialEq, Eq)]
88/// A variant of a union type
89pub struct UnionVariant {
90 /// The name of the variant if any
91 pub name: Option<String>,
92 /// The type inside the variant
93 pub ty: FieldType,
94 /// The name the variant will be serialized to
95 pub serialized_name: String,
96}
97
98/// The serialized representation of the union type
99///
100/// See https://serde.rs/enum-representations.html for details
101#[derive(Clone, Debug)]
102pub enum UnionRepresentation {
103 /// An adjacently tagged representation
104 AdjacentlyTagged {
105 /// The name of the tag field
106 tag: String,
107 /// The name of the content field
108 content: String,
109 },
110 /// An internally tagged representation
111 InternallyTagged {
112 /// The name of the tag field
113 tag: String,
114 },
115 /// An externally tagged representation
116 ExternallyTagged,
117 /// An untagged representation
118 Untagged,
119}
120
121/// The type of a field.
122#[derive(Debug, PartialEq, Eq)]
123pub enum FieldType {
124 /// A `Option<T>` field
125 Optional(Box<FieldType>),
126 /// a `Vec<T>` field
127 List(Box<FieldType>),
128 /// a `HashMap<K, V>` field
129 Map {
130 /// The type of the HashMaps keys
131 key: Box<FieldType>,
132 /// The type of the HashMaps values
133 value: Box<FieldType>,
134 },
135 /// A field with a named type
136 Named(TypeRef),
137 /// A field with a primitive type
138 Primitive(Primitive),
139}
140
141/// The primitive types
142#[derive(Debug, PartialEq, Eq)]
143pub enum Primitive {
144 /// Strings
145 String,
146 /// Floating point numbers
147 Float,
148 /// Integers
149 Int,
150 /// Booleans
151 Bool,
152 /// Time
153 Time,
154}
155
156/// A reference to a given named type
157#[derive(Debug, PartialEq, Eq)]
158pub struct TypeRef {
159 pub(crate) name: String,
160 // TODO: id: std::any::TypeId,
161}
162
163impl TypeRef {
164 /// Gets the name of the referenced type
165 pub(crate) fn name(&self) -> &str {
166 &self.name
167 }
168}