@@ -1266,6 +1266,24 @@ private static void generateEnum(
1266
1266
indent (writer , 0 , "}\n " );
1267
1267
1268
1268
// From impl
1269
+ generateFromImplForEnum (enumRustName , primitiveType , messageBody , writer );
1270
+
1271
+ // Into impl
1272
+ generateIntoImplForEnum (enumRustName , primitiveType , messageBody , writer );
1273
+
1274
+ // FromStr impl
1275
+ generateFromStrImplForEnum (enumRustName , primitiveType , messageBody , writer );
1276
+
1277
+ // Display impl
1278
+ generateDisplayImplForEnum (enumRustName , primitiveType , messageBody , writer );
1279
+ }
1280
+
1281
+ private static void generateFromImplForEnum (
1282
+ final String enumRustName ,
1283
+ final String primitiveType ,
1284
+ final List <Token > messageBody ,
1285
+ final Appendable writer ) throws IOException
1286
+ {
1269
1287
indent (writer , 0 , "impl From<%s> for %s {\n " , primitiveType , enumRustName );
1270
1288
indent (writer , 1 , "#[inline]\n " );
1271
1289
indent (writer , 1 , "fn from(v: %s) -> Self {\n " , primitiveType );
@@ -1285,6 +1303,76 @@ private static void generateEnum(
1285
1303
indent (writer , 0 , "}\n " );
1286
1304
}
1287
1305
1306
+ private static void generateIntoImplForEnum (
1307
+ final String enumRustName ,
1308
+ final String primitiveType ,
1309
+ final List <Token > messageBody ,
1310
+ final Appendable writer ) throws IOException
1311
+ {
1312
+ indent (writer , 0 , "impl Into<%s> for %s {\n " , primitiveType , enumRustName );
1313
+ indent (writer , 1 , "#[inline]\n " );
1314
+ indent (writer , 1 , "fn into(self) -> %s {\n " , primitiveType );
1315
+ indent (writer , 2 , "match self {\n " );
1316
+ for (final Token token : messageBody )
1317
+ {
1318
+ final Encoding encoding = token .encoding ();
1319
+ final String literal = generateRustLiteral (encoding .primitiveType (), encoding .constValue ().toString ());
1320
+ indent (writer , 3 , "Self::%s => %s, \n " , token .name (), literal );
1321
+ }
1322
+ {
1323
+ final Encoding encoding = messageBody .get (0 ).encoding ();
1324
+ final CharSequence nullVal = generateRustLiteral (encoding .primitiveType (),
1325
+ encoding .applicableNullValue ().toString ());
1326
+ indent (writer , 3 , "Self::NullVal => %s,\n " , nullVal );
1327
+ }
1328
+ indent (writer , 2 , "}\n " );
1329
+ indent (writer , 1 , "}\n " );
1330
+ indent (writer , 0 , "}\n " );
1331
+ }
1332
+
1333
+ private static void generateFromStrImplForEnum (
1334
+ final String enumRustName ,
1335
+ final String primitiveType ,
1336
+ final List <Token > messageBody ,
1337
+ final Appendable writer ) throws IOException
1338
+ {
1339
+ indent (writer , 0 , "impl core::str::FromStr for %s {\n " , enumRustName );
1340
+ indent (writer , 1 , "type Err = ();\n \n " );
1341
+ indent (writer , 1 , "#[inline]\n " );
1342
+ indent (writer , 1 , "fn from_str(v: &str) -> core::result::Result<Self, Self::Err> {\n " );
1343
+ indent (writer , 2 , "match v {\n " );
1344
+ for (final Token token : messageBody )
1345
+ {
1346
+ indent (writer , 3 , "\" %1$s\" => core::result::Result::Ok(Self::%1$s), \n " , token .name ());
1347
+ }
1348
+ // default => NullVal
1349
+ indent (writer , 3 , "_ => core::result::Result::Ok(Self::NullVal),\n " );
1350
+ indent (writer , 2 , "}\n " );
1351
+ indent (writer , 1 , "}\n " );
1352
+ indent (writer , 0 , "}\n " );
1353
+ }
1354
+
1355
+ private static void generateDisplayImplForEnum (
1356
+ final String enumRustName ,
1357
+ final String primitiveType ,
1358
+ final List <Token > messageBody ,
1359
+ final Appendable writer ) throws IOException
1360
+ {
1361
+ indent (writer , 0 , "impl core::fmt::Display for %s {\n " , enumRustName );
1362
+ indent (writer , 1 , "#[inline]\n " );
1363
+ indent (writer , 1 , "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n " );
1364
+ indent (writer , 2 , "match self {\n " );
1365
+ for (final Token token : messageBody )
1366
+ {
1367
+ indent (writer , 3 , "Self::%1$s => write!(f, \" %1$s\" ), \n " , token .name ());
1368
+ }
1369
+ // default => Err
1370
+ indent (writer , 3 , "Self::NullVal => write!(f, \" NullVal\" ),\n " );
1371
+ indent (writer , 2 , "}\n " );
1372
+ indent (writer , 1 , "}\n " );
1373
+ indent (writer , 0 , "}\n " );
1374
+ }
1375
+
1288
1376
private static void generateComposites (
1289
1377
final String schemaVersionType ,
1290
1378
final Ir ir ,
0 commit comments