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 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
//! Models and custom GraphQL endpoints.
use crate::engine::schema::{rel_name_variants, type_name_variants};
use crate::Error;
use log::trace;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fs::File;
use std::io::BufReader;
use std::slice::Iter;
const LATEST_CONFIG_VERSION: i32 = 2;
// Convenience function for setting serde default value
fn get_false() -> bool {
false
}
// Convenience function for setting serde default value
fn get_true() -> bool {
true
}
// Convenience function for setting serde default value
fn get_none() -> Option<String> {
None
}
/// Configuration for a Warpgrapher data model. The configuration contains the version of the
/// Warpgrapher configuration file format, a vector of [`Type`] structures, and a vector of
/// [`Endpoint`] structures.
///
/// [`Endpoint`]: struct.Endpoint.html
/// [`Type`]: struct.Type.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let c = Configuration::new(1, Vec::new(), Vec::new());
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Configuration {
/// Version of the Warpgrapher configuration file format used
version: i32,
/// A vector of [`Type`] structures, each defining one type in the data model
///
/// [`Type`]: struct.Type.html
#[serde(default)]
pub model: Vec<Type>,
/// A vector of [`Endpoint`] structures, each defining a custom root endpoint in the graphql
/// schema
///
/// [`Endpoint`]: struct.Endpoint.html
#[serde(default)]
endpoints: Vec<Endpoint>,
}
impl Configuration {
/// Creates a new [`Configuration`] data structure with the version, [`Type`] vector, and
/// [`Endpoint`] vector provided as arguments.
///
/// [`Configuration`]: struct.Configuration.html
/// [`Endpoint`]: struct.Endpoint.html
/// [`Type`]: struct.Type.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let c = Configuration::new(1, Vec::new(), Vec::new());
/// ```
pub fn new(version: i32, model: Vec<Type>, endpoints: Vec<Endpoint>) -> Configuration {
Configuration {
version,
model,
endpoints,
}
}
/// Returns an iterator over the [`Endpoint`] structs defining custom root endpoints in the
/// GraphQL schema
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let c = Configuration::new(1, Vec::new(), Vec::new());
/// for e in c.endpoints() {
/// let _name = e.name();
/// }
/// ```
pub fn endpoints(&self) -> Iter<Endpoint> {
self.endpoints.iter()
}
/// Returns an iterator over the [`Type`] structs defining types in the GraphQL schema
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let c = Configuration::new(1, Vec::new(), Vec::new());
/// for t in c.types() {
/// let _name = t.name();
/// }
/// ```
pub fn types(&self) -> Iter<Type> {
self.model.iter()
}
/// Validates the [`Configuration`] data structure. Checks that there are no duplicate
/// [`Endpoint`] or [`Type`] items, and that the [`Endpoint`] input/output types are defined
/// in the model. Returns () if there are no validation errors.
///
/// # Errors
///
/// Returns an [`Error`] variant [`ConfigItemDuplicated`] if there is more than one type or
/// more than one endpoint that use the same name.
///
/// Returns an [`Error`] variant [`ConfigItemReserved`] if a named configuration item, such as
/// an endpoint or type, has a name that is a reserved word, such as "ID" or the name of a
/// GraphQL scalar type.
///
/// [`ConfigItemDuplicated`]: ../../error/enum.Error.html#variant.ConfigItemDuplicated
/// [`ConfigItemReserved`]: ../../error/enum.Error.html#variant.ConfigItemReserved
/// [`Error`]: ../../error/enum.Error.html
///
/// # Example
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let config = Configuration::new(1, Vec::new(), Vec::new());
/// config.validate();
/// ```
pub fn validate(&self) -> Result<(), Error> {
trace!("Config::validate called");
let scalar_names = ["Int", "Float", "Boolean", "String", "ID"];
self.model
.iter()
.map(|t| {
if self.model.iter().filter(|t2| t2.name == t.name).count() > 1 {
return Err(Error::ConfigItemDuplicated {
type_name: t.name.to_string(),
});
}
let name_variants = type_name_variants(t);
self.model.iter().try_for_each(|t2| {
if name_variants.contains(t2.name()) {
Err(Error::ConfigItemDuplicated {
type_name: t2.name().to_string(),
})
} else {
Ok(())
}
})?;
if scalar_names.iter().any(|s| s == &t.name) {
return Err(Error::ConfigItemReserved {
type_name: t.name.clone(),
});
}
if t.props.iter().any(|p| p.name().to_uppercase() == "ID") {
return Err(Error::ConfigItemReserved {
type_name: "ID".to_string(),
});
}
// Used by Gremlin return format to return the label itself
if t.props.iter().any(|p| p.name().to_uppercase() == "LABEL") {
return Err(Error::ConfigItemReserved {
type_name: "label".to_string(),
});
}
if t.rels
.iter()
.any(|r| r.props.iter().any(|p| p.name().to_uppercase() == "ID"))
{
return Err(Error::ConfigItemReserved {
type_name: "ID".to_string(),
});
}
// Used by Gremlin return format to return the label itself
if t.rels
.iter()
.any(|r| r.props.iter().any(|p| p.name().to_uppercase() == "LABEL"))
{
return Err(Error::ConfigItemReserved {
type_name: "label".to_string(),
});
}
if t.rels
.iter()
.any(|r| r.props.iter().any(|p| p.name().to_uppercase() == "SRC"))
{
return Err(Error::ConfigItemReserved {
type_name: "src".to_string(),
});
}
if t.rels
.iter()
.any(|r| r.props.iter().any(|p| p.name().to_uppercase() == "DST"))
{
return Err(Error::ConfigItemReserved {
type_name: "dst".to_string(),
});
}
t.rels.iter().try_for_each(|r| {
let rel_name_variants = rel_name_variants(t, r);
self.model.iter().try_for_each(|t2| {
if rel_name_variants.contains(t2.name()) {
Err(Error::ConfigItemDuplicated {
type_name: t2.name().to_string(),
})
} else {
Ok(())
}
})
})?;
Ok(())
})
.collect::<Result<Vec<_>, Error>>()?;
self.endpoints
.iter()
.map(|ep| {
// Check for duplicate endpoints
if self.endpoints.iter().filter(|e| e.name == ep.name).count() > 1 {
return Err(Error::ConfigItemDuplicated {
type_name: ep.name.to_string(),
});
}
// Check for endpoint custom input using reserved names (GraphQL scalars)
if let Some(input) = &ep.input {
if let TypeDef::Custom(t) = &input.type_def {
if scalar_names.iter().any(|s| s == &t.name) {
return Err(Error::ConfigItemReserved {
type_name: t.name.to_string(),
});
}
}
}
// Check for endpoint custom input using reserved names (GraphQL scalars)
if let TypeDef::Custom(t) = &ep.output.type_def {
if scalar_names.iter().any(|s| s == &t.name) {
return Err(Error::ConfigItemReserved {
type_name: t.name.to_string(),
});
}
}
Ok(())
})
.collect::<Result<Vec<_>, Error>>()?;
Ok(())
}
/// Returns the version number of the configuration format used for the configuration
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Configuration;
///
/// let c = Configuration::new(1, Vec::new(), Vec::new());
///
/// assert_eq!(1, c.version());
/// ```
pub fn version(&self) -> i32 {
self.version
}
}
impl Default for Configuration {
fn default() -> Configuration {
Configuration {
version: 1,
model: vec![],
endpoints: vec![],
}
}
}
impl TryFrom<File> for Configuration {
type Error = Error;
fn try_from(f: File) -> Result<Configuration, Error> {
let r = BufReader::new(f);
Ok(serde_yaml::from_reader(r)?)
}
}
impl TryFrom<String> for Configuration {
type Error = Error;
fn try_from(s: String) -> Result<Configuration, Error> {
Ok(serde_yaml::from_str(&s)?)
}
}
impl TryFrom<&str> for Configuration {
type Error = Error;
fn try_from(s: &str) -> Result<Configuration, Error> {
Ok(serde_yaml::from_str(s)?)
}
}
/// Configuration item for custom endpoints
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Endpoint {
/// Name of this Endpoint
name: String,
/// Class of endpoint (Mutation or Query)
class: EndpointClass,
/// Defines the input of the endpoint
input: Option<EndpointType>,
/// Defines the type returned by the endpoint
output: EndpointType,
}
impl Endpoint {
/// Creates a new configuration item for a custom GraphQL endpoint
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
/// ```
pub fn new(
name: String,
class: EndpointClass,
input: Option<EndpointType>,
output: EndpointType,
) -> Endpoint {
Endpoint {
name,
class,
input,
output,
}
}
/// Returns the [`EndpointClass`] of a custom GraphQL endpoint, indicating whether the custom
/// endpoint is a Query or Mutation.
///
/// [`EndpointClass`]: ./enum.EndpointClass.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
///
/// assert_eq!(&EndpointClass::Query, e.class());
/// ```
pub fn class(&self) -> &EndpointClass {
&self.class
}
/// Returns the name of a custom GraphQL endpoint.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
///
/// assert_eq!("CountItems", e.name());
/// ```
pub fn name(&self) -> &str {
&self.name
}
/// Returns the optional type definition of the input to a custom endpoint. A value of None
/// indicates that the GraphQL endpoint does not take an input.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
///
/// assert!(e.input().is_none());
/// ```
pub fn input(&self) -> Option<&EndpointType> {
self.input.as_ref()
}
/// Returns the type definition of the output for a custom endpoint
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Endpoint, EndpointClass, EndpointType, TypeDef,
/// # GraphqlType};
///
/// let e = Endpoint::new("CountItems".to_string(), EndpointClass::Query, None,
/// EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true));
///
/// assert_eq!(&EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true),
/// e.output());
/// ```
pub fn output(&self) -> &EndpointType {
&self.output
}
}
impl TryFrom<&str> for Endpoint {
type Error = Error;
/// Creates a new Endpoint struct from a yaml-formatted string.
///
/// # Errors
///
/// Returns an [`Error`] variant [`YamlDeserializationFailed`] if the yaml-formatted
/// string is improperly formatted.
///
/// [`YamlDeserializationFailed`]: ../../error/enum.Error.html#variant.YamlDeserializationFailed
/// [`Error`]: ../../error/enum.Error.html
///
/// # Examples
///
/// ```rust
/// use warpgrapher::engine::config::{Endpoint};
///
/// use std::convert::TryFrom;
/// let t = Endpoint::try_from("
/// name: RegisterUser
/// class: Mutation
/// input:
/// type: UserInput
/// output:
/// type: User
/// ").unwrap();
/// ```
fn try_from(yaml: &str) -> Result<Endpoint, Error> {
serde_yaml::from_str(yaml).map_err(|e| Error::YamlDeserializationFailed { source: e })
}
}
/// Determines whether a custom GraphQL endpoint is a query or mutation endpoint
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointClass;
///
/// let ec = EndpointClass::Query;
/// ```
#[derive(Copy, Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum EndpointClass {
/// Indicates that a custome GraphQL endpoint should be a query
Query,
/// Indicates that a cutom GraphQL endpoint should be a mutation
Mutation,
}
/// Configuration item for endpoint filters. This allows configuration to control which of the
/// basic create, read, update, and delete (CRUD) operations are auto-generated for a [`Type`] or a
/// [`Relationship`]. If a filter boolean is set to true, the operation is generated. False
/// indicates that the operation should not be generated.
///
/// [`Relationship`]: ./struct.Relationship.html
/// [`Type`]: ./struct.Type.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::new(true, true, true, true);
/// ```
#[derive(Copy, Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct EndpointsFilter {
/// True if a read endpoint should be generated for the corresponding type/rel
#[serde(default = "get_true")]
read: bool,
/// True if a create endpoint should be generated for the corresponding type/rel
#[serde(default = "get_true")]
create: bool,
/// True if a update endpoint should be generated for the corresponding type/rel
#[serde(default = "get_true")]
update: bool,
/// True if a delete endpoint should be generated for the corresponding type/rel
#[serde(default = "get_true")]
delete: bool,
}
impl EndpointsFilter {
/// Creates a new filter with the option to configure all endpoints
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::new(true, true, false, false);
/// ```
pub fn new(read: bool, create: bool, update: bool, delete: bool) -> EndpointsFilter {
EndpointsFilter {
read,
create,
update,
delete,
}
}
/// Creates a new filter with all endpoints -- create, read, update, and delete
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::all();
/// ```
pub fn all() -> EndpointsFilter {
EndpointsFilter {
read: true,
create: true,
update: true,
delete: true,
}
}
/// Returns true if Warpgrapher should generate a create operation for the relationship
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::all();
/// assert_eq!(true, ef.create());
/// ```
pub fn create(self) -> bool {
self.create
}
/// Returns true if Warpgrapher should generate a delete operation for the relationship
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::all();
/// assert_eq!(true, ef.delete());
/// ```
pub fn delete(self) -> bool {
self.delete
}
/// Creates a new filter with all endpoints -- create, read, update, and delete -- filtered out
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::none();
/// ```
pub fn none() -> EndpointsFilter {
EndpointsFilter {
read: false,
create: false,
update: false,
delete: false,
}
}
/// Returns true if Warpgrapher should generate a read operation for the relationship
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::all();
/// assert_eq!(true, ef.read());
/// ```
pub fn read(self) -> bool {
self.read
}
/// Returns true if Warpgrapher should generate a update operation for the relationship
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::EndpointsFilter;
///
/// let ef = EndpointsFilter::all();
/// assert_eq!(true, ef.update());
/// ```
pub fn update(self) -> bool {
self.update
}
}
impl Default for EndpointsFilter {
fn default() -> EndpointsFilter {
EndpointsFilter {
read: true,
create: true,
update: true,
delete: true,
}
}
}
/// Configuration item describing a type used with a custom GraphQL endpoint, either as the input
/// to the custom endpoint, or as its output
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointType, GraphqlType, TypeDef};
///
/// let et = EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true);
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EndpointType {
/// Defines option for an endpoint type to use an existing or custom type
#[serde(rename = "type")]
type_def: TypeDef,
/// Determines if the endpoint type is a list
#[serde(default = "get_false")]
list: bool,
/// Determine if the type is required (non-nullable)
#[serde(default = "get_false")]
required: bool,
}
impl EndpointType {
/// Creates a new [`EndpointType`] configuration item, describing either the input or output
/// type of a custom GraphQL endpoint
///
/// [`EndpointType`]: ./struct.EndpointType.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointType, GraphqlType, TypeDef};
///
/// let et = EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true);
/// ```
pub fn new(type_def: TypeDef, list: bool, required: bool) -> EndpointType {
EndpointType {
type_def,
list,
required,
}
}
/// True if the type associated with a custom GraphQL endpoint is a list of elements; false if
/// the type is a single value
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointType, GraphqlType, TypeDef};
///
/// let et = EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true);
///
/// assert_eq!(false, et.list());
/// ```
pub fn list(&self) -> bool {
self.list
}
/// True if the type associated with a custom GraphQL endpoint is required; false if it is
/// optional
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointType, GraphqlType, TypeDef};
///
/// let et = EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true);
///
/// assert_eq!(true, et.required());
/// ```
pub fn required(&self) -> bool {
self.required
}
/// Returns a [`TypeDef`] enumeration, describing whether the type is a GraphQL scalar type,
/// an existing type defined elsewhere in the configuration, or a new custom type.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointType, GraphqlType, TypeDef};
///
/// let et = EndpointType::new(TypeDef::Scalar(GraphqlType::Int), false, true);
///
/// assert_eq!(&TypeDef::Scalar(GraphqlType::Int), et.type_def());
/// ```
pub fn type_def(&self) -> &TypeDef {
&self.type_def
}
}
/// Enumeration representing Graphql scalar types
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::GraphqlType;
///
/// let gt = GraphqlType::Int;
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum GraphqlType {
/// GraphQL integer
Int,
/// GraphQL floating point number
Float,
/// GraphQL string value
String,
/// GraphQL boolean value
Boolean,
}
/// Configuration item for a property on a GraphQL type, modeled as properties on a graph node.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Property, UsesFilter};
///
/// let p = Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true,
/// false, None, None);
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Property {
/// Name of the property
name: String,
/// Filter of properties that determines whether the property will be used in creation,
/// matching/query, update, and output/shape portions of the schema
#[serde(default)]
uses: UsesFilter,
/// The name of the type of the property (e.g. String)
#[serde(rename = "type")]
type_name: String,
/// True if this property is required to be present on this type; false if the property is
/// optional
#[serde(default = "get_true")]
required: bool,
/// True if this property is a list
#[serde(default = "get_false")]
list: bool,
/// The name of the resolver function to be called when querying for the value of this prop.
/// If this field is None, the prop resolves the scalar value from the database.
#[serde(default = "get_none")]
resolver: Option<String>,
/// The name of the validator function to be called when creating or modifying the value of
/// this prop. If this field is None, the prop resolves the scalar value from the database.
#[serde(default = "get_none")]
validator: Option<String>,
}
impl Property {
/// Creates a new Property struct.
///
/// # Arguments
///
/// * a String for the name of the property
/// * a String for the type of the property
/// * a boolean that, if true, indicatees that the property is mandatory, and if false, that
/// the property is optional
/// * a boolean that, if true, indicates that the property is a list of scalers, and if false,
/// that the property is a single value
/// * an optional string providing the name of a resolver, if the property is a dynamic
/// property with a custom resolver, and
/// * an optional string providing the name of a custom validator
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Property, UsesFilter};
///
/// let p = Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true,
/// false, None, None);
/// ```
pub fn new(
name: String,
uses: UsesFilter,
type_name: String,
required: bool,
list: bool,
resolver: Option<String>,
validator: Option<String>,
) -> Property {
Property {
name,
uses,
type_name,
required,
list,
resolver,
validator,
}
}
/// Returns a boolean that if true, indicates that this property contains a list of scalar
/// values, and if false, indicates that the property contains only one value (or potentially
/// zero values if required is also false).
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("name".to_string(), UsesFiter::all(), "String".to_string(),
/// true, false, None, None);
///
/// assert!(!p.list());
pub fn list(&self) -> bool {
self.list
}
/// Returns the name of the property
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("propname".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None);
///
/// assert_eq!("propname", p.name());
pub fn name(&self) -> &str {
&self.name
}
/// Returns the filter describing how a property is to be used
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("propname".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None);
///
/// assert_eq!(UsesFilter::all(), p.uses());
pub fn uses(&self) -> UsesFilter {
self.uses
}
/// Returns the optional name of the custom resolver associated with this property
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("propname".to_string(), "String".to_string(), true, false,
/// Some("CustomResolver".to_string()), None);
///
/// assert_eq!("CustomResolver", p.resolver().unwrap());
pub fn resolver(&self) -> Option<&String> {
self.resolver.as_ref()
}
/// Returns a boolean that if true, indicates that this property is mandatory, and if false,
/// that the property is not required, and may be absent.
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("name".to_string(), "String".to_string(), true, false, None, None);
///
/// assert!(p.required());
pub fn required(&self) -> bool {
self.required
}
/// Returns the name of the type of the property
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("propname".to_string(), "String".to_string(), true, false, None,
/// None);
///
/// assert_eq!("String", p.type_name());
pub fn type_name(&self) -> &str {
&self.type_name
}
/// Returns the optional name of the custom validator associated with this property
///
/// # Examples
///
/// # use warpgrapher::engine::config::Property;
///
/// let p = Property::new("propname".to_string(), "String".to_string(), true, false,
/// None, Some("CustomValidator".to_string()));
///
/// assert_eq!("CustomValidator", p.validator().unwrap());
pub fn validator(&self) -> Option<&String> {
self.validator.as_ref()
}
}
/// Configuration item for a relationship on a GraphQL type
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Relationship, EndpointsFilter};
///
/// let r = Relationship::new(
/// "teams".to_string(),
/// true,
/// vec!["User".to_string()],
/// vec![],
/// EndpointsFilter::all(),
/// None
/// );
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Relationship {
/// Name of the relationship
name: String,
/// True if its a multi-node relationship
#[serde(default = "get_false")]
list: bool,
/// List of possible dst nodes for the relationship. A single element
/// vector indicates a single-type rel and more than one element
/// indicates a multi-type relationship.
nodes: Vec<String>,
/// Properties of the relationship
#[serde(default)]
props: Vec<Property>,
/// Filter of endpoints that determines which CRUD endpoints will be
/// auto generated for the relationship
#[serde(default)]
endpoints: EndpointsFilter,
/// The name of the resolver function to be called when querying for the value of this prop.
/// If this field is None, the prop resolves the scalar value from the database.
#[serde(default = "get_none")]
resolver: Option<String>,
}
impl Relationship {
/// Creates a new Relationship struct.
///
/// # Arguments
///
/// * a String for the name of the relationship
/// * a boolean that, if true, indicates that the property is a list, and if false, that the
/// relationship is to a single node
/// * a list of possible destination node types for the relationship. A single element in the
/// list indicates that this is a single-type relationship, whereas more than one element
/// indicates a multi-type relationship.
/// * an [`EndpointsFilter`] struct indicating which of the standard operations (create, read,
/// update, and delete) should be generated for this relationship
/// * an optional string providing the name of a resolver, if the relationship is a dynamic
/// relationship with a custom resolver
///
/// [`EndpointsFilter`]: ./struct.EndpointsFilter.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("name".to_string(), false, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
/// ```
pub fn new(
name: String,
list: bool,
nodes: Vec<String>,
props: Vec<Property>,
endpoints: EndpointsFilter,
resolver: Option<String>,
) -> Relationship {
Relationship {
name,
list,
nodes,
props,
endpoints,
resolver,
}
}
/// Returns the [`EndpointsFilter`] struct that indicates which of the four basic Create, Read,
/// Update, and Delete (CRUD) operations Warpgrapher should auto-generate for this
/// relationship.
///
/// [`EndpointsFilter`]: ./struct.EndpointsFilter.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("name".to_string(), false, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert_eq!(&EndpointsFilter::all(), r.endpoints());
/// ```
pub fn endpoints(&self) -> &EndpointsFilter {
&self.endpoints
}
/// Returns true if the relationship is a list, indicating a one-to-many (or many-to-many)
/// relationship. Returns false if the node can only have one relationship of this type.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("name".to_string(), true, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert!(r.list());
/// ```
pub fn list(&self) -> bool {
self.list
}
/// Returns the name of the relationship
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("RelName".to_string(), true, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert_eq!("RelName", r.name());
/// ```
pub fn name(&self) -> &str {
&self.name
}
/// Returns an iterator over the names of the Warpgrapher [`Type`] definitions that are
/// possible destination nodes for this relationship.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("RelName".to_string(), true, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert_eq!(1, r.nodes().count());
/// assert_eq!("User", r.nodes().next().expect("Expected an element"));
/// ```
pub fn nodes(&self) -> Iter<String> {
self.nodes.iter()
}
/// Returns a slice of [`Property`] references for properties associated with the relationship.
///
/// [`Property`]: ./struct.Property.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("RelName".to_string(), true, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert_eq!(0, r.props_as_slice().len())
/// ```
pub fn props_as_slice(&self) -> &[Property] {
&self.props
}
/// Returns an option for a string containing the name of a custom resolver, if this
/// relationship is resolved by a custom resolver instead of an auto-generated read resolver.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Relationship};
///
/// let r = Relationship::new("RelName".to_string(), true, vec!["User".to_string()], vec![],
/// EndpointsFilter::all(), None);
///
/// assert!(r.resolver().is_none())
/// ```
pub fn resolver(&self) -> Option<&String> {
self.resolver.as_ref()
}
}
/// Configuration item for a GraphQL type. In back-end storage, the type is recorded in a label
/// attached to the graph node.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None),
/// Property::new("role".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None)),
/// vec!(),
/// EndpointsFilter::all()
/// );
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Type {
/// Name of this GraphQL type, also used as the label for nodes
name: String,
/// Vector of properties on this type
#[serde(default)]
props: Vec<Property>,
/// Vector of relationships on this type
#[serde(default)]
rels: Vec<Relationship>,
/// Filter of endpoints that determines which CRUD endpoints will be
/// auto generated for the relationship
#[serde(default)]
endpoints: EndpointsFilter,
}
impl Type {
/// Creates a new Type struct.
///
/// # Arguments
///
/// * name - the name of the type, which will be recorded as the label on a node in the graph
/// back-end
/// * props - a vector of [`Property`] structs describing the properties on the node
/// * rels - a vector of [`Relationship`] structs describing the outgoing relationships from
/// this node type
/// * endpoints - an [`EndpointsFilter`] struct describing which CRUD operations should be
/// generated automatically for this node type.
///
/// [`EndpointsFilter`]: ./struct.EndpointsFilter.html
/// [`Property`]: ./struct.Property.html
/// [`Type`]: ./struct.Type.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None),
/// Property::new("role".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None)),
/// vec!(),
/// EndpointsFilter::all()
/// );
/// ```
pub fn new(
name: String,
props: Vec<Property>,
rels: Vec<Relationship>,
endpoints: EndpointsFilter,
) -> Type {
Type {
name,
props,
rels,
endpoints,
}
}
/// Returns the name of the type. This type name is used as the label on nodes of this type in
/// the graph database storage back-end.
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None),
/// Property::new("role".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None)),
/// vec!(),
/// EndpointsFilter::all()
/// );
///
/// assert_eq!("User", t.name());
/// ```
pub fn name(&self) -> &str {
&self.name
}
/// Returns the [`EndpointsFilter`] struct associate with this type, determining which CRUD
/// operations should be auto-generated for this node type.
///
/// [`EndpointsFilter`]: ./struct.EndpointsFilter.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None),
/// Property::new("role".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None)),
/// vec!(),
/// EndpointsFilter::all()
/// );
///
/// assert_eq!(&EndpointsFilter::all(), t.endpoints());
/// ```
pub fn endpoints(&self) -> &EndpointsFilter {
&self.endpoints
}
/// Returns an iterator over the [`Property`] structs defining properties on this node type.
///
/// [`Property`]: ./struct.Property.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(),
/// true, false, None, None)), vec!(), EndpointsFilter::all());
///
/// assert_eq!("name", t.props().next().expect("Expected property").name());
/// ```
pub fn props(&self) -> Iter<Property> {
self.props.iter()
}
pub fn mut_props(&mut self) -> &mut Vec<Property> {
&mut self.props
}
/// Returns a slice of the [`Property`] structs defining properties on this node type.
///
/// [`Property`]: ./struct.Property.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Type, Property, EndpointsFilter, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None)),
/// vec!(),
/// EndpointsFilter::all()
/// );
///
/// assert_eq!("name", t.props_as_slice()[0].name());
/// ```
pub fn props_as_slice(&self) -> &[Property] {
&self.props
}
/// Returns an iterator over the [`Relationship`] structs defining relationships originating
/// from this node type.
///
/// [`Relationship`]: ./struct.Relationship.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{EndpointsFilter, Property, Relationship,
/// # Type, UsesFilter};
///
/// let t = Type::new(
/// "User".to_string(),
/// vec!(Property::new("name".to_string(), UsesFilter::all(), "String".to_string(), true,
/// false, None, None)),
/// vec!(Relationship::new("rel_name".to_string(), false, vec!("Role".to_string()), vec!(
/// Property::new("rel_prop".to_string(), UsesFilter::all(), "String".to_string(), true, false, None, None)
/// ), EndpointsFilter::all(), None)),
/// EndpointsFilter::all()
/// );
///
/// assert_eq!("rel_name", t.rels().next().expect("Expected relationship").name());
/// ```
pub fn rels(&self) -> Iter<Relationship> {
self.rels.iter()
}
}
impl TryFrom<&str> for Type {
type Error = Error;
/// Creates a new Type struct from a yaml-formatted string.
///
/// # Errors
///
/// Returns an [`Error`] variant [`YamlDeserializationFailed`] if the yaml-formatted
/// string is improperly formatted.
///
/// [`YamlDeserializationFailed`]: ../../error/enum.Error.html#variant.YamlDeserializationFailed
/// [`Error`]: ../../error/enum.Error.html
///
/// # Examples
///
/// ```rust
/// use warpgrapher::engine::config::{Type};
///
/// use std::convert::TryFrom;
/// let t = Type::try_from("
/// name: User
/// props:
/// - name: name
/// type: String
/// ").unwrap();
/// ```
fn try_from(yaml: &str) -> Result<Type, Error> {
serde_yaml::from_str(yaml).map_err(|e| Error::YamlDeserializationFailed { source: e })
}
}
/// Enumeration representing the definition of a type used as the optional input or the output for
/// a custom GraphQL endpooint
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{GraphqlType, TypeDef};
///
/// let td = TypeDef::Scalar(GraphqlType::Int);
/// ```
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(untagged)]
pub enum TypeDef {
/// The type is a GraphQL scalar type. The tuple value indicates the scalar type to use.
Scalar(GraphqlType),
/// The type is an existing type, already defined by the configuration file or auto-generated
/// by Warpgrapher. The tuple value is the name of the existing type to use.
Existing(String),
/// The type is a new custom type, defined for this custom endpoint. The tuple value is a
/// [`Type`] struct defining the custom type.alloc
///
/// [`Type`]: ./struct.Type.html
Custom(Type),
}
/// Configuration item for property usage filters. This allows configuration to control which of the
/// basic creation input, query input, update input, and output uses are auto-generated for a
/// [`Property`]. If a filter boolean is set to true, the use of the property is generated. False
/// indicates that the property should be omitted from use. For example, one might set the output
/// use to true and all other uses to false for calculated value that is derived upon request but
/// would never appear in the creation or update of a node. If all are set to false, the property
/// is hidden, meaning that it can be read from and written to the database but does not appear in
/// the client-facing GraphQL schema.
///
/// [`Property`]: ./struct.Property.html
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::new(true, true, true, true);
/// ```
#[derive(Copy, Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct UsesFilter {
/// True if the property should be included in the NodeCreateMutationInput portion of the schema
#[serde(default = "get_true")]
create: bool,
/// True if the property should be included in the NodeQueryInput portion of the schema
#[serde(default = "get_true")]
query: bool,
/// True if the property should be included in the NodeUpdateMutationInput portion of the schema
#[serde(default = "get_true")]
update: bool,
/// True if the property should be included in output shape portion of the schema
#[serde(default = "get_true")]
output: bool,
}
impl UsesFilter {
/// Creates a new filter with the option to configure uses of a property
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::new(false, false, false, true);
/// ```
pub fn new(create: bool, query: bool, update: bool, output: bool) -> UsesFilter {
UsesFilter {
create,
query,
update,
output,
}
}
/// Creates a new filter with all uses -- create, query, update, and output
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::all();
/// ```
pub fn all() -> UsesFilter {
UsesFilter {
create: true,
query: true,
update: true,
output: true,
}
}
/// Returns true if Warpgrapher should use the property in the NodeCreateMutationInput
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::all();
/// assert_eq!(true, uf.create());
/// ```
pub fn create(self) -> bool {
self.create
}
/// Returns true if Warpgrapher should use the property in the NodeQueryInput
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::all();
/// assert_eq!(true, uf.query());
/// ```
pub fn query(self) -> bool {
self.query
}
/// Creates a new filter with no uses of a property, hiding it from the GraphQL schema
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::none();
/// ```
pub fn none() -> UsesFilter {
UsesFilter {
create: false,
query: false,
update: false,
output: false,
}
}
/// Returns true if Warpgrapher should generate a property in the output shape of a node
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::all();
/// assert_eq!(true, uf.output());
/// ```
pub fn output(self) -> bool {
self.output
}
/// Returns true if Warpgrapher should use the property in the NodeUpdateInput
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::UsesFilter;
///
/// let uf = UsesFilter::all();
/// assert_eq!(true, uf.update());
/// ```
pub fn update(self) -> bool {
self.update
}
}
impl Default for UsesFilter {
fn default() -> UsesFilter {
UsesFilter {
create: true,
query: true,
update: true,
output: true,
}
}
}
/// Creates a combined [`Configuration`] data structure from multiple [`Configuration`] structs.
/// All [`Configuration`] structs must be the same version.
///
/// Returns a `Result<Configuration, Error>` with a single [`Configuration`] struct, or if the
/// versions across all `Configuration`s do not match, a [`ConfigVersionMismatched`] error.
///
/// [`Configuration`]: struct.Configuration.html
/// [`ConfigVersionMismatched`]: ../enum.Error.html#variant.ConfigVersionMismatched
///
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::engine::config::{Configuration, compose};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut config_vec: Vec<Configuration> = Vec::new();
/// let config = compose(config_vec)?;
/// # Ok(())
/// # }
/// ```
pub fn compose(configs: Vec<Configuration>) -> Result<Configuration, Error> {
let mut version: Option<i32> = None;
let mut model: Vec<Type> = Vec::new();
let mut endpoints: Vec<Endpoint> = Vec::new();
configs
.into_iter()
.map(|mut c| {
match version {
None => version = Some(c.version()),
Some(v) => {
if v != c.version {
return Err(Error::ConfigVersionMismatched {
expected: v,
found: c.version,
});
}
}
}
model.append(&mut c.model);
endpoints.append(&mut c.endpoints);
Ok(())
})
.collect::<Result<Vec<_>, Error>>()?;
// There will be no version number if the vector of Configurations is empty, in which case
// we might as well use the latest version
Ok(Configuration::new(
version.unwrap_or(LATEST_CONFIG_VERSION),
model,
endpoints,
))
}
#[cfg(test)]
pub(crate) fn mock_project_config() -> Configuration {
Configuration::new(1, vec![mock_project_type()], vec![])
}
#[cfg(test)]
pub(crate) fn mock_project_type() -> Type {
Type::new(
"Project".to_string(),
vec![
Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
),
Property::new(
"tags".to_string(),
UsesFilter::all(),
"String".to_string(),
false,
true,
None,
None,
),
Property::new(
"public".to_string(),
UsesFilter::all(),
"Boolean".to_string(),
true,
false,
None,
None,
),
],
vec![
Relationship::new(
"owner".to_string(),
false,
vec!["User".to_string()],
vec![Property::new(
"since".to_string(),
UsesFilter::all(),
"String".to_string(),
false,
false,
None,
None,
)],
EndpointsFilter::all(),
None,
),
Relationship::new(
"board".to_string(),
false,
vec!["ScrumBoard".to_string(), "KanbanBoard".to_string()],
vec![],
EndpointsFilter::all(),
None,
),
Relationship::new(
"commits".to_string(),
true,
vec!["Commit".to_string()],
vec![],
EndpointsFilter::all(),
None,
),
Relationship::new(
"issues".to_string(),
true,
vec!["Feature".to_string(), "Bug".to_string()],
vec![],
EndpointsFilter::all(),
None,
),
],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_user_type() -> Type {
Type::new(
"User".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_kanbanboard_type() -> Type {
Type::new(
"KanbanBoard".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_scrumboard_type() -> Type {
Type::new(
"ScrumBoard".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_feature_type() -> Type {
Type::new(
"Feature".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_bug_type() -> Type {
Type::new(
"Bug".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
fn mock_commit_type() -> Type {
Type::new(
"Commit".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)
}
#[cfg(test)]
pub(crate) fn mock_endpoint_one() -> Endpoint {
// RegisterUsers(input: [UserCreateMutationInput]): [User]
Endpoint::new(
"RegisterUsers".to_string(),
EndpointClass::Mutation,
Some(EndpointType::new(
TypeDef::Existing("UserCreateMutationInput".to_string()),
true,
true,
)),
EndpointType::new(TypeDef::Existing("User".to_string()), true, true),
)
}
#[cfg(test)]
pub(crate) fn mock_endpoint_two() -> Endpoint {
// DisableUser(input: UserQueryInput): User
Endpoint::new(
"DisableUser".to_string(),
EndpointClass::Mutation,
Some(EndpointType::new(
TypeDef::Existing("UserQueryInput".to_string()),
false,
true,
)),
EndpointType::new(TypeDef::Existing("User".to_string()), false, true),
)
}
#[cfg(test)]
pub(crate) fn mock_endpoint_three() -> Endpoint {
// ComputeBurndown(input: BurndownFilter): BurndownMetrics
Endpoint::new(
"ComputeBurndown".to_string(),
EndpointClass::Query,
Some(EndpointType::new(
TypeDef::Custom(Type::new(
"BurndownFilter".to_string(),
vec![Property::new(
"ticket_types".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)),
false,
false,
)),
EndpointType::new(
TypeDef::Custom(Type::new(
"BurndownMetrics".to_string(),
vec![Property::new(
"points".to_string(),
UsesFilter::all(),
"Int".to_string(),
false,
false,
None,
None,
)],
vec![],
EndpointsFilter::all(),
)),
false,
true,
),
)
}
#[cfg(test)]
pub(crate) fn mock_config() -> Configuration {
Configuration::new(
1,
vec![
mock_project_type(),
mock_user_type(),
mock_kanbanboard_type(),
mock_scrumboard_type(),
mock_feature_type(),
mock_bug_type(),
mock_commit_type(),
],
vec![
mock_endpoint_one(),
mock_endpoint_two(),
mock_endpoint_three(),
],
)
}
#[cfg(test)]
pub(crate) fn mock_endpoints_filter() -> Configuration {
Configuration::new(
1,
vec![Type::new(
"User".to_string(),
vec![Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
)],
vec![],
EndpointsFilter::new(false, true, false, false),
)],
Vec::new(),
)
}
#[cfg(test)]
mod tests {
use super::{
compose, Configuration, Endpoint, EndpointType, EndpointsFilter, Property, Relationship,
Type, UsesFilter,
};
use crate::Error;
use std::convert::TryInto;
use std::fs::File;
/// There's not really much of a "test" per se, in this first unit test.
/// This is the example used in the book/src/warpgrapher/config.md file, so
/// having it here is a forcing function to make sure we catch any changes
/// in interface that would change this code and update the book to match.
#[test]
fn warpgrapher_book_config() {
let config = Configuration::new(
1,
vec![
// User
Type::new(
"User".to_string(),
vec![
Property::new(
"username".to_string(),
UsesFilter::all(),
"String".to_string(),
false,
false,
None,
None,
),
Property::new(
"email".to_string(),
UsesFilter::all(),
"String".to_string(),
false,
false,
None,
None,
),
],
Vec::new(),
EndpointsFilter::all(),
),
// Team
Type::new(
"Team".to_string(),
vec![Property::new(
"teamname".to_string(),
UsesFilter::all(),
"String".to_string(),
false,
false,
None,
None,
)],
vec![Relationship::new(
"members".to_string(),
true,
vec!["User".to_string()],
Vec::new(),
EndpointsFilter::default(),
None,
)],
EndpointsFilter::all(),
),
],
vec![],
);
assert!(!config.model.is_empty());
}
/// Passes if a new Configuration is created
#[test]
fn new_warpgrapher_config() {
let c = Configuration::new(1, Vec::new(), Vec::new());
assert!(c.version == 1);
assert!(c.model.is_empty());
}
// Passes if a Property is created and prints correctly
#[test]
fn new_property() {
let p = Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
);
assert!(p.name == "name");
assert!(p.type_name == "String");
}
/// Passes if a Type is created
#[test]
fn new_node_type() {
let t = Type::new(
"User".to_string(),
vec![
Property::new(
"name".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
),
Property::new(
"role".to_string(),
UsesFilter::all(),
"String".to_string(),
true,
false,
None,
None,
),
],
vec![],
EndpointsFilter::all(),
);
assert!(t.name == "User");
assert!(t.props.get(0).unwrap().name == "name");
assert!(t.props.get(1).unwrap().name == "role");
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn test_validate() {
//Test valid config
let valid_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_ok.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
assert!(valid_config.validate().is_ok());
//Test composed config
let mut config_vec: Vec<Configuration> = Vec::new();
let valid_config_0: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_0.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
let valid_config_1: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_1.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
let valid_config_2: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_2.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
config_vec.push(valid_config_0);
config_vec.push(valid_config_1);
config_vec.push(valid_config_2);
let composed_config: Configuration = match compose(config_vec) {
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
assert!(composed_config.validate().is_ok());
//Test duplicate Type
let duplicate_type_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_duplicate_type.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match duplicate_type_config.validate() {
Err(Error::ConfigItemDuplicated { type_name: _ }) => (),
_ => panic!(),
}
//Test duplicate Endpoint type
let duplicate_endpoint_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_duplicate_endpoint.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match duplicate_endpoint_config.validate() {
Err(Error::ConfigItemDuplicated { type_name: _ }) => (),
_ => panic!(),
}
let duplicate_derived_name_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_duplicate_derived_name.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match duplicate_derived_name_config.validate() {
Err(Error::ConfigItemDuplicated { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_prop_name_id_test() {
let node_prop_name_id_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_node_prop_name_id.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match node_prop_name_id_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
let rel_prop_name_id_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_rel_prop_name_id.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match rel_prop_name_id_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_prop_name_src_test() {
let rel_prop_name_src_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_rel_prop_name_src.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match rel_prop_name_src_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_prop_name_dst_test() {
let rel_prop_name_dst_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_rel_prop_name_dst.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match rel_prop_name_dst_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_scalar_name_int_test() {
//Test Scalar Type Name: Int
let scalar_type_name_int_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_type_name_int.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_type_name_int_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Input Name: Int
let scalar_endpoint_input_type_name_int_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_input_type_name_int.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_input_type_name_int_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Output Name: Int
let scalar_endpoint_output_type_name_int_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_output_type_name_int.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_output_type_name_int_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_scalar_name_float_test() {
//Test Scalar Type Name: Float
let scalar_type_name_float_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_type_name_float.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_type_name_float_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Input Name: Float
let scalar_endpoint_input_type_name_float_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_input_type_name_float.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_input_type_name_float_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Output Name: Float
let scalar_endpoint_output_type_name_float_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_output_type_name_float.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_output_type_name_float_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_scalar_name_string_test() {
//Test Scalar Type Name: String
let scalar_type_name_string_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_type_name_string.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_type_name_string_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Input Name: String
let scalar_endpoint_input_type_name_string_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_input_type_name_string.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_input_type_name_string_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Output Name: String
let scalar_endpoint_output_type_name_string_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_output_type_name_string.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_output_type_name_string_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_scalar_name_id_test() {
//Test Scalar Type Name: ID
let scalar_type_name_id_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_type_name_id.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_type_name_id_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Input Name: id
let scalar_endpoint_input_type_name_id_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_input_type_name_id.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_input_type_name_id_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Output Name: ID
let scalar_endpoint_output_type_name_id_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_output_type_name_id.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_output_type_name_id_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn config_scalar_name_boolean_test() {
//Test Scalar Type Name: Boolean
let scalar_type_name_boolean_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_type_name_boolean.yml",
)
.expect("Coudln't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_type_name_boolean_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Input Name: Boolean
let scalar_endpoint_input_type_name_boolean_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_input_type_name_boolean.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_input_type_name_boolean_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
//Test Scalar Endpoint Output Name: Boolean
let scalar_endpoint_output_type_name_boolean_config: Configuration = match File::open(
"tests/fixtures/config-validation/test_config_scalar_endpoint_output_type_name_boolean.yml",
)
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
match scalar_endpoint_output_type_name_boolean_config.validate() {
Err(Error::ConfigItemReserved { type_name: _ }) => (),
_ => panic!(),
}
}
#[allow(clippy::match_wild_err_arm)]
#[test]
fn test_compose() {
assert!(TryInto::<Configuration>::try_into(
File::open("tests/fixtures/config-validation/test_config_err.yml")
.expect("Couldn't open file")
)
.is_err());
assert!(TryInto::<Configuration>::try_into(
File::open("tests/fixtures/config-validation/test_config_ok.yml")
.expect("Couldn't open file")
)
.is_ok());
let mut config_vec: Vec<Configuration> = Vec::new();
assert!(compose(config_vec.clone()).is_ok());
let valid_config_0: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_0.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
let valid_config_1: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_1.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
let valid_config_2: Configuration =
match File::open("tests/fixtures/config-validation/test_config_compose_2.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
let mismatch_version_config: Configuration =
match File::open("tests/fixtures/config-validation/test_config_with_version_100.yml")
.expect("Couldn't open file")
.try_into()
{
Err(e) => panic!("{}", e),
Ok(wgc) => wgc,
};
config_vec.push(valid_config_0);
config_vec.push(valid_config_1);
config_vec.push(valid_config_2);
assert!(compose(config_vec.clone()).is_ok());
config_vec.push(mismatch_version_config);
assert!(compose(config_vec).is_err());
}
/// Passes if Configuration implements the Send trait
#[test]
fn test_config_send() {
fn assert_send<T: Send>() {}
assert_send::<Configuration>();
}
/// Passes if Configuration implements the Sync trait
#[test]
fn test_config_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Configuration>();
}
/// Passes if Endpoint implements the Send trait
#[test]
fn test_endpoint_send() {
fn assert_send<T: Send>() {}
assert_send::<Endpoint>();
}
/// Passes if Endpoint implements the Sync trait
#[test]
fn test_endpoint_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Endpoint>();
}
/// Passes if EndpointsFilter implements the Send trait
#[test]
fn test_endpoints_filter_send() {
fn assert_send<T: Send>() {}
assert_send::<EndpointsFilter>();
}
/// Passes if EndpointsFilter implements the Sync trait
#[test]
fn test_endpoints_filter_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<EndpointsFilter>();
}
/// Passes if EndpointType implements the Send trait
#[test]
fn test_endpoints_type_send() {
fn assert_send<T: Send>() {}
assert_send::<EndpointType>();
}
/// Passes if EndpointType implements the Sync trait
#[test]
fn test_endpoints_type_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<EndpointType>();
}
/// Passes if Property implements the Send trait
#[test]
fn test_property_send() {
fn assert_send<T: Send>() {}
assert_send::<Property>();
}
/// Passes if Property implements the Sync trait
#[test]
fn test_property_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Property>();
}
/// Passes if Relationship implements the Send trait
#[test]
fn test_relationship_send() {
fn assert_send<T: Send>() {}
assert_send::<Relationship>();
}
/// Passes if Relationship implements the Sync trait
#[test]
fn test_relationship_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Relationship>();
}
/// Passes if Type implements the Send trait
#[test]
fn test_type_send() {
fn assert_send<T: Send>() {}
assert_send::<Type>();
}
/// Passes if Type implements the Sync trait
#[test]
fn test_type_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Type>();
}
}