@@ -39,11 +39,38 @@ impl serde::Serialize for ToolsRequestToolsItemUnion {
3939 ),
4040 )
4141 })? ;
42- object
43- .insert (
44- " type" .to_string (),
45- serde_json ::Value ::String (" text" .to_string ()),
46- );
42+ match object .get (" type" ) {
43+ Some(
44+ serde_json : :Value ::String (tag ),
45+ ) if matches!(tag .as_str (), "text") => {}
46+ Some (serde_json ::Value ::String (tag )) => {
47+ return Err (
48+ serde ::ser ::Error ::custom (
49+ format ! (
50+ " discriminator `{}` value `{tag}` is not valid for variant `{}`" ,
51+ " type" , stringify ! (TextTool ),
52+ ),
53+ ),
54+ );
55+ }
56+ Some (_ ) => {
57+ return Err (
58+ serde ::ser ::Error ::custom (
59+ concat ! (
60+ " discriminator `" , " type" ,
61+ " ` did not serialize as a string" ,
62+ ),
63+ ),
64+ );
65+ }
66+ None => {
67+ object
68+ .insert (
69+ " type" .to_string (),
70+ serde_json ::Value ::String (" text" .to_string ()),
71+ );
72+ }
73+ }
4774 value .serialize (serializer )
4875 }
4976 Self :: CodeTool (payload ) => {
@@ -59,11 +86,38 @@ impl serde::Serialize for ToolsRequestToolsItemUnion {
5986 ),
6087 )
6188 })? ;
62- object
63- .insert (
64- " type" .to_string (),
65- serde_json ::Value ::String (" code" .to_string ()),
66- );
89+ match object .get (" type" ) {
90+ Some(
91+ serde_json : :Value ::String (tag ),
92+ ) if matches!(tag .as_str (), "code") => {}
93+ Some (serde_json ::Value ::String (tag )) => {
94+ return Err (
95+ serde ::ser ::Error ::custom (
96+ format ! (
97+ " discriminator `{}` value `{tag}` is not valid for variant `{}`" ,
98+ " type" , stringify ! (CodeTool ),
99+ ),
100+ ),
101+ );
102+ }
103+ Some (_ ) => {
104+ return Err (
105+ serde ::ser ::Error ::custom (
106+ concat ! (
107+ " discriminator `" , " type" ,
108+ " ` did not serialize as a string" ,
109+ ),
110+ ),
111+ );
112+ }
113+ None => {
114+ object
115+ .insert (
116+ " type" .to_string (),
117+ serde_json ::Value ::String (" code" .to_string ()),
118+ );
119+ }
120+ }
67121 value .serialize (serializer )
68122 }
69123 }
@@ -75,30 +129,99 @@ impl<'de> serde::Deserialize<'de> for ToolsRequestToolsItemUnion {
75129 D: serde::Deserializer<' de>,
76130 {
77131 let value = serde_json ::Value ::deserialize (deserializer )? ;
78- let discriminator = value
79- .get (" type" )
80- .and_then (serde_json ::Value ::as_str )
81- .ok_or_else (|| serde ::de ::Error ::custom (
82- concat ! (" missing string discriminator `" , " type" , " `" ,),
83- ))?
84- .to_string ();
85- match discriminator .as_str () {
86- "text" => {
87- serde_json::from_value ::<TextTool >(value)
88- .map(Self::TextTool)
89- .map_err(serde::de::Error::custom)
132+ let discriminator = match value .get (" type" ) {
133+ Some(serde_json : :Value ::String (discriminator )) => {
134+ Some (discriminator .as_str ())
90135 }
91- "code" => {
92- serde_json ::from_value ::<CodeTool >(value)
93- .map(Self::CodeTool)
94- .map_err(serde::de::Error::custom)
136+ Some(_ ) => {
137+ return Err (
138+ serde ::de ::Error ::custom (
139+ concat ! (" non-string discriminator `" , " type" , " `" ,),
140+ ),
141+ );
142+ }
143+ None => None ,
144+ };
145+ match discriminator {
146+ Some(discriminator ) => {
147+ match discriminator {
148+ " text" => {
149+ let primary_error = match serde_json ::from_value ::<
150+ TextTool ,
151+ > (value .clone ()) {
152+ Ok (payload ) => return Ok (Self ::TextTool (payload )),
153+ Err (error ) => error ,
154+ };
155+ let mut structural_match: Option <(Self , &'static str )> = None ;
156+ if let Ok(payload) = serde_json ::from_value ::<
157+ CodeTool ,
158+ > (value .clone ()) {
159+ if let Some((_, first_name)) = & structural_match {
160+ return Err(
161+ serde : :de ::Error ::custom (
162+ format !(
163+ " discriminator `{}` value `{}` did not fit its mapped branch and structurally matched both `{}` and `{}`" ,
164+ " type" , " text" , first_name , stringify !(CodeTool ),
165+ ),
166+ ),
167+ );
168+ }
169+ structural_match = Some ((
170+ Self ::CodeTool (payload ),
171+ stringify ! (CodeTool ),
172+ ));
173+ }
174+ match structural_match {
175+ Some ((payload , _ )) => Ok (payload ),
176+ None => Err (serde ::de ::Error ::custom (primary_error )),
177+ }
178+ }
179+ " code" => {
180+ let primary_error = match serde_json ::from_value ::<
181+ CodeTool ,
182+ > (value .clone ()) {
183+ Ok (payload ) => return Ok (Self ::CodeTool (payload )),
184+ Err (error ) => error ,
185+ };
186+ let mut structural_match: Option <(Self , &'static str )> = None ;
187+ if let Ok(payload) = serde_json ::from_value ::<
188+ TextTool ,
189+ > (value .clone ()) {
190+ if let Some((_, first_name)) = & structural_match {
191+ return Err(
192+ serde : :de ::Error ::custom (
193+ format !(
194+ " discriminator `{}` value `{}` did not fit its mapped branch and structurally matched both `{}` and `{}`" ,
195+ " type" , " code" , first_name , stringify !(TextTool ),
196+ ),
197+ ),
198+ );
199+ }
200+ structural_match = Some ((
201+ Self ::TextTool (payload ),
202+ stringify ! (TextTool ),
203+ ));
204+ }
205+ match structural_match {
206+ Some ((payload , _ )) => Ok (payload ),
207+ None => Err (serde ::de ::Error ::custom (primary_error )),
208+ }
209+ }
210+ other => {
211+ Err (
212+ serde ::de ::Error ::custom (
213+ format ! (
214+ " unknown discriminator value `{other}` for `{}`" , " type" ,
215+ ),
216+ ),
217+ )
218+ }
219+ }
95220 }
96- other => {
221+ None => {
97222 Err(
98223 serde : :de ::Error ::custom (
99- format ! (
100- " unknown discriminator value `{other}` for `{}`" , " type" ,
101- ),
224+ concat !(" missing string discriminator `" , " type" , " `" ,),
102225 ),
103226 )
104227 }
0 commit comments