1+ mod variant;
2+
13use std:: fmt:: Write ;
24
35use crate :: { casings:: CasingExt , output:: attr_output:: Attributes , schema} ;
@@ -9,6 +11,7 @@ pub struct InputObject<'schema> {
911 pub name : String ,
1012 pub fields : Vec < InputObjectField < ' schema > > ,
1113 pub schema_name : Option < String > ,
14+ pub is_oneof : bool ,
1215}
1316
1417#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -32,28 +35,54 @@ impl std::fmt::Display for InputObject<'_> {
3235 }
3336
3437 write ! ( f, "{attributes}" ) ?;
35- writeln ! (
36- f,
37- "pub struct {}{} {{" ,
38- self . name. to_pascal_case( ) ,
39- schema:: TypeSpec :: lifetime( self . fields. iter( ) . map( |f| & f. type_spec) )
40- ) ?;
41-
42- for field in self . fields . iter ( ) {
43- let mut f = indented ( f, 4 ) ;
44-
45- let name = field. schema_field . name . to_snake_case ( ) ;
46- let mut output = super :: Field :: new ( & name, & field. type_spec ) ;
47-
48- if name. to_camel_case ( ) != field. schema_field . name {
49- // If a snake -> camel casing roundtrip is not lossless
50- // we need to explicitly rename this field
51- output. add_rename ( field. schema_field . name ) ;
38+ if self . is_oneof {
39+ writeln ! (
40+ f,
41+ "pub enum {}{} {{" ,
42+ self . name. to_pascal_case( ) ,
43+ schema:: TypeSpec :: lifetime( self . fields. iter( ) . map( |f| & f. type_spec) )
44+ ) ?;
45+
46+ for field in self . fields . iter ( ) {
47+ let mut f = indented ( f, 4 ) ;
48+
49+ let name = field. schema_field . name . to_pascal_case ( ) ;
50+ let mut output = variant:: Variant :: new ( & name, & field. type_spec ) ;
51+
52+ if name. to_snake_case ( ) != field. schema_field . name {
53+ // If a snake -> pascal casing roundtrip is not lossless
54+ // we need to explicitly rename this field
55+ output. add_rename ( field. schema_field . name ) ;
56+ }
57+
58+ write ! ( f, "{}" , output) ?;
5259 }
5360
54- write ! ( f, "{}" , output) ?;
55- }
61+ writeln ! ( f, "}}" )
62+ } else {
63+ writeln ! (
64+ f,
65+ "pub struct {}{} {{" ,
66+ self . name. to_pascal_case( ) ,
67+ schema:: TypeSpec :: lifetime( self . fields. iter( ) . map( |f| & f. type_spec) )
68+ ) ?;
5669
57- writeln ! ( f, "}}" )
70+ for field in self . fields . iter ( ) {
71+ let mut f = indented ( f, 4 ) ;
72+
73+ let name = field. schema_field . name . to_snake_case ( ) ;
74+ let mut output = super :: Field :: new ( & name, & field. type_spec ) ;
75+
76+ if name. to_camel_case ( ) != field. schema_field . name {
77+ // If a snake -> camel casing roundtrip is not lossless
78+ // we need to explicitly rename this field
79+ output. add_rename ( field. schema_field . name ) ;
80+ }
81+
82+ write ! ( f, "{}" , output) ?;
83+ }
84+
85+ writeln ! ( f, "}}" )
86+ }
5887 }
5988}
0 commit comments