(variants: RustUnionVariant[])
| 245 | } |
| 246 | |
| 247 | function findRustDiscriminator(variants: RustUnionVariant[]): string | null { |
| 248 | const first = variants[0]?.schema; |
| 249 | if (!isObjectSchema(first) || !first.properties) return null; |
| 250 | |
| 251 | for (const [propName, propSchema] of Object.entries(first.properties).sort( |
| 252 | ([a], [b]) => a.localeCompare(b), |
| 253 | )) { |
| 254 | if (typeof propSchema !== "object") continue; |
| 255 | if ((propSchema as JSONSchema7).const === undefined) continue; |
| 256 | |
| 257 | const values = new Set<string>(); |
| 258 | let isValid = true; |
| 259 | for (const { schema } of variants) { |
| 260 | if (!isObjectSchema(schema) || !schema.properties) { |
| 261 | isValid = false; |
| 262 | break; |
| 263 | } |
| 264 | const candidate = schema.properties[propName]; |
| 265 | if (typeof candidate !== "object") { |
| 266 | isValid = false; |
| 267 | break; |
| 268 | } |
| 269 | const value = (candidate as JSONSchema7).const; |
| 270 | if (value === undefined) { |
| 271 | isValid = false; |
| 272 | break; |
| 273 | } |
| 274 | const key = String(value); |
| 275 | if (values.has(key)) { |
| 276 | isValid = false; |
| 277 | break; |
| 278 | } |
| 279 | values.add(key); |
| 280 | } |
| 281 | if (isValid) return propName; |
| 282 | } |
| 283 | |
| 284 | return null; |
| 285 | } |
| 286 | |
| 287 | function tryEmitRustUnion( |
| 288 | schema: JSONSchema7, |
no test coverage detected
searching dependent graphs…