@@ -14,6 +14,7 @@ const PG_RANGE_H: &'static str = include_str!("pg_range.h");
14
14
struct Type {
15
15
name : & ' static str ,
16
16
variant : String ,
17
+ ident : String ,
17
18
kind : & ' static str ,
18
19
element : u32 ,
19
20
doc : String ,
@@ -27,8 +28,8 @@ pub fn build(path: &Path) {
27
28
28
29
make_header ( & mut file) ;
29
30
make_enum ( & mut file, & types) ;
30
- make_display_impl ( & mut file) ;
31
31
make_impl ( & mut file, & types) ;
32
+ make_consts ( & mut file, & types) ;
32
33
}
33
34
34
35
fn parse_ranges ( ) -> BTreeMap < u32 , u32 > {
@@ -69,14 +70,10 @@ fn parse_types(ranges: &BTreeMap<u32, u32>) -> BTreeMap<u32, Type> {
69
70
70
71
let name = split[ 5 ] ;
71
72
72
- let variant = match name {
73
- "anyarray" => "AnyArray" . to_owned ( ) ,
74
- name => {
75
- let variant = range_vector_re. replace ( name, "_$1" ) ;
76
- let variant = array_re. replace ( & variant, "$1_array" ) ;
77
- snake_to_camel ( & variant)
78
- }
79
- } ;
73
+ let ident = range_vector_re. replace ( name, "_$1" ) ;
74
+ let ident = array_re. replace ( & ident, "$1_array" ) ;
75
+ let variant = snake_to_camel ( & ident) ;
76
+ let ident = ident. to_ascii_uppercase ( ) ;
80
77
81
78
let kind = split[ 11 ] ;
82
79
@@ -106,11 +103,12 @@ fn parse_types(ranges: &BTreeMap<u32, u32>) -> BTreeMap<u32, Type> {
106
103
let doc = String :: from_utf8 ( doc) . unwrap ( ) ;
107
104
108
105
let type_ = Type {
109
- name : name,
110
- variant : variant,
111
- kind : kind,
112
- element : element,
113
- doc : doc,
106
+ name,
107
+ variant,
108
+ ident,
109
+ kind,
110
+ element,
111
+ doc,
114
112
} ;
115
113
116
114
types. insert ( oid, type_) ;
@@ -123,74 +121,60 @@ fn make_header(w: &mut BufWriter<File>) {
123
121
write ! (
124
122
w,
125
123
"// Autogenerated file - DO NOT EDIT
126
- use std::fmt ;
124
+ use std::sync::Arc ;
127
125
128
- use types::{{Oid, Kind, Other }};
126
+ use types::{{Type, Oid, Kind }};
129
127
128
+ #[derive(PartialEq, Eq, Debug)]
129
+ pub struct Other {{
130
+ pub name: String,
131
+ pub oid: Oid,
132
+ pub kind: Kind,
133
+ pub schema: String,
134
+ }}
130
135
"
131
136
) . unwrap ( ) ;
132
137
}
133
138
134
139
fn make_enum ( w : & mut BufWriter < File > , types : & BTreeMap < u32 , Type > ) {
135
140
write ! (
136
141
w,
137
- "/// A Postgres type.
142
+ "
138
143
#[derive(PartialEq, Eq, Clone, Debug)]
139
- pub enum Type {{
140
- "
144
+ pub enum Inner {{"
141
145
) . unwrap ( ) ;
142
146
143
147
for type_ in types. values ( ) {
144
148
write ! (
145
149
w,
146
- " /// {}
147
- {},
148
- " ,
149
- type_. doc,
150
+ "
151
+ {}," ,
150
152
type_. variant
151
153
) . unwrap ( ) ;
152
154
}
153
155
154
156
write ! (
155
157
w,
156
- r" /// An unknown type.
157
- Other(Other),
158
+ r"
159
+ Other(Arc< Other> ),
158
160
}}
159
161
160
162
"
161
163
) . unwrap ( ) ;
162
164
}
163
165
164
- fn make_display_impl ( w : & mut BufWriter < File > ) {
165
- write ! ( w,
166
- r#"impl fmt::Display for Type {{
167
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {{
168
- match self.schema() {{
169
- "public" | "pg_catalog" => {{}}
170
- schema => write!(fmt, "{{}}.", schema)?,
171
- }}
172
- fmt.write_str(self.name())
173
- }}
174
- }}
175
-
176
- "# ,
177
- ) . unwrap ( ) ;
178
- }
179
-
180
166
fn make_impl ( w : & mut BufWriter < File > , types : & BTreeMap < u32 , Type > ) {
181
167
write ! ( w,
182
- "impl Type {{
183
- /// Returns the `Type` corresponding to the provided `Oid` if it
184
- /// corresponds to a built-in type.
185
- pub fn from_oid(oid: Oid) -> Option<Type> {{
168
+ "impl Inner {{
169
+ pub fn from_oid(oid: Oid) -> Option<Inner> {{
186
170
match oid {{
187
171
" ,
188
172
) . unwrap ( ) ;
189
173
190
174
for ( oid, type_) in types {
191
175
write ! (
192
176
w,
193
- " {} => Some(Type ::{}),
177
+ " {} => Some(Inner ::{}),
194
178
" ,
195
179
oid,
196
180
type_. variant
@@ -202,7 +186,6 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
202
186
}}
203
187
}}
204
188
205
- /// Returns the OID of the `Type`.
206
189
pub fn oid(&self) -> Oid {{
207
190
match *self {{
208
191
" ,
@@ -212,19 +195,18 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
212
195
for ( oid, type_) in types {
213
196
write ! (
214
197
w,
215
- " Type ::{} => {},
198
+ " Inner ::{} => {},
216
199
" ,
217
200
type_. variant,
218
201
oid
219
202
) . unwrap ( ) ;
220
203
}
221
204
222
205
write ! ( w,
223
- " Type ::Other(ref u) => u.oid() ,
206
+ " Inner ::Other(ref u) => u.oid,
224
207
}}
225
208
}}
226
209
227
- /// Returns the kind of this type.
228
210
pub fn kind(&self) -> &Kind {{
229
211
match *self {{
230
212
" ,
@@ -233,14 +215,14 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
233
215
for type_ in types. values ( ) {
234
216
let kind = match type_. kind {
235
217
"P" => "Pseudo" . to_owned ( ) ,
236
- "A" => format ! ( "Array(Type::{})" , types[ & type_. element] . variant) ,
237
- "R" => format ! ( "Range(Type::{})" , types[ & type_. element] . variant) ,
218
+ "A" => format ! ( "Array(Type(Inner ::{}) )" , types[ & type_. element] . variant) ,
219
+ "R" => format ! ( "Range(Type(Inner ::{}) )" , types[ & type_. element] . variant) ,
238
220
_ => "Simple" . to_owned ( ) ,
239
221
} ;
240
222
241
223
write ! (
242
224
w,
243
- " Type ::{} => {{
225
+ " Inner ::{} => {{
244
226
const V: &'static Kind = &Kind::{};
245
227
V
246
228
}}
@@ -251,19 +233,10 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
251
233
}
252
234
253
235
write ! ( w,
254
- r#" Type ::Other(ref u) => u.kind() ,
236
+ r#" Inner ::Other(ref u) => & u.kind,
255
237
}}
256
238
}}
257
239
258
- /// Returns the schema of this type.
259
- pub fn schema(&self) -> &str {{
260
- match *self {{
261
- Type::Other(ref u) => u.schema(),
262
- _ => "pg_catalog",
263
- }}
264
- }}
265
-
266
- /// Returns the name of this type.
267
240
pub fn name(&self) -> &str {{
268
241
match *self {{
269
242
"# ,
@@ -272,7 +245,7 @@ r#" Type::Other(ref u) => u.kind(),
272
245
for type_ in types. values ( ) {
273
246
write ! (
274
247
w,
275
- r#" Type ::{} => "{}",
248
+ r#" Inner ::{} => "{}",
276
249
"# ,
277
250
type_. variant,
278
251
type_. name
@@ -281,10 +254,34 @@ r#" Type::Other(ref u) => u.kind(),
281
254
282
255
write ! (
283
256
w,
284
- " Type ::Other(ref u) => u.name() ,
257
+ " Inner ::Other(ref u) => & u.name,
285
258
}}
286
259
}}
287
260
}}
288
261
"
289
262
) . unwrap ( ) ;
290
263
}
264
+
265
+ fn make_consts ( w : & mut BufWriter < File > , types : & BTreeMap < u32 , Type > ) {
266
+ write ! ( w,
267
+ "pub mod consts {{
268
+ use types::Type;
269
+ use types::type_gen::Inner;
270
+ " ,
271
+ ) . unwrap ( ) ;
272
+
273
+ for type_ in types. values ( ) {
274
+ write ! ( w,
275
+ "
276
+ /// {docs}
277
+ pub const {ident}: Type = Type(Inner::{variant});
278
+ " ,
279
+ docs = type_. doc,
280
+ ident = type_. ident,
281
+ variant = type_. variant) . unwrap ( ) ;
282
+ }
283
+
284
+ write ! ( w,
285
+ "}}"
286
+ ) . unwrap ( ) ;
287
+ }
0 commit comments