1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
use std::{
borrow::Cow,
fmt, iter,
ops::{Range, RangeInclusive},
sync::Arc,
};
use prost::{
bytes::{Buf, BufMut},
encoding::{self, WireType},
DecodeError, EncodeError, Message,
};
use prost_types::{
DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto,
FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, OneofDescriptorProto,
ServiceDescriptorProto,
};
use crate::{
descriptor::{
error::DescriptorErrorKind,
find_enum_proto, find_message_proto, tag, to_index,
types::{self, Options},
Definition, DefinitionKind, DescriptorIndex, EnumDescriptorInner, EnumValueDescriptorInner,
ExtensionDescriptorInner, FieldDescriptorInner, FileDescriptorInner, KindIndex,
MessageDescriptorInner, MethodDescriptorInner, OneofDescriptorInner,
ServiceDescriptorInner, MAP_ENTRY_KEY_NUMBER, MAP_ENTRY_VALUE_NUMBER,
},
Cardinality, DescriptorError, DescriptorPool, DynamicMessage, EnumDescriptor,
EnumValueDescriptor, ExtensionDescriptor, FieldDescriptor, FileDescriptor, Kind,
MessageDescriptor, MethodDescriptor, OneofDescriptor, ServiceDescriptor, Syntax, Value,
};
impl fmt::Debug for Syntax {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Syntax::Proto2 => write!(f, "proto2"),
Syntax::Proto3 => write!(f, "proto3"),
}
}
}
impl Kind {
fn new(pool: &DescriptorPool, kind: KindIndex) -> Self {
match kind {
KindIndex::Double => Kind::Double,
KindIndex::Float => Kind::Float,
KindIndex::Int64 => Kind::Int64,
KindIndex::Uint64 => Kind::Uint64,
KindIndex::Int32 => Kind::Int32,
KindIndex::Fixed64 => Kind::Fixed64,
KindIndex::Fixed32 => Kind::Fixed32,
KindIndex::Bool => Kind::Bool,
KindIndex::String => Kind::String,
KindIndex::Bytes => Kind::Bytes,
KindIndex::Uint32 => Kind::Uint32,
KindIndex::Sfixed32 => Kind::Sfixed32,
KindIndex::Sfixed64 => Kind::Sfixed64,
KindIndex::Sint32 => Kind::Sint32,
KindIndex::Sint64 => Kind::Sint64,
KindIndex::Message(index) | KindIndex::Group(index) => {
Kind::Message(MessageDescriptor {
pool: pool.clone(),
index,
})
}
KindIndex::Enum(index) => Kind::Enum(EnumDescriptor {
pool: pool.clone(),
index,
}),
}
}
/// Gets a reference to the [`MessageDescriptor`] if this is a message type,
/// or `None` otherwise.
pub fn as_message(&self) -> Option<&MessageDescriptor> {
match self {
Kind::Message(desc) => Some(desc),
_ => None,
}
}
/// Gets a reference to the [`EnumDescriptor`] if this is an enum type,
/// or `None` otherwise.
pub fn as_enum(&self) -> Option<&EnumDescriptor> {
match self {
Kind::Enum(desc) => Some(desc),
_ => None,
}
}
/// Returns the [`WireType`] used to encode this type.
///
/// Note: The [`Kind::Message`] returns [` WireType::LengthDelimited`],
/// as [groups are deprecated](https://protobuf.dev/programming-guides/encoding/#groups).
pub fn wire_type(&self) -> WireType {
match self {
Kind::Double | Kind::Fixed64 | Kind::Sfixed64 => WireType::SixtyFourBit,
Kind::Float | Kind::Fixed32 | Kind::Sfixed32 => WireType::ThirtyTwoBit,
Kind::Enum(_)
| Kind::Int32
| Kind::Int64
| Kind::Uint32
| Kind::Uint64
| Kind::Sint32
| Kind::Sint64
| Kind::Bool => WireType::Varint,
Kind::String | Kind::Bytes | Kind::Message(_) => WireType::LengthDelimited,
}
}
}
impl fmt::Debug for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Double => write!(f, "double"),
Self::Float => write!(f, "float"),
Self::Int32 => write!(f, "int32"),
Self::Int64 => write!(f, "int64"),
Self::Uint32 => write!(f, "uint32"),
Self::Uint64 => write!(f, "uint64"),
Self::Sint32 => write!(f, "sint32"),
Self::Sint64 => write!(f, "sint64"),
Self::Fixed32 => write!(f, "fixed32"),
Self::Fixed64 => write!(f, "fixed64"),
Self::Sfixed32 => write!(f, "sfixed32"),
Self::Sfixed64 => write!(f, "sfixed64"),
Self::Bool => write!(f, "bool"),
Self::String => write!(f, "string"),
Self::Bytes => write!(f, "bytes"),
Self::Message(m) => write!(f, "{}", m.full_name()),
Self::Enum(e) => write!(f, "{}", e.full_name()),
}
}
}
impl DescriptorPool {
/// Creates a new, empty [`DescriptorPool`].
///
/// For the common case of creating a `DescriptorPool` from a single [`FileDescriptorSet`], see
/// [`DescriptorPool::from_file_descriptor_set`] or [`DescriptorPool::decode`].
pub fn new() -> Self {
DescriptorPool::default()
}
/// Creates a [`DescriptorPool`] from a [`FileDescriptorSet`].
///
/// A file descriptor set may be generated by running the protobuf compiler with the
/// `--descriptor_set_out` flag. If you are using [`prost-build`](https://crates.io/crates/prost-build),
/// then [`Config::file_descriptor_set_path`](https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method..file_descriptor_set_path)
/// is a convenient way to generate it as part of your build.
pub fn from_file_descriptor_set(
file_descriptor_set: FileDescriptorSet,
) -> Result<Self, DescriptorError> {
let mut pool = DescriptorPool::new();
pool.add_file_descriptor_set(file_descriptor_set)?;
Ok(pool)
}
/// Decodes and adds a set of file descriptors to the pool.
///
/// A file descriptor set may be generated by running the protobuf compiler with the
/// `--descriptor_set_out` flag. If you are using [`prost-build`](https://crates.io/crates/prost-build),
/// then [`Config::file_descriptor_set_path`](https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method..file_descriptor_set_path)
/// is a convenient way to generate it as part of your build.
///
/// Unlike when using [`DescriptorPool::from_file_descriptor_set`], any extension options
/// defined in the file descriptors are preserved.
///
/// # Errors
///
/// Returns an error if the given bytes are not a valid protobuf-encoded file descriptor set, or if the descriptor set itself
/// is invalid. When using a file descriptor set generated by the protobuf compiler, this method will always succeed.
pub fn decode<B>(bytes: B) -> Result<Self, DescriptorError>
where
B: Buf,
{
let file_descriptor_set = types::FileDescriptorSet::decode(bytes).map_err(|err| {
DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
})?;
let mut pool = DescriptorPool::new();
pool.build_files(file_descriptor_set.file.into_iter())?;
Ok(pool)
}
/// Adds a new [`FileDescriptorSet`] to this [`DescriptorPool`].
///
/// A file descriptor set may be generated by running the protobuf compiler with the
/// `--descriptor_set_out` flag. If you are using [`prost-build`](https://crates.io/crates/prost-build),
/// then [`Config::file_descriptor_set_path`](https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method..file_descriptor_set_path)
/// is a convenient way to generate it as part of your build.
///
/// Any duplicates of files already in the pool will be skipped. Note this may cause issues when trying to add two different versions of a file with the same name.
///
/// # Errors
///
/// Returns an error if the descriptor set is invalid, for example if it references types not yet added
/// to the pool. When using a file descriptor set generated by the protobuf compiler, this method will
/// always succeed.
pub fn add_file_descriptor_set(
&mut self,
file_descriptor_set: FileDescriptorSet,
) -> Result<(), DescriptorError> {
self.add_file_descriptor_protos(file_descriptor_set.file)
}
/// Adds a collection of file descriptors to this pool.
///
/// The file descriptors may be provided in any order, however all types referenced must be defined
/// either in one of the files provided, or in a file previously added to the pool.
///
/// Any duplicates of files already in the pool will be skipped. Note this may cause issues when trying to add two different versions of a file with the same name.
///
/// # Errors
///
/// Returns an error if any of the given file descriptor is invalid, for example if they reference
/// types not yet added to the pool.
pub fn add_file_descriptor_protos<I>(&mut self, files: I) -> Result<(), DescriptorError>
where
I: IntoIterator<Item = FileDescriptorProto>,
{
self.build_files(
files
.into_iter()
.map(types::FileDescriptorProto::from_prost),
)
}
/// Add a single file descriptor to the pool.
///
/// All types referenced by the file must be defined either in the file itself, or in a file
/// previously added to the pool.
///
/// If the file is a duplicate of a file already in the pool, it will be skipped. Note this may cause issues when trying to add two different versions of a file with the same name.
///
/// # Errors
///
/// Returns an error if the given file descriptor is invalid, for example if it references types not yet added
/// to the pool.
pub fn add_file_descriptor_proto(
&mut self,
file: FileDescriptorProto,
) -> Result<(), DescriptorError> {
self.add_file_descriptor_protos(iter::once(file))
}
/// Decode and add a single file descriptor to the pool.
///
/// All types referenced by the file must be defined either in the file itself, or in a file
/// previously added to the pool.
///
/// Unlike when using [`add_file_descriptor_proto()`][DescriptorPool::add_file_descriptor_proto], any extension options
/// defined in the file descriptor are preserved.
///
/// If the file is a duplicate of a file already in the pool, it will be skipped. Note this may cause issues when trying to add two different versions of a file with the same name.
///
/// # Errors
///
/// Returns an error if the given bytes are not a valid protobuf-encoded file descriptor, or if the file descriptor itself
/// is invalid, for example if it references types not yet added to the pool.
pub fn decode_file_descriptor_proto<B>(&mut self, bytes: B) -> Result<(), DescriptorError>
where
B: Buf,
{
let file = types::FileDescriptorProto::decode(bytes).map_err(|err| {
DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
})?;
self.build_files(iter::once(file))
}
/// Decode and add a set of file descriptors to the pool.
///
/// A file descriptor set may be generated by running the protobuf compiler with the
/// `--descriptor_set_out` flag. If you are using [`prost-build`](https://crates.io/crates/prost-build),
/// then [`Config::file_descriptor_set_path`](https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method..file_descriptor_set_path)
/// is a convenient way to generate it as part of your build.
///
/// Unlike when using [`add_file_descriptor_set()`][DescriptorPool::add_file_descriptor_set], any extension options
/// defined in the file descriptors are preserved.
///
/// Any duplicates of files already in the pool will be skipped. Note this may cause issues when trying to add two different versions of a file with the same name.
///
/// # Errors
///
/// Returns an error if the given bytes are not a valid protobuf-encoded file descriptor set, or if the descriptor set itself
/// is invalid. When using a file descriptor set generated by the protobuf compiler, this method will always succeed.
pub fn decode_file_descriptor_set<B>(&mut self, bytes: B) -> Result<(), DescriptorError>
where
B: Buf,
{
let file = types::FileDescriptorSet::decode(bytes).map_err(|err| {
DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
})?;
self.build_files(file.file)
}
/// Gets an iterator over the file descriptors added to this pool.
pub fn files(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
indices(&self.inner.files).map(|index| FileDescriptor {
pool: self.clone(),
index,
})
}
/// Gets a file descriptor by its name, or `None` if no such file has been added.
pub fn get_file_by_name(&self, name: &str) -> Option<FileDescriptor> {
if let Some(&index) = self.inner.file_names.get(name) {
Some(FileDescriptor {
pool: self.clone(),
index,
})
} else {
None
}
}
/// Gets a iterator over the raw [`FileDescriptorProto`] instances wrapped by this [`DescriptorPool`].
pub fn file_descriptor_protos(
&self,
) -> impl ExactSizeIterator<Item = &FileDescriptorProto> + '_ {
indices(&self.inner.files).map(|index| &self.inner.files[index as usize].prost)
}
/// Encodes the files contained within this [`DescriptorPool`] to their byte representation.
///
/// The encoded message is equivalent to a [`FileDescriptorSet`], however also includes
/// any extension options that were defined.
pub fn encode<B>(&self, buf: B) -> Result<(), EncodeError>
where
B: BufMut,
{
use prost::encoding::{encoded_len_varint, DecodeContext};
struct FileDescriptorSet<'a> {
files: &'a [FileDescriptorInner],
}
impl<'a> fmt::Debug for FileDescriptorSet<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FileDescriptorSet").finish_non_exhaustive()
}
}
impl<'a> Message for FileDescriptorSet<'a> {
fn encode_raw(&self, buf: &mut impl BufMut)
where
Self: Sized,
{
for file in self.files {
encoding::message::encode(
tag::file_descriptor_set::FILE as u32,
&file.raw,
buf,
);
}
}
fn encoded_len(&self) -> usize {
encoding::key_len(tag::file_descriptor_set::FILE as u32) * self.files.len()
+ self
.files
.iter()
.map(|f| &f.raw)
.map(Message::encoded_len)
.map(|len| len + encoded_len_varint(len as u64))
.sum::<usize>()
}
fn merge_field(
&mut self,
_: u32,
_: WireType,
_: &mut impl Buf,
_: DecodeContext,
) -> Result<(), DecodeError>
where
Self: Sized,
{
unimplemented!()
}
fn clear(&mut self) {
unimplemented!()
}
}
let mut buf = buf;
FileDescriptorSet {
files: &self.inner.files,
}
.encode(&mut buf)
}
/// Encodes the files contained within this [`DescriptorPool`] to a newly allocated buffer.
///
/// The encoded message is equivalent to a [`FileDescriptorSet`], however also includes
/// any extension options that were defined.
pub fn encode_to_vec(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.encode(&mut buf).expect("vec should have capacity");
buf
}
/// Gets an iterator over the services defined in these protobuf files.
pub fn services(&self) -> impl ExactSizeIterator<Item = ServiceDescriptor> + '_ {
indices(&self.inner.services).map(|index| ServiceDescriptor {
pool: self.clone(),
index,
})
}
/// Gets an iterator over all message types defined in these protobuf files.
///
/// The iterator includes nested messages defined in another message.
pub fn all_messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
indices(&self.inner.messages).map(|index| MessageDescriptor {
pool: self.clone(),
index,
})
}
/// Gets an iterator over all enum types defined in these protobuf files.
///
/// The iterator includes nested enums defined in another message.
pub fn all_enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
indices(&self.inner.enums).map(|index| EnumDescriptor {
pool: self.clone(),
index,
})
}
/// Gets an iterator over all extension fields defined in these protobuf files.
///
/// The iterator includes nested extension fields defined in another message.
pub fn all_extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
indices(&self.inner.extensions).map(|index| ExtensionDescriptor {
pool: self.clone(),
index,
})
}
/// Gets a [`MessageDescriptor`] by its fully qualified name, for example `my.package.MessageName`.
pub fn get_message_by_name(&self, name: &str) -> Option<MessageDescriptor> {
match self.inner.get_by_name(name) {
Some(&Definition {
kind: DefinitionKind::Message(index),
..
}) => Some(MessageDescriptor {
pool: self.clone(),
index,
}),
_ => None,
}
}
/// Gets an [`EnumDescriptor`] by its fully qualified name, for example `my.package.EnumName`.
pub fn get_enum_by_name(&self, name: &str) -> Option<EnumDescriptor> {
match self.inner.get_by_name(name) {
Some(&Definition {
kind: DefinitionKind::Enum(index),
..
}) => Some(EnumDescriptor {
pool: self.clone(),
index,
}),
_ => None,
}
}
/// Gets an [`ExtensionDescriptor`] by its fully qualified name, for example `my.package.my_extension`.
pub fn get_extension_by_name(&self, name: &str) -> Option<ExtensionDescriptor> {
match self.inner.get_by_name(name) {
Some(&Definition {
kind: DefinitionKind::Extension(index),
..
}) => Some(ExtensionDescriptor {
pool: self.clone(),
index,
}),
_ => None,
}
}
/// Gets an [`ServiceDescriptor`] by its fully qualified name, for example `my.package.MyService`.
pub fn get_service_by_name(&self, name: &str) -> Option<ServiceDescriptor> {
match self.inner.get_by_name(name) {
Some(&Definition {
kind: DefinitionKind::Service(index),
..
}) => Some(ServiceDescriptor {
pool: self.clone(),
index,
}),
_ => None,
}
}
}
impl fmt::Debug for DescriptorPool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DescriptorPool")
.field("files", &debug_fmt_iter(self.files()))
.field("services", &debug_fmt_iter(self.services()))
.field("all_messages", &debug_fmt_iter(self.all_messages()))
.field("all_enums", &debug_fmt_iter(self.all_enums()))
.field("all_extensions", &debug_fmt_iter(self.all_extensions()))
.finish()
}
}
impl PartialEq for DescriptorPool {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for DescriptorPool {}
impl FileDescriptor {
/// Create a new [`FileDescriptor`] referencing the file at `index` within the given [`DescriptorPool`].
///
/// # Panics
///
/// Panics if `index` is out-of-bounds.
pub fn new(descriptor_pool: DescriptorPool, index: usize) -> Self {
debug_assert!(index < descriptor_pool.files().len());
FileDescriptor {
pool: descriptor_pool,
index: to_index(index),
}
}
/// Gets a reference to the [`DescriptorPool`] this file is included in.
pub fn parent_pool(&self) -> &DescriptorPool {
&self.pool
}
/// Gets the unique name of this file relative to the root of the source tree,
/// e.g. `path/to/my_package.proto`.
pub fn name(&self) -> &str {
self.inner().prost.name()
}
/// Gets the name of the package specifier for a file, e.g. `my.package`.
///
/// If no package name is set, an empty string is returned.
pub fn package_name(&self) -> &str {
self.inner().prost.package()
}
/// Gets the index of this file within the parent [`DescriptorPool`].
pub fn index(&self) -> usize {
self.index as usize
}
/// Gets the syntax of this protobuf file.
pub fn syntax(&self) -> Syntax {
self.inner().syntax
}
/// Gets the dependencies of this file.
///
/// This corresponds to the [`FileDescriptorProto::dependency`] field.
pub fn dependencies(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
let pool = self.parent_pool();
self.file_descriptor_proto()
.dependency
.iter()
.map(|name| pool.get_file_by_name(name).expect("file not found"))
}
/// Gets the public dependencies of this file.
///
/// This corresponds to the [`FileDescriptorProto::public_dependency`] field.
pub fn public_dependencies(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
let pool = self.parent_pool();
let raw = self.file_descriptor_proto();
raw.public_dependency.iter().map(|&index| {
pool.get_file_by_name(&raw.dependency[index as usize])
.expect("file not found")
})
}
/// Gets the top-level message types defined within this file.
///
/// This does not include nested messages defined within another message.
pub fn messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
let pool = self.parent_pool();
let raw_file = self.file_descriptor_proto();
raw_file.message_type.iter().map(move |raw_message| {
pool.get_message_by_name(join_name(raw_file.package(), raw_message.name()).as_ref())
.expect("message not found")
})
}
/// Gets the top-level enum types defined within this file.
///
/// This does not include nested enums defined within another message.
pub fn enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
let pool = self.parent_pool();
let raw_file = self.file_descriptor_proto();
raw_file.enum_type.iter().map(move |raw_enum| {
pool.get_enum_by_name(join_name(raw_file.package(), raw_enum.name()).as_ref())
.expect("enum not found")
})
}
/// Gets the top-level extension fields defined within this file.
///
/// This does not include nested extensions defined within another message.
pub fn extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
let pool = self.parent_pool();
let raw_file = self.file_descriptor_proto();
raw_file.extension.iter().map(move |raw_extension| {
pool.get_extension_by_name(join_name(raw_file.package(), raw_extension.name()).as_ref())
.expect("extension not found")
})
}
/// Gets the services defined within this file.
pub fn services(&self) -> impl ExactSizeIterator<Item = ServiceDescriptor> + '_ {
let pool = self.parent_pool();
let raw_file = self.file_descriptor_proto();
raw_file.service.iter().map(move |raw_service| {
pool.get_service_by_name(join_name(raw_file.package(), raw_service.name()).as_ref())
.expect("service not found")
})
}
/// Gets a reference to the raw [`FileDescriptorProto`] wrapped by this [`FileDescriptor`].
pub fn file_descriptor_proto(&self) -> &FileDescriptorProto {
&self.inner().prost
}
/// Encodes this file descriptor to its byte representation.
///
/// The encoded message is equivalent to a [`FileDescriptorProto`], however also includes
/// any extension options that were defined.
pub fn encode<B>(&self, buf: B) -> Result<(), EncodeError>
where
B: BufMut,
{
let mut buf = buf;
self.inner().raw.encode(&mut buf)
}
/// Encodes this file descriptor to a newly allocated buffer.
///
/// The encoded message is equivalent to a [`FileDescriptorProto`], however also includes
/// any extension options that were defined.
pub fn encode_to_vec(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.encode(&mut buf).expect("vec should have capacity");
buf
}
/// Decodes the options defined for this [`FileDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.FileOptions",
&self.inner().raw.options,
)
}
fn inner(&self) -> &FileDescriptorInner {
&self.pool.inner.files[self.index as usize]
}
}
impl fmt::Debug for FileDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FileDescriptor")
.field("name", &self.name())
.field("package_name", &self.package_name())
.finish()
}
}
impl MessageDescriptor {
/// Gets a reference to the [`DescriptorPool`] this message is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
&self.pool
}
/// Gets the [`FileDescriptor`] this message is defined in.
pub fn parent_file(&self) -> FileDescriptor {
FileDescriptor {
pool: self.pool.clone(),
index: self.inner().id.file,
}
}
/// Gets the parent message type if this message type is nested inside a another message, or `None` otherwise
pub fn parent_message(&self) -> Option<MessageDescriptor> {
self.inner().parent.map(|index| MessageDescriptor {
pool: self.pool.clone(),
index,
})
}
/// Gets the short name of the message type, e.g. `MyMessage`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the message type, e.g. `my.package.MyMessage`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the name of the package this message type is defined in, e.g. `my.package`.
///
/// If no package name is set, an empty string is returned.
pub fn package_name(&self) -> &str {
self.raw_file().package()
}
/// Gets the path where this message is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[4, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the [`FileDescriptorProto`] in which this message is defined.
pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].prost
}
/// Gets a reference to the raw [`DescriptorProto`] wrapped by this [`MessageDescriptor`].
pub fn descriptor_proto(&self) -> &DescriptorProto {
find_message_proto_prost(self.parent_file_descriptor_proto(), self.path())
}
/// Decodes the options defined for this [`MessageDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.MessageOptions",
&self.raw().options,
)
}
/// Gets an iterator yielding a [`FieldDescriptor`] for each field defined in this message.
pub fn fields(&self) -> impl ExactSizeIterator<Item = FieldDescriptor> + '_ {
self.inner()
.field_numbers
.values()
.map(|&index| FieldDescriptor {
message: self.clone(),
index,
})
}
/// Gets an iterator yielding a [`OneofDescriptor`] for each oneof field defined in this message.
pub fn oneofs(&self) -> impl ExactSizeIterator<Item = OneofDescriptor> + '_ {
indices(&self.inner().oneofs).map(|index| OneofDescriptor {
message: self.clone(),
index,
})
}
/// Gets the nested message types defined within this message.
pub fn child_messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
let pool = self.parent_pool();
let namespace = self.full_name();
let raw_message = self.descriptor_proto();
raw_message.nested_type.iter().map(move |raw_message| {
pool.get_message_by_name(join_name(namespace, raw_message.name()).as_ref())
.expect("message not found")
})
}
/// Gets the nested enum types defined within this message.
pub fn child_enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
let pool = self.parent_pool();
let namespace = self.full_name();
let raw_message = self.descriptor_proto();
raw_message.enum_type.iter().map(move |raw_enum| {
pool.get_enum_by_name(join_name(namespace, raw_enum.name()).as_ref())
.expect("enum not found")
})
}
/// Gets the nested extension fields defined within this message.
///
/// Note this only returns extensions defined nested within this message. See
/// [`MessageDescriptor::extensions`] to get fields defined anywhere that extend this message.
pub fn child_extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
let pool = self.parent_pool();
let namespace = self.full_name();
let raw_message = self.descriptor_proto();
raw_message.extension.iter().map(move |raw_extension| {
pool.get_extension_by_name(join_name(namespace, raw_extension.name()).as_ref())
.expect("extension not found")
})
}
/// Gets an iterator over all extensions to this message defined in the parent [`DescriptorPool`].
///
/// Note this iterates over extension fields defined anywhere which extend this message. See
/// [`MessageDescriptor::child_extensions`] to just get extensions defined nested within this message.
pub fn extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
self.inner()
.extensions
.iter()
.map(|&index| ExtensionDescriptor {
pool: self.parent_pool().clone(),
index,
})
}
/// Gets a [`FieldDescriptor`] with the given number, or `None` if no such field exists.
pub fn get_field(&self, number: u32) -> Option<FieldDescriptor> {
self.inner()
.field_numbers
.get(&number)
.map(|&index| FieldDescriptor {
message: self.clone(),
index,
})
}
/// Gets a [`FieldDescriptor`] with the given name, or `None` if no such field exists.
pub fn get_field_by_name(&self, name: &str) -> Option<FieldDescriptor> {
self.inner()
.field_names
.get(name)
.map(|&index| FieldDescriptor {
message: self.clone(),
index,
})
}
/// Gets a [`FieldDescriptor`] with the given JSON name, or `None` if no such field exists.
pub fn get_field_by_json_name(&self, json_name: &str) -> Option<FieldDescriptor> {
self.inner()
.field_json_names
.get(json_name)
.map(|&index| FieldDescriptor {
message: self.clone(),
index,
})
}
/// Returns `true` if this is an auto-generated message type to
/// represent the entry type for a map field.
//
/// If this method returns `true`, [`fields`][Self::fields] is guaranteed to
/// yield the following two fields:
///
/// * A "key" field with a field number of 1
/// * A "value" field with a field number of 2
///
/// See [`map_entry_key_field`][MessageDescriptor::map_entry_key_field] and
/// [`map_entry_value_field`][MessageDescriptor::map_entry_value_field] for more a convenient way
/// to get these fields.
pub fn is_map_entry(&self) -> bool {
self.raw()
.options
.as_ref()
.map(|o| o.value.map_entry())
.unwrap_or(false)
}
/// If this is a [map entry](MessageDescriptor::is_map_entry), returns a [`FieldDescriptor`] for the key.
///
/// # Panics
///
/// This method may panic if [`is_map_entry`][MessageDescriptor::is_map_entry] returns `false`.
pub fn map_entry_key_field(&self) -> FieldDescriptor {
debug_assert!(self.is_map_entry());
self.get_field(MAP_ENTRY_KEY_NUMBER)
.expect("map entry should have key field")
}
/// If this is a [map entry](MessageDescriptor::is_map_entry), returns a [`FieldDescriptor`] for the value.
///
/// # Panics
///
/// This method may panic if [`is_map_entry`][MessageDescriptor::is_map_entry] returns `false`.
pub fn map_entry_value_field(&self) -> FieldDescriptor {
debug_assert!(self.is_map_entry());
self.get_field(MAP_ENTRY_VALUE_NUMBER)
.expect("map entry should have value field")
}
/// Gets an iterator over reserved field number ranges in this message.
pub fn reserved_ranges(&self) -> impl ExactSizeIterator<Item = Range<u32>> + '_ {
self.raw()
.reserved_range
.iter()
.map(|n| (n.start() as u32)..(n.end() as u32))
}
/// Gets an iterator over reserved field names in this message.
pub fn reserved_names(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
self.raw().reserved_name.iter().map(|n| n.as_ref())
}
/// Gets an iterator over extension field number ranges in this message.
pub fn extension_ranges(&self) -> impl ExactSizeIterator<Item = Range<u32>> + '_ {
self.raw()
.extension_range
.iter()
.map(|n| (n.start() as u32)..(n.end() as u32))
}
/// Gets an extension to this message by its number, or `None` if no such extension exists.
pub fn get_extension(&self, number: u32) -> Option<ExtensionDescriptor> {
self.extensions().find(|ext| ext.number() == number)
}
/// Gets an extension to this message by its full name (e.g. `my.package.my_extension`), or `None` if no such extension exists.
pub fn get_extension_by_full_name(&self, name: &str) -> Option<ExtensionDescriptor> {
self.extensions().find(|ext| ext.full_name() == name)
}
/// Gets an extension to this message by its JSON name (e.g. `[my.package.my_extension]`), or `None` if no such extension exists.
pub fn get_extension_by_json_name(&self, name: &str) -> Option<ExtensionDescriptor> {
self.extensions().find(|ext| ext.json_name() == name)
}
fn inner(&self) -> &MessageDescriptorInner {
&self.pool.inner.messages[self.index as usize]
}
fn raw(&self) -> &types::DescriptorProto {
find_message_proto(self.raw_file(), self.path())
}
fn raw_file(&self) -> &types::FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].raw
}
}
impl fmt::Debug for MessageDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MessageDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("is_map_entry", &self.is_map_entry())
.field("fields", &debug_fmt_iter(self.fields()))
.field("oneofs", &debug_fmt_iter(self.oneofs()))
.finish()
}
}
impl FieldDescriptor {
/// Gets a reference to the [`DescriptorPool`] this field is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
self.message.parent_pool()
}
/// Gets the [`FileDescriptor`] this field is defined in.
pub fn parent_file(&self) -> FileDescriptor {
self.message.parent_file()
}
/// Gets a reference to the [`MessageDescriptor`] this field is defined in.
pub fn parent_message(&self) -> &MessageDescriptor {
&self.message
}
/// Gets the short name of the message type, e.g. `my_field`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the message field, e.g. `my.package.MyMessage.my_field`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the path where this message field is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[4, 0, 2, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the raw [`FieldDescriptorProto`] wrapped by this [`FieldDescriptor`].
pub fn field_descriptor_proto(&self) -> &FieldDescriptorProto {
&self.parent_message().descriptor_proto().field[*self.path().last().unwrap() as usize]
}
/// Decodes the options defined for this [`FieldDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.FieldOptions",
&self.raw().options,
)
}
/// Gets the unique number for this message field.
pub fn number(&self) -> u32 {
self.inner().number
}
/// Gets the name used for JSON serialization.
///
/// This is usually the camel-cased form of the field name, unless
/// another value is set in the proto file.
pub fn json_name(&self) -> &str {
&self.inner().json_name
}
/// Whether this field is encoded using the proto2 group encoding.
pub fn is_group(&self) -> bool {
matches!(self.inner().kind, KindIndex::Group(_))
}
/// Whether this field is a list type.
///
/// Equivalent to checking that the cardinality is `Repeated` and that
/// [`is_map`][Self::is_map] returns `false`.
pub fn is_list(&self) -> bool {
self.cardinality() == Cardinality::Repeated && !self.is_map()
}
/// Whether this field is a map type.
///
/// Equivalent to checking that the cardinality is `Repeated` and that
/// the field type is a message where [`is_map_entry`][MessageDescriptor::is_map_entry]
/// returns `true`.
pub fn is_map(&self) -> bool {
self.cardinality() == Cardinality::Repeated
&& match self.kind() {
Kind::Message(message) => message.is_map_entry(),
_ => false,
}
}
/// Whether this field is a list encoded using [packed encoding](https://developers.google.com/protocol-buffers/docs/encoding#packed).
pub fn is_packed(&self) -> bool {
self.inner().is_packed
}
/// The cardinality of this field.
pub fn cardinality(&self) -> Cardinality {
self.inner().cardinality
}
/// Whether this field supports distinguishing between an unpopulated field and
/// the default value.
///
/// For proto2 messages this returns `true` for all non-repeated fields.
/// For proto3 this returns `true` for message fields, and fields contained
/// in a `oneof`.
pub fn supports_presence(&self) -> bool {
self.inner().supports_presence
}
/// Gets the [`Kind`] of this field.
pub fn kind(&self) -> Kind {
Kind::new(self.parent_pool(), self.inner().kind)
}
/// Gets a [`OneofDescriptor`] representing the oneof containing this field,
/// or `None` if this field is not contained in a oneof.
pub fn containing_oneof(&self) -> Option<OneofDescriptor> {
self.inner().oneof.map(|index| OneofDescriptor {
message: self.message.clone(),
index,
})
}
pub(crate) fn default_value(&self) -> Option<&Value> {
self.inner().default.as_ref()
}
pub(crate) fn is_packable(&self) -> bool {
self.inner().kind.is_packable()
}
fn inner(&self) -> &FieldDescriptorInner {
&self.message.inner().fields[self.index as usize]
}
fn raw(&self) -> &types::FieldDescriptorProto {
&self.message.raw().field[self.index as usize]
}
}
impl fmt::Debug for FieldDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FieldDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("json_name", &self.json_name())
.field("number", &self.number())
.field("kind", &self.kind())
.field("cardinality", &self.cardinality())
.field(
"containing_oneof",
&self.containing_oneof().map(|o| o.name().to_owned()),
)
.field("default_value", &self.default_value())
.field("is_group", &self.is_group())
.field("is_list", &self.is_list())
.field("is_map", &self.is_map())
.field("is_packed", &self.is_packed())
.field("supports_presence", &self.supports_presence())
.finish()
}
}
impl ExtensionDescriptor {
/// Gets a reference to the [`DescriptorPool`] this extension field is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
&self.pool
}
/// Gets the [`FileDescriptor`] this extension field is defined in.
pub fn parent_file(&self) -> FileDescriptor {
FileDescriptor {
pool: self.pool.clone(),
index: self.inner().id.file,
}
}
/// Gets the parent message type if this extension is defined within another message, or `None` otherwise.
///
/// Note this just corresponds to where the extension is defined in the proto file. See [`containing_message`][ExtensionDescriptor::containing_message]
/// for the message this field extends.
pub fn parent_message(&self) -> Option<MessageDescriptor> {
self.inner().parent.map(|index| MessageDescriptor {
pool: self.pool.clone(),
index,
})
}
/// Gets the short name of the extension field type, e.g. `my_extension`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the extension field, e.g. `my.package.ParentMessage.my_extension`.
///
/// Note this includes the name of the parent message if any, not the message this field extends.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the name of the package this extension field is defined in, e.g. `my.package`.
///
/// If no package name is set, an empty string is returned.
pub fn package_name(&self) -> &str {
self.raw_file().package()
}
/// Gets the path where this extension field is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[7, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the [`FileDescriptorProto`] in which this extension is defined.
pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].prost
}
/// Gets a reference to the raw [`FieldDescriptorProto`] wrapped by this [`ExtensionDescriptor`].
pub fn field_descriptor_proto(&self) -> &FieldDescriptorProto {
let file = self.parent_file_descriptor_proto();
let path = self.path();
debug_assert_ne!(path.len(), 0);
debug_assert_eq!(path.len() % 2, 0);
if path.len() == 2 {
debug_assert_eq!(path[0], tag::file::EXTENSION);
&file.extension[path[1] as usize]
} else {
let message = find_message_proto_prost(file, &path[..path.len() - 2]);
debug_assert_eq!(path[path.len() - 2], tag::message::EXTENSION);
&message.extension[path[path.len() - 1] as usize]
}
}
/// Decodes the options defined for this [`ExtensionDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.FieldOptions",
&self.raw().options,
)
}
/// Gets the number for this extension field.
pub fn number(&self) -> u32 {
self.inner().number
}
/// Gets the name used for JSON serialization of this extension field, e.g. `[my.package.ParentMessage.my_field]`.
pub fn json_name(&self) -> &str {
&self.inner().json_name
}
/// Whether this field is encoded using the proto2 group encoding.
pub fn is_group(&self) -> bool {
matches!(self.inner().kind, KindIndex::Group(_))
}
/// Whether this field is a list type.
///
/// Equivalent to checking that the cardinality is `Repeated` and that
/// [`is_map`][Self::is_map] returns `false`.
pub fn is_list(&self) -> bool {
self.cardinality() == Cardinality::Repeated && !self.is_map()
}
/// Whether this field is a map type.
///
/// Equivalent to checking that the cardinality is `Repeated` and that
/// the field type is a message where [`is_map_entry`][MessageDescriptor::is_map_entry]
/// returns `true`.
pub fn is_map(&self) -> bool {
self.cardinality() == Cardinality::Repeated
&& match self.kind() {
Kind::Message(message) => message.is_map_entry(),
_ => false,
}
}
/// Whether this field is a list encoded using [packed encoding](https://developers.google.com/protocol-buffers/docs/encoding#packed).
pub fn is_packed(&self) -> bool {
self.inner().is_packed
}
/// The cardinality of this field.
pub fn cardinality(&self) -> Cardinality {
self.inner().cardinality
}
/// Whether this extension supports distinguishing between an unpopulated field and
/// the default value.
///
/// This is equivalent to `cardinality() != Cardinality::Repeated`
pub fn supports_presence(&self) -> bool {
self.cardinality() != Cardinality::Repeated
}
/// Gets the [`Kind`] of this field.
pub fn kind(&self) -> Kind {
Kind::new(&self.pool, self.inner().kind)
}
/// Gets the containing message that this field extends.
pub fn containing_message(&self) -> MessageDescriptor {
MessageDescriptor {
pool: self.pool.clone(),
index: self.inner().extendee,
}
}
pub(crate) fn default_value(&self) -> Option<&Value> {
self.inner().default.as_ref()
}
pub(crate) fn is_packable(&self) -> bool {
self.inner().kind.is_packable()
}
fn inner(&self) -> &ExtensionDescriptorInner {
&self.pool.inner.extensions[self.index as usize]
}
fn raw(&self) -> &types::FieldDescriptorProto {
let file = self.raw_file();
let path = self.path();
debug_assert_ne!(path.len(), 0);
debug_assert_eq!(path.len() % 2, 0);
if path.len() == 2 {
debug_assert_eq!(path[0], tag::file::EXTENSION);
&file.extension[path[1] as usize]
} else {
let message = find_message_proto(file, &path[..path.len() - 2]);
debug_assert_eq!(path[path.len() - 2], tag::message::EXTENSION);
&message.extension[path[path.len() - 1] as usize]
}
}
fn raw_file(&self) -> &types::FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].raw
}
}
impl fmt::Debug for ExtensionDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExtensionDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("json_name", &self.json_name())
.field("number", &self.number())
.field("kind", &self.kind())
.field("cardinality", &self.cardinality())
.field(
"containing_message",
&self.containing_message().name().to_owned(),
)
.field("default_value", &self.default_value())
.field("is_group", &self.is_group())
.field("is_list", &self.is_list())
.field("is_map", &self.is_map())
.field("is_packed", &self.is_packed())
.field("supports_presence", &self.supports_presence())
.finish()
}
}
impl EnumDescriptor {
/// Gets a reference to the [`DescriptorPool`] this enum type is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
&self.pool
}
/// Gets the [`FileDescriptor`] this enum type is defined in.
pub fn parent_file(&self) -> FileDescriptor {
FileDescriptor {
pool: self.pool.clone(),
index: self.inner().id.file,
}
}
/// Gets the parent message type if this enum type is nested inside a another message, or `None` otherwise
pub fn parent_message(&self) -> Option<MessageDescriptor> {
self.inner().parent.map(|index| MessageDescriptor {
pool: self.pool.clone(),
index,
})
}
/// Gets the short name of the enum type, e.g. `MyEnum`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the enum, e.g. `my.package.MyEnum`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the name of the package this enum type is defined in, e.g. `my.package`.
///
/// If no package name is set, an empty string is returned.
pub fn package_name(&self) -> &str {
self.raw_file().package()
}
/// Gets the path where this enum type is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[5, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the [`FileDescriptorProto`] in which this enum is defined.
pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].prost
}
/// Gets a reference to the raw [`EnumDescriptorProto`] wrapped by this [`EnumDescriptor`].
pub fn enum_descriptor_proto(&self) -> &EnumDescriptorProto {
let file = self.parent_file_descriptor_proto();
let path = self.path();
debug_assert_ne!(path.len(), 0);
debug_assert_eq!(path.len() % 2, 0);
if path.len() == 2 {
debug_assert_eq!(path[0], tag::file::ENUM_TYPE);
&file.enum_type[path[1] as usize]
} else {
let message = find_message_proto_prost(file, &path[..path.len() - 2]);
debug_assert_eq!(path[path.len() - 2], tag::message::ENUM_TYPE);
&message.enum_type[path[path.len() - 1] as usize]
}
}
/// Decodes the options defined for this [`EnumDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.EnumOptions",
&self.raw().options,
)
}
/// Gets the default value for the enum type.
pub fn default_value(&self) -> EnumValueDescriptor {
EnumValueDescriptor {
parent: self.clone(),
index: 0,
}
}
/// Gets a [`EnumValueDescriptor`] for the enum value with the given name, or `None` if no such value exists.
pub fn get_value_by_name(&self, name: &str) -> Option<EnumValueDescriptor> {
self.inner()
.value_names
.get(name)
.map(|&index| EnumValueDescriptor {
parent: self.clone(),
index,
})
}
/// Gets a [`EnumValueDescriptor`] for the enum value with the given number, or `None` if no such value exists.
///
/// If the enum is defined with the `allow_alias` option and has multiple values with the given number, it is
/// unspecified which one will be returned.
pub fn get_value(&self, number: i32) -> Option<EnumValueDescriptor> {
self.inner()
.value_numbers
.binary_search_by(|(l, _)| l.cmp(&number))
.ok()
.map(|index| EnumValueDescriptor {
parent: self.clone(),
index: self.inner().value_numbers[index].1,
})
}
/// Gets an iterator yielding a [`EnumValueDescriptor`] for each value in this enum.
pub fn values(&self) -> impl ExactSizeIterator<Item = EnumValueDescriptor> + '_ {
self.inner()
.value_numbers
.iter()
.map(|&(_, index)| EnumValueDescriptor {
parent: self.clone(),
index,
})
}
/// Gets an iterator over reserved value number ranges in this enum.
pub fn reserved_ranges(&self) -> impl ExactSizeIterator<Item = RangeInclusive<i32>> + '_ {
self.raw()
.reserved_range
.iter()
.map(|n| n.start()..=n.end())
}
/// Gets an iterator over reserved value names in this enum.
pub fn reserved_names(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
self.raw().reserved_name.iter().map(|n| n.as_ref())
}
fn inner(&self) -> &EnumDescriptorInner {
&self.pool.inner.enums[self.index as usize]
}
fn raw(&self) -> &types::EnumDescriptorProto {
find_enum_proto(self.raw_file(), self.path())
}
fn raw_file(&self) -> &types::FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].raw
}
}
impl fmt::Debug for EnumDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EnumDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("default_value", &self.default_value())
.field("values", &debug_fmt_iter(self.values()))
.finish()
}
}
impl EnumValueDescriptor {
/// Gets a reference to the [`DescriptorPool`] this enum value is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
self.parent.parent_pool()
}
/// Gets the [`FileDescriptor`] this enum value is defined in.
pub fn parent_file(&self) -> FileDescriptor {
self.parent.parent_file()
}
/// Gets a reference to the [`EnumDescriptor`] this enum value is defined in.
pub fn parent_enum(&self) -> &EnumDescriptor {
&self.parent
}
/// Gets the short name of the enum value, e.g. `MY_VALUE`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the enum value, e.g. `my.package.MY_VALUE`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the path where this enum value is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[5, 0, 2, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the raw [`EnumValueDescriptorProto`] wrapped by this [`EnumValueDescriptor`].
pub fn enum_value_descriptor_proto(&self) -> &EnumValueDescriptorProto {
&self.parent.enum_descriptor_proto().value[self.index as usize]
}
/// Decodes the options defined for this [`EnumValueDescriptor`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.EnumValueOptions",
&self.raw().options,
)
}
/// Gets the number representing this enum value.
pub fn number(&self) -> i32 {
self.inner().number
}
fn inner(&self) -> &EnumValueDescriptorInner {
&self.parent.inner().values[self.index as usize]
}
fn raw(&self) -> &types::EnumValueDescriptorProto {
&self.parent.raw().value[self.index as usize]
}
}
impl fmt::Debug for EnumValueDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EnumValueDescriptor")
.field("name", &self.number())
.field("full_name", &self.full_name())
.field("number", &self.number())
.finish()
}
}
impl OneofDescriptor {
/// Gets a reference to the [`DescriptorPool`] this oneof is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
self.message.parent_pool()
}
/// Gets the [`FileDescriptor`] this oneof is defined in.
pub fn parent_file(&self) -> FileDescriptor {
self.message.parent_file()
}
/// Gets a reference to the [`MessageDescriptor`] this oneof is defined in.
pub fn parent_message(&self) -> &MessageDescriptor {
&self.message
}
/// Gets the short name of the oneof, e.g. `my_oneof`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the oneof, e.g. `my.package.MyMessage.my_oneof`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the path where this oneof is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[4, 0, 8, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the raw [`OneofDescriptorProto`] wrapped by this [`OneofDescriptor`].
pub fn oneof_descriptor_proto(&self) -> &OneofDescriptorProto {
&self.message.descriptor_proto().oneof_decl[self.index as usize]
}
/// Decodes the options defined for this [`OneofDescriptorProto`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.OneofOptions",
&self.raw().options,
)
}
/// Gets an iterator yielding a [`FieldDescriptor`] for each field of the parent message this oneof contains.
pub fn fields(&self) -> impl ExactSizeIterator<Item = FieldDescriptor> + '_ {
self.inner().fields.iter().map(|&index| FieldDescriptor {
message: self.parent_message().clone(),
index,
})
}
fn inner(&self) -> &OneofDescriptorInner {
&self.message.inner().oneofs[self.index as usize]
}
fn raw(&self) -> &types::OneofDescriptorProto {
&self.message.raw().oneof_decl[self.index as usize]
}
}
impl fmt::Debug for OneofDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OneofDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("fields", &debug_fmt_iter(self.fields()))
.finish()
}
}
impl ServiceDescriptor {
/// Create a new [`ServiceDescriptor`] referencing the service at `index` within the given [`DescriptorPool`].
///
/// # Panics
///
/// Panics if `index` is out-of-bounds.
pub fn new(pool: DescriptorPool, index: usize) -> Self {
debug_assert!(index < pool.services().len());
ServiceDescriptor {
pool,
index: to_index(index),
}
}
/// Returns the index of this [`ServiceDescriptor`] within the parent [`DescriptorPool`].
pub fn index(&self) -> usize {
self.index as usize
}
/// Gets a reference to the [`DescriptorPool`] this service is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
&self.pool
}
/// Gets the [`FileDescriptor`] this service is defined in.
pub fn parent_file(&self) -> FileDescriptor {
FileDescriptor {
pool: self.pool.clone(),
index: self.inner().id.file,
}
}
/// Gets the short name of the service, e.g. `MyService`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the service, e.g. `my.package.Service`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the name of the package this service is defined in, e.g. `my.package`.
///
/// If no package name is set, an empty string is returned.
pub fn package_name(&self) -> &str {
self.raw_file().package()
}
/// Gets the path where this service is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[6, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the [`FileDescriptorProto`] in which this service is defined.
pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].prost
}
/// Gets a reference to the raw [`ServiceDescriptorProto`] wrapped by this [`ServiceDescriptor`].
pub fn service_descriptor_proto(&self) -> &ServiceDescriptorProto {
let path = self.path();
debug_assert!(!path.is_empty());
&self.parent_file_descriptor_proto().service[*path.last().unwrap() as usize]
}
/// Decodes the options defined for this [`ServiceDescriptorProto`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.ServiceOptions",
&self.raw().options,
)
}
/// Gets an iterator yielding a [`MethodDescriptor`] for each method defined in this service.
pub fn methods(&self) -> impl ExactSizeIterator<Item = MethodDescriptor> + '_ {
indices(&self.inner().methods).map(|index| MethodDescriptor {
service: self.clone(),
index,
})
}
fn inner(&self) -> &ServiceDescriptorInner {
&self.pool.inner.services[self.index as usize]
}
fn raw(&self) -> &types::ServiceDescriptorProto {
let path = self.path();
debug_assert!(!path.is_empty());
&self.raw_file().service[*path.last().unwrap() as usize]
}
fn raw_file(&self) -> &types::FileDescriptorProto {
&self.pool.inner.files[self.inner().id.file as usize].raw
}
}
impl fmt::Debug for ServiceDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ServiceDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("index", &self.index())
.field("methods", &debug_fmt_iter(self.methods()))
.finish()
}
}
impl MethodDescriptor {
/// Create a new [`MethodDescriptor`] referencing the method at `index` within the [`ServiceDescriptor`].
///
/// # Panics
///
/// Panics if `index` is out-of-bounds.
pub fn new(service: ServiceDescriptor, index: usize) -> Self {
debug_assert!(index < service.methods().len());
MethodDescriptor {
service,
index: to_index(index),
}
}
/// Gets the index of the method within the parent [`ServiceDescriptor`].
pub fn index(&self) -> usize {
self.index as usize
}
/// Gets a reference to the [`ServiceDescriptor`] this method is defined in.
pub fn parent_service(&self) -> &ServiceDescriptor {
&self.service
}
/// Gets a reference to the [`DescriptorPool`] this method is defined in.
pub fn parent_pool(&self) -> &DescriptorPool {
self.service.parent_pool()
}
/// Gets the [`FileDescriptor`] this method is defined in.
pub fn parent_file(&self) -> FileDescriptor {
self.service.parent_file()
}
/// Gets the short name of the method, e.g. `method`.
pub fn name(&self) -> &str {
self.inner().id.name()
}
/// Gets the full name of the method, e.g. `my.package.MyService.my_method`.
pub fn full_name(&self) -> &str {
self.inner().id.full_name()
}
/// Gets the path where this method is defined within the [`FileDescriptorProto`][FileDescriptorProto], e.g. `[6, 0, 2, 0]`.
///
/// See [`path`][prost_types::source_code_info::Location::path] for more details on the structure of the path.
pub fn path(&self) -> &[i32] {
&self.inner().id.path
}
/// Gets a reference to the raw [`MethodDescriptorProto`] wrapped by this [`MethodDescriptor`].
pub fn method_descriptor_proto(&self) -> &MethodDescriptorProto {
&self.service.service_descriptor_proto().method[self.index as usize]
}
/// Decodes the options defined for this [`MethodDescriptorProto`], including any extension options.
pub fn options(&self) -> DynamicMessage {
decode_options(
self.parent_pool(),
"google.protobuf.MethodOptions",
&self.raw().options,
)
}
/// Gets the [`MessageDescriptor`] for the input type of this method.
pub fn input(&self) -> MessageDescriptor {
MessageDescriptor {
pool: self.parent_pool().clone(),
index: self.inner().input,
}
}
/// Gets the [`MessageDescriptor`] for the output type of this method.
pub fn output(&self) -> MessageDescriptor {
MessageDescriptor {
pool: self.parent_pool().clone(),
index: self.inner().output,
}
}
/// Returns `true` if the client streams multiple messages.
pub fn is_client_streaming(&self) -> bool {
self.raw().client_streaming()
}
/// Returns `true` if the server streams multiple messages.
pub fn is_server_streaming(&self) -> bool {
self.raw().server_streaming()
}
fn inner(&self) -> &MethodDescriptorInner {
&self.service.inner().methods[self.index as usize]
}
fn raw(&self) -> &types::MethodDescriptorProto {
&self.service.raw().method[self.index as usize]
}
}
impl fmt::Debug for MethodDescriptor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MethodDescriptor")
.field("name", &self.name())
.field("full_name", &self.full_name())
.field("index", &self.index())
.field("input", &self.input())
.field("output", &self.output())
.field("is_client_streaming", &self.is_client_streaming())
.field("is_server_streaming", &self.is_server_streaming())
.finish()
}
}
fn debug_fmt_iter<I>(i: I) -> impl fmt::Debug
where
I: Iterator,
I::Item: fmt::Debug,
{
struct Wrapper<T>(Vec<T>);
impl<T> fmt::Debug for Wrapper<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(&self.0).finish()
}
}
Wrapper(i.collect())
}
#[allow(clippy::ptr_arg)]
fn indices<T>(f: &Vec<T>) -> Range<DescriptorIndex> {
0..to_index(f.len())
}
fn join_name<'a>(namespace: &str, name: &'a str) -> Cow<'a, str> {
if namespace.is_empty() {
Cow::Borrowed(name)
} else {
Cow::Owned(format!("{}.{}", namespace, name))
}
}
fn decode_options<T>(
pool: &DescriptorPool,
name: &str,
option: &Option<Options<T>>,
) -> DynamicMessage {
let message_desc = pool
.get_message_by_name(name)
.unwrap_or_else(|| DescriptorPool::global().get_message_by_name(name).unwrap());
let bytes = option
.as_ref()
.map(|o| o.encoded.as_slice())
.unwrap_or_default();
DynamicMessage::decode(message_desc, bytes).unwrap()
}
fn find_message_proto_prost<'a>(
file: &'a FileDescriptorProto,
path: &[i32],
) -> &'a DescriptorProto {
debug_assert_ne!(path.len(), 0);
debug_assert_eq!(path.len() % 2, 0);
let mut message: Option<&'a DescriptorProto> = None;
for part in path.chunks(2) {
match part[0] {
tag::file::MESSAGE_TYPE => message = Some(&file.message_type[part[1] as usize]),
tag::message::NESTED_TYPE => {
message = Some(&message.unwrap().nested_type[part[1] as usize])
}
_ => panic!("invalid message path"),
}
}
message.unwrap()
}