Welcome to mirror list, hosted at ThFree Co, Russian Federation.

just_gtfs.h « just_gtfs « include - github.com/mapsme/just_gtfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c13f22f59a22304fe4ba9d7dc406933d3f1609a0 (plain)
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
#pragma once

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <exception>
#include <filesystem>
#include <fstream>
#include <functional>
#include <istream>
#include <map>
#include <optional>
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace gtfs
{
// Helper classes ----------------------------------------------------------------------------------
struct InvalidFieldFormat : public std::exception
{
public:
  explicit InvalidFieldFormat(const std::string & msg) : message(prefix + msg) {}

  const char * what() const noexcept
  {
    return message.c_str();
  }

private:
  const std::string prefix = "Invalid GTFS field format. ";
  std::string message;
};

enum ResultCode
{
  OK,
  END_OF_FILE,
  ERROR_INVALID_GTFS_PATH,
  ERROR_FILE_ABSENT,
  ERROR_REQUIRED_FIELD_ABSENT,
  ERROR_INVALID_FIELD_FORMAT
};

using Message = std::string;

struct Result
{
  ResultCode code = OK;
  Message message;

  bool operator==(ResultCode result_code) const { return code == result_code; }
  bool operator!=(ResultCode result_code) const { return !(code == result_code); }
};

// Csv parser  -------------------------------------------------------------------------------------
class CsvParser
{
public:
  CsvParser() = default;
  inline explicit CsvParser(const std::string & gtfs_directory);

  inline Result read_header(const std::string & csv_filename);
  inline Result read_row(std::map<std::string, std::string> & obj);

  inline static std::vector<std::string> split_record(const std::string & record,
                                                      bool is_header = false);

private:
  std::vector<std::string> field_sequence;
  std::filesystem::path gtfs_path;
  std::ifstream csv_stream;
  static const char delimiter = ',';
};

inline CsvParser::CsvParser(const std::string & gtfs_directory) : gtfs_path(gtfs_directory) {}

inline void trim_spaces(std::string & token)
{
  while (!token.empty() && token.back() == ' ')
    token.pop_back();
}

inline std::vector<std::string> CsvParser::split_record(const std::string & record, bool is_header)
{
  std::string const delims = "\r\t";
  size_t start_index = 0;
  if (is_header)
  {
    // ignore UTF-8 BOM prefix:
    if (record.size() > 2 && record[0] == '\xef' && record[1] == '\xbb' && record[2] == '\xbf')
      start_index = 3;
  }
  std::vector<std::string> fields;
  fields.reserve(20);

  std::string token;
  token.reserve(record.size());

  size_t token_start_index = start_index;
  bool is_inside_quotes = false;

  for (size_t i = start_index; i < record.size(); ++i)
  {
    if (record[i] == '"')
    {
      is_inside_quotes = !is_inside_quotes;
      continue;
    }

    if (record[i] == ' ')
    {
      if (token_start_index == i)
        token_start_index = i + 1;
      else
        token += record[i];
      continue;
    }

    if (record[i] == delimiter)
    {
      if (is_inside_quotes)
      {
        token += record[i];
        continue;
      }
      token_start_index = i + 1;
      trim_spaces(token);
      fields.push_back(token);
      token.erase();
      continue;
    }

    if (delims.find(record[i]) == std::string::npos)
      token += record[i];
  }
  trim_spaces(token);
  fields.push_back(token);
  return fields;
}

inline Result CsvParser::read_header(const std::string & csv_filename)
{
  if (csv_stream.is_open())
    csv_stream.close();

  csv_stream.open(gtfs_path / csv_filename);
  if (!csv_stream.is_open())
    return {ResultCode::ERROR_FILE_ABSENT, {}};

  std::string header;
  if (!getline(csv_stream, header) || header.empty())
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, {}};

  field_sequence = split_record(header, true);
  return {ResultCode::OK, {}};
}

inline Result CsvParser::read_row(std::map<std::string, std::string> & obj)
{
  std::string row;
  if (!getline(csv_stream, row))
  {
    obj = {};
    return {ResultCode::END_OF_FILE, {}};
  }

  if (row == "\r")
  {
    obj = {};
    return {ResultCode::OK, {}};
  }

  std::vector<std::string> fields_values = split_record(row);

  // Different count of fields in row and in the header of csv.
  // Typical approach to skip not required fields.
  if (fields_values.size() != field_sequence.size())
    obj = {};

  for (size_t i = 0; i < field_sequence.size(); ++i)
    obj[field_sequence[i]] = fields_values[i];

  return {ResultCode::OK, {}};
}

// Custom types for GTFS fields --------------------------------------------------------------------
// Id of GTFS entity, a sequence of any UTF-8 characters. Used as type for ID GTFS fields.
using Id = std::string;
// A string of UTF-8 characters. Used as type for Text GTFS fields.
using Text = std::string;

// Time in GTFS is in the HH:MM:SS format (H:MM:SS is also accepted)
// Time within a service day can be above 24:00:00, e.g. 28:41:30
class Time
{
public:
  inline Time() = default;
  inline explicit Time(const std::string & raw_time_str);
  inline Time(uint16_t hours, uint16_t minutes, uint16_t seconds);
  inline bool is_provided() const;
  inline size_t get_total_seconds() const;
  inline std::tuple<uint16_t, uint16_t, uint16_t> get_hh_mm_ss() const;
  inline std::string get_raw_time() const;
  inline bool limit_hours_to_24max();

private:
  inline void set_total_seconds();
  inline void set_raw_time();
  bool time_is_provided = false;
  std::string raw_time;
  size_t total_seconds = 0;
  uint16_t hh = 0;
  uint16_t mm = 0;
  uint16_t ss = 0;
};

inline bool operator==(const Time & lhs, const Time & rhs)
{
  return lhs.get_hh_mm_ss() == rhs.get_hh_mm_ss() && lhs.is_provided() == rhs.is_provided();
}

inline bool Time::limit_hours_to_24max()
{
  if (hh < 24)
    return false;

  hh = hh % 24;
  set_total_seconds();
  set_raw_time();
  return true;
}

inline void Time::set_total_seconds()
{
  total_seconds = hh * 60 * 60 + mm * 60 + ss;
}

inline std::string append_leading_zero(const std::string & s, bool check = true)
{
  if (check && s.size() > 2)
    throw InvalidFieldFormat("The string for appending zero is too long: " + s);

  if (s.size() == 2)
    return s;
  return "0" + s;
}

inline void Time::set_raw_time()
{
  const std::string hh_str = append_leading_zero(std::to_string(hh), false);
  const std::string mm_str = append_leading_zero(std::to_string(mm));
  const std::string ss_str = append_leading_zero(std::to_string(ss));

  raw_time = hh_str + ":" + mm_str + ":" + ss_str;
}

// Time in the HH:MM:SS format (H:MM:SS is also accepted). Used as type for Time GTFS fields.
inline Time::Time(const std::string & raw_time_str) : raw_time(raw_time_str)
{
  if (raw_time_str.empty())
    return;

  const size_t len = raw_time.size();
  if (!(len == 7 || len == 8) || (raw_time[len - 3] != ':' && raw_time[len - 6] != ':'))
    throw InvalidFieldFormat("Time is not in [H]H:MM:SS format: " + raw_time_str);

  hh = static_cast<uint16_t>(std::stoi(raw_time.substr(0, len - 6)));
  mm = static_cast<uint16_t>(std::stoi(raw_time.substr(len - 5, 2)));
  ss = static_cast<uint16_t>(std::stoi(raw_time.substr(len - 2)));

  if (mm > 60 || ss > 60)
    throw InvalidFieldFormat("Time minutes/seconds wrong value: " + std::to_string(mm) +
                             " minutes, " + std::to_string(ss) + " seconds");

  set_total_seconds();
  time_is_provided = true;
}

inline Time::Time(uint16_t hours, uint16_t minutes, uint16_t seconds)
    : hh(hours), mm(minutes), ss(seconds)
{
  if (mm > 60 || ss > 60)
    throw InvalidFieldFormat("Time is out of range: " + std::to_string(mm) + "minutes " +
                             std::to_string(ss) + "seconds");

  set_total_seconds();
  set_raw_time();
  time_is_provided = true;
}

inline bool Time::is_provided() const { return time_is_provided; }

inline size_t Time::get_total_seconds() const { return total_seconds; }

inline std::tuple<uint16_t, uint16_t, uint16_t> Time::get_hh_mm_ss() const { return {hh, mm, ss}; }

inline std::string Time::get_raw_time() const { return raw_time; }

// Service day in the YYYYMMDD format.
class Date
{
public:
  inline Date() = default;
  inline Date(uint16_t year, uint16_t month, uint16_t day);
  inline explicit Date(const std::string & raw_date_str);
  inline bool is_provided() const;
  inline std::tuple<uint16_t, uint16_t, uint16_t> get_yyyy_mm_dd() const;
  inline std::string get_raw_date() const;

private:
  inline void check_valid() const;

  std::string raw_date;
  uint16_t yyyy = 0;
  uint16_t mm = 0;
  uint16_t dd = 0;
  bool date_is_provided = false;
};

inline bool operator==(const Date & lhs, const Date & rhs)
{
  return lhs.get_yyyy_mm_dd() == rhs.get_yyyy_mm_dd() && lhs.is_provided() == rhs.is_provided();
}

inline void Date::check_valid() const
{
  if (yyyy < 1000 || yyyy > 9999 || mm < 1 || mm > 12 || dd < 1 || dd > 31)
    throw InvalidFieldFormat("Date check failed: out of range. " + std::to_string(yyyy) +
                             " year, " + std::to_string(mm) + " month, " + std::to_string(dd) +
                             " day");

  if (mm == 2 && dd > 28)
  {
    // The year is not leap. Days count should be 28.
    if (yyyy % 4 != 0 || (yyyy % 100 == 0 && yyyy % 400 != 0))
      throw InvalidFieldFormat("Invalid days count in February of non-leap year: " +
                               std::to_string(dd) + " year" + std::to_string(yyyy));

    // The year is leap. Days count should be 29.
    if (dd > 29)
      throw InvalidFieldFormat("Invalid days count in February of leap year: " +
                               std::to_string(dd) + " year" + std::to_string(yyyy));
  }

  if (dd > 30 && (mm == 4 || mm == 6 || mm == 9 || mm == 11))
    throw InvalidFieldFormat("Invalid days count in month: " + std::to_string(dd) + " days in " +
                             std::to_string(mm));
}

inline Date::Date(uint16_t year, uint16_t month, uint16_t day) : yyyy(year), mm(month), dd(day)
{
  check_valid();
  const std::string mm_str = append_leading_zero(std::to_string(mm));
  const std::string dd_str = append_leading_zero(std::to_string(dd));

  raw_date = std::to_string(yyyy) + mm_str + dd_str;
  date_is_provided = true;
}

inline Date::Date(const std::string & raw_date_str) : raw_date(raw_date_str)
{
  if (raw_date.empty())
    return;

  if (raw_date.size() != 8)
    throw InvalidFieldFormat("Date is not in YYYY:MM::DD format: " + raw_date_str);

  yyyy = static_cast<uint16_t>(std::stoi(raw_date.substr(0, 4)));
  mm = static_cast<uint16_t>(std::stoi(raw_date.substr(4, 2)));
  dd = static_cast<uint16_t>(std::stoi(raw_date.substr(6, 2)));

  check_valid();

  date_is_provided = true;
}

inline bool Date::is_provided() const { return date_is_provided; }

inline std::tuple<uint16_t, uint16_t, uint16_t> Date::get_yyyy_mm_dd() const
{
  return {yyyy, mm, dd};
}

inline std::string Date::get_raw_date() const { return raw_date; }

// An ISO 4217 alphabetical currency code. Used as type for Currency Code GTFS fields.
using CurrencyCode = std::string;
// An IETF BCP 47 language code. Used as type for Language Code GTFS fields.
using LanguageCode = std::string;

// Helper enums for some GTFS fields ---------------------------------------------------------------
enum class StopLocationType
{
  StopOrPlatform = 0,
  Station = 1,
  EntranceExit = 2,
  GenericNode = 3,
  BoardingArea = 4
};

// The type of transportation used on a route.
enum class RouteType
{
  // GTFS route types
  Tram = 0,         // Tram, Streetcar, Light rail
  Subway = 1,       // Any underground rail system within a metropolitan area
  Rail = 2,         // Intercity or long-distance travel
  Bus = 3,          // Short- and long-distance bus routes
  Ferry = 4,        // Boat service
  CableTram = 5,    // Street-level rail cars where the cable runs beneath the vehicle
  AerialLift = 6,   // Aerial lift, suspended cable car (gondola lift, aerial tramway)
  Funicular = 7,    // Any rail system designed for steep inclines
  Trolleybus = 11,  // Electric buses that draw power from overhead wires using poles
  Monorail = 12,    // Railway in which the track consists of a single rail or a beam

  // Extended route types
  // https://developers.google.com/transit/gtfs/reference/extended-route-types
  RailwayService = 100,
  HighSpeedRailService = 101,
  LongDistanceTrains = 102,
  InterRegionalRailService = 103,
  CarTransportRailService = 104,
  SleeperRailService = 105,
  RegionalRailService = 106,
  TouristRailwayService = 107,
  RailShuttleWithinComplex = 108,
  SuburbanRailway = 109,
  ReplacementRailService = 110,
  SpecialRailService = 111,
  LorryTransportRailService = 112,
  AllRailServices = 113,
  CrossCountryRailService = 114,
  VehicleTransportRailService = 115,
  RackAndPinionRailway = 116,
  AdditionalRailService = 117,

  CoachService = 200,
  InternationalCoachService = 201,
  NationalCoachService = 202,
  ShuttleCoachService = 203,
  RegionalCoachService = 204,
  SpecialCoachService = 205,
  SightseeingCoachService = 206,
  TouristCoachService = 207,
  CommuterCoachService = 208,
  AllCoachServices = 209,

  UrbanRailwayService400 = 400,
  MetroService = 401,
  UndergroundService = 402,
  UrbanRailwayService403 = 403,
  AllUrbanRailwayServices = 404,
  Monorail405 = 405,

  BusService = 700,
  RegionalBusService = 701,
  ExpressBusService = 702,
  StoppingBusService = 703,
  LocalBusService = 704,
  NightBusService = 705,
  PostBusService = 706,
  SpecialNeedsBus = 707,
  MobilityBusService = 708,
  MobilityBusForRegisteredDisabled = 709,
  SightseeingBus = 710,
  ShuttleBus = 711,
  SchoolBus = 712,
  SchoolAndPublicServiceBus = 713,
  RailReplacementBusService = 714,
  DemandAndResponseBusService = 715,
  AllBusServices = 716,

  TrolleybusService = 800,

  TramService = 900,
  CityTramService = 901,
  LocalTramService = 902,
  RegionalTramService = 903,
  SightseeingTramService = 904,
  ShuttleTramService = 905,
  AllTramServices = 906,

  WaterTransportService = 1000,
  AirService = 1100,
  FerryService = 1200,
  AerialLiftService = 1300,
  FunicularService = 1400,
  TaxiService = 1500,
  CommunalTaxiService = 1501,
  WaterTaxiService = 1502,
  RailTaxiService = 1503,
  BikeTaxiService = 1504,
  LicensedTaxiService = 1505,
  PrivateHireServiceVehicle = 1506,
  AllTaxiServices = 1507,
  MiscellaneousService = 1700,
  HorseDrawnCarriage = 1702
};

enum class TripDirectionId
{
  DefaultDirection = 0,  // e.g. outbound
  OppositeDirection = 1  // e.g. inbound
};

enum class TripAccess
{
  NoInfo = 0,
  Yes = 1,
  No = 2
};

enum class StopTimeBoarding
{
  RegularlyScheduled = 0,
  No = 1,                   // Not available
  Phone = 2,                // Must phone agency to arrange
  CoordinateWithDriver = 3  // Must coordinate with driver to arrange
};

enum class StopTimePoint
{
  Approximate = 0,
  Exact = 1
};

enum class CalendarAvailability
{
  NotAvailable = 0,
  Available = 1
};

enum class CalendarDateException
{
  Added = 1,  // Service has been added for the specified date
  Removed = 2
};

enum class FarePayment
{
  OnBoard = 0,
  BeforeBoarding = 1  // Fare must be paid before boarding
};

enum class FareTransfers
{
  No = 0,  // No transfers permitted on this fare
  Once = 1,
  Twice = 2,
  Unlimited = 3
};

enum class FrequencyTripService
{
  FrequencyBased = 0,  // Frequency-based trips
  ScheduleBased = 1    // Schedule-based trips with the exact same headway throughout the day
};

enum class TransferType
{
  Recommended = 0,
  Timed = 1,
  MinimumTime = 2,
  NotPossible = 3
};

enum class PathwayMode
{
  Walkway = 1,
  Stairs = 2,
  MovingSidewalk = 3,  // Moving sidewalk/travelator
  Escalator = 4,
  Elevator = 5,
  FareGate = 6,  // Payment gate
  ExitGate = 7
};

enum class PathwayDirection
{
  Unidirectional = 0,
  Bidirectional = 1
};

enum class TranslationTable
{
  Agency = 0,
  Stops,
  Routes,
  Trips,
  StopTimes,
  FeedInfo
};

enum class AttributionRole
{
  No = 0,  // Organization doesn’t have this role
  Yes = 1  // Organization does have this role
};

// Structures representing GTFS entities -----------------------------------------------------------
// Required dataset file
struct Agency
{
  // Conditionally optional:
  Id agency_id;

  // Required:
  Text agency_name;
  Text agency_url;
  Text agency_timezone;

  // Optional:
  Text agency_lang;
  Text agency_phone;
  Text agency_fare_url;
  Text agency_email;
};

// Required dataset file
struct Stop
{
  // Required:
  Id stop_id;

  // Conditionally required:
  Text stop_name;

  bool coordinates_present = true;
  double stop_lat = 0.0;
  double stop_lon = 0.0;
  Id zone_id;
  Id parent_station;

  // Optional:
  Text stop_code;
  Text stop_desc;
  Text stop_url;
  StopLocationType location_type = StopLocationType::GenericNode;
  Text stop_timezone;
  Text wheelchair_boarding;
  Id level_id;
  Text platform_code;
};

// Required dataset file
struct Route
{
  // Required:
  Id route_id;
  RouteType route_type = RouteType::Tram;

  // Conditionally required:
  Id agency_id;
  Text route_short_name;
  Text route_long_name;

  // Optional
  Text route_desc;
  Text route_url;
  Text route_color;
  Text route_text_color;
  size_t route_sort_order = 0;  // Routes with smaller value values should be displayed first
};

// Required dataset file
struct Trip
{
  // Required:
  Id route_id;
  Id service_id;
  Id trip_id;

  // Optional:
  Text trip_headsign;
  Text trip_short_name;
  TripDirectionId direction_id = TripDirectionId::DefaultDirection;
  Id block_id;
  Id shape_id;
  TripAccess wheelchair_accessible = TripAccess::NoInfo;
  TripAccess bikes_allowed = TripAccess::NoInfo;
};

// Required dataset file
struct StopTime
{
  // Required:
  Id trip_id;
  Id stop_id;
  size_t stop_sequence = 0;

  // Conditionally required:
  Time arrival_time;

  Time departure_time;

  // Optional:
  Text stop_headsign;
  StopTimeBoarding pickup_type = StopTimeBoarding::RegularlyScheduled;
  StopTimeBoarding drop_off_type = StopTimeBoarding::RegularlyScheduled;

  double shape_dist_traveled = 0.0;
  StopTimePoint timepoint = StopTimePoint::Exact;
};

// Conditionally required dataset file:
struct CalendarItem
{
  // Required:
  Id service_id;

  CalendarAvailability monday = CalendarAvailability::NotAvailable;
  CalendarAvailability tuesday = CalendarAvailability::NotAvailable;
  CalendarAvailability wednesday = CalendarAvailability::NotAvailable;
  CalendarAvailability thursday = CalendarAvailability::NotAvailable;
  CalendarAvailability friday = CalendarAvailability::NotAvailable;
  CalendarAvailability saturday = CalendarAvailability::NotAvailable;
  CalendarAvailability sunday = CalendarAvailability::NotAvailable;

  Date start_date;
  Date end_date;
};

// Conditionally required dataset file
struct CalendarDate
{
  // Required:
  Id service_id;
  Date date;
  CalendarDateException exception_type = CalendarDateException::Added;
};

// Optional dataset file
struct FareAttribute
{
  // Required:
  Id fare_id;
  double price = 0.0;
  CurrencyCode currency_code;
  FarePayment payment_method = FarePayment::BeforeBoarding;
  FareTransfers transfers = FareTransfers::Unlimited;

  // Conditionally required:
  Id agency_id;

  // Optional:
  size_t transfer_duration = 0;  // Length of time in seconds before a transfer expires
};

// Optional dataset file
struct FareRule
{
  // Required:
  Id fare_id;

  // Optional:
  Id route_id;
  Id origin_id;
  Id destination_id;
  Id contains_id;
};

// Optional dataset file
struct ShapePoint
{
  // Required:
  Id shape_id;
  double shape_pt_lat = 0.0;
  double shape_pt_lon = 0.0;
  size_t shape_pt_sequence = 0;

  // Optional:
  double shape_dist_traveled = 0;
};

// Optional dataset file
struct Frequency
{
  // Required:
  Id trip_id;
  Time start_time;
  Time end_time;
  size_t headway_secs = 0;

  // Optional:
  FrequencyTripService exact_times = FrequencyTripService::FrequencyBased;
};

// Optional dataset file
struct Transfer
{
  // Required:
  Id from_stop_id;
  Id to_stop_id;
  TransferType transfer_type = TransferType::Recommended;

  // Optional:
  size_t min_transfer_time = 0;
};

// Optional dataset file for the GTFS-Pathways extension
struct Pathway
{
  // Required:
  Id pathway_d;
  Id from_stop_id;
  Id to_stop_id;
  PathwayMode pathway_mode = PathwayMode::Walkway;
  PathwayDirection is_bidirectional = PathwayDirection::Unidirectional;

  // Optional fields:
  // Horizontal length in meters of the pathway from the origin location
  double length = 0.0;
  // Average time in seconds needed to walk through the pathway from the origin location
  size_t traversal_time = 0;
  // Number of stairs of the pathway
  size_t stair_count = 0;
  // Maximum slope ratio of the pathway
  double max_slope = 0.0;
  // Minimum width of the pathway in meters
  double min_width = 0.0;
  // Text from physical signage visible to transit riders
  Text signposted_as;
  // Same as signposted_as, but when the pathways is used backward
  Text reversed_signposted_as;
};

// Optional dataset file
struct Level
{
  // Required:
  Id level_id;

  // Numeric index of the level that indicates relative position of this level in relation to other
  // levels (levels with higher indices are assumed to be located above levels with lower indices).
  // Ground level should have index 0, with levels above ground indicated by positive indices and
  // levels below ground by negative indices
  double level_index = 0.0;

  // Optional:
  Text level_name;
};

// Optional dataset file
struct FeedInfo
{
  // Required:
  Text feed_publisher_name;
  Text feed_publisher_url;
  LanguageCode feed_lang;

  // Optional:
  Date feed_start_date;
  Date feed_end_date;
  Text feed_version;
  Text feed_contact_email;
  Text feed_contact_url;
};

// Optional dataset file
struct Translation
{
  // Required:
  TranslationTable table_name = TranslationTable::Agency;
  Text field_name;
  LanguageCode language;
  Text translation;

  // Conditionally required:
  Id record_id;
  Id record_sub_id;
  Text field_value;
};

// Optional dataset file
struct Attribution
{
  // Required:
  Text organization_name;

  // Optional:
  Id attribution_id;  // Useful for translations
  Id agency_id;
  Id route_id;
  Id trip_id;

  AttributionRole is_producer = AttributionRole::No;
  AttributionRole is_operator = AttributionRole::No;
  AttributionRole is_authority = AttributionRole::No;

  Text attribution_url;
  Text attribution_email;
  Text attribution_phone;
};

// Main classes for working with GTFS feeds
using Agencies = std::vector<Agency>;
using Stops = std::vector<Stop>;
using Routes = std::vector<Route>;
using Trips = std::vector<Trip>;
using StopTimes = std::vector<StopTime>;
using Calendar = std::vector<CalendarItem>;
using CalendarDates = std::vector<CalendarDate>;

using FareRules = std::vector<FareRule>;
using Shapes = std::vector<ShapePoint>;
using Shape = std::vector<ShapePoint>;
using Frequencies = std::vector<Frequency>;
using Transfers = std::vector<Transfer>;
using Pathways = std::vector<Pathway>;
using Levels = std::vector<Level>;
// FeedInfo is a unique object and doesn't need a container.
using Translations = std::vector<Translation>;
using Attributions = std::vector<Attribution>;

using ParsedCsvRow = std::map<std::string, std::string>;

class Feed
{
public:
  inline Feed() = default;
  inline explicit Feed(const std::string & gtfs_path);

  inline Result read_feed();

  inline Result write_feed(const std::string & gtfs_path = {}) const;

  inline Result read_agencies();
  inline const Agencies & get_agencies() const;
  inline std::optional<Agency> get_agency(const Id & agency_id) const;
  inline void add_agency(const Agency & agency);

  inline Result read_stops();
  inline const Stops & get_stops() const;
  inline std::optional<Stop> get_stop(const Id & stop_id) const;
  inline void add_stop(const Stop & stop);

  inline Result read_routes();
  inline const Routes & get_routes() const;
  inline std::optional<Route> get_route(const Id & route_id) const;
  inline void add_route(const Route & route);

  inline Result read_trips();
  inline const Trips & get_trips() const;
  inline std::optional<Trip> get_trip(const Id & trip_id) const;
  inline void add_trip(const Trip & trip);

  inline Result read_stop_times();
  inline const StopTimes & get_stop_times() const;
  inline StopTimes get_stop_times_for_stop(const Id & stop_id) const;
  inline StopTimes get_stop_times_for_trip(const Id & trip_id, bool sort_by_sequence = true) const;
  inline void add_stop_time(const StopTime & stop_time);

  inline Result read_calendar();
  inline const Calendar & get_calendar() const;
  inline std::optional<CalendarItem> get_calendar(const Id & service_id) const;
  inline void add_calendar_item(const CalendarItem & calendar_item);

  inline Result read_calendar_dates();
  inline const CalendarDates & get_calendar_dates() const;
  inline CalendarDates get_calendar_dates(const Id & service_id, bool sort_by_date = true) const;
  inline void add_calendar_date(const CalendarDate & calendar_date);

  inline Result read_fare_rules();
  inline const FareRules & get_fare_rules() const;
  inline std::optional<FareRule> get_fare_rule(const Id & fare_id) const;
  inline void add_fare_rule(const FareRule & fare_rule);

  inline Result read_shapes();
  inline const Shapes & get_shapes() const;
  inline Shape get_shape(const Id & shape_id, bool sort_by_sequence = true) const;
  inline void add_shape(const ShapePoint & shape);

  inline Result read_frequencies();
  inline const Frequencies & get_frequencies() const;
  inline Frequencies get_frequencies(const Id & trip_id) const;
  inline void add_frequency(const Frequency & frequency);

  inline Result read_transfers();
  inline const Transfers & get_transfers() const;
  inline std::optional<Transfer> get_transfer(const Id & from_stop_id, const Id & to_stop_id) const;
  inline void add_transfer(const Transfer & transfer);

  inline Result read_pathways();
  inline const Pathways & get_pathways() const;
  inline std::optional<Pathway> get_pathway(const Id & pathway_id) const;
  inline std::optional<Pathway> get_pathway(const Id & from_stop_id, const Id & to_stop_id) const;
  inline void add_pathway(const Pathway & pathway);

  inline Result read_levels();
  inline const Levels & get_levels() const;
  inline std::optional<Level> get_level(const Id & level_id) const;
  inline void add_level(const Level & level);

  inline Result read_feed_info();
  inline FeedInfo get_feed_info() const;
  inline void set_feed_info(const FeedInfo & feed_info);

  inline Result read_translations();
  inline const Translations & get_translations() const;
  inline std::optional<Translation> get_translation(const TranslationTable & table_name) const;
  inline void add_translation(const Translation & translation);

  inline Result read_attributions();
  inline const Attributions & get_attributions() const;
  inline void add_attribution(const Attribution & attribution);

private:
  inline Result parse_csv(const std::string & filename,
                          const std::function<Result(const ParsedCsvRow & record)> & add_entity);

  inline Result add_agency(ParsedCsvRow const & row);
  inline Result add_route(ParsedCsvRow const & row);
  inline Result add_shape(ParsedCsvRow const & row);
  inline Result add_trip(ParsedCsvRow const & row);
  inline Result add_stop(ParsedCsvRow const & row);
  inline Result add_stop_time(ParsedCsvRow const & row);
  inline Result add_calendar_item(ParsedCsvRow const & row);
  inline Result add_calendar_date(ParsedCsvRow const & row);
  inline Result add_transfer(ParsedCsvRow const & row);
  inline Result add_frequency(ParsedCsvRow const & row);

  std::string gtfs_directory;

  Agencies agencies;
  Stops stops;
  Routes routes;
  Trips trips;
  StopTimes stop_times;

  Calendar calendar;
  CalendarDates calendar_dates;
  FareRules fare_rules;
  Shape shapes;
  Frequencies frequencies;
  Transfers transfers;
  Pathways pathways;
  Levels levels;
  Translations translations;
  Attributions attributions;
  FeedInfo feed_info;
};

inline Feed::Feed(const std::string & gtfs_path) : gtfs_directory(gtfs_path) {}

inline Result Feed::read_feed()
{
  if (!std::filesystem::exists(gtfs_directory))
    return {ResultCode::ERROR_INVALID_GTFS_PATH, "Invalid path " + gtfs_directory};

  // Read required files
  if (auto const res = read_agencies(); res.code != ResultCode::OK)
    return res;

  if (auto const res = read_stops(); res.code != ResultCode::OK)
    return res;

  if (auto const res = read_routes(); res.code != ResultCode::OK)
    return res;

  if (auto const res = read_trips(); res.code != ResultCode::OK)
    return res;

  if (auto const res = read_stop_times(); res.code != ResultCode::OK)
    return res;

  // Conditionally required:
  if (auto const res = read_calendar(); res.code != ResultCode::OK)
  {
    if (res != ResultCode::ERROR_FILE_ABSENT)
      return res;
  }

  if (auto const res = read_calendar_dates(); res.code != ResultCode::OK)
  {
    if (res != ResultCode::ERROR_FILE_ABSENT)
      return res;
  }

  // Optional files:
  if (auto const res = read_shapes(); res.code != ResultCode::OK)
  {
    if (res != ResultCode::ERROR_FILE_ABSENT)
      return res;
  }

  if (auto const res = read_transfers(); res.code != ResultCode::OK)
  {
    if (res != ResultCode::ERROR_FILE_ABSENT)
      return res;
  }

  if (auto const res = read_frequencies(); res.code != ResultCode::OK)
  {
    if (res != ResultCode::ERROR_FILE_ABSENT)
      return res;
  }

  // TODO Read other conditionally optional and optional files

  return {ResultCode::OK, {}};
}

inline Result Feed::write_feed(const std::string & gtfs_path) const
{
  if (gtfs_path.empty())
    return {ResultCode::ERROR_INVALID_GTFS_PATH, "Empty output path for writing feed"};
  // TODO Write feed to csv files
  return {};
}

inline std::string get_value_or_default(ParsedCsvRow const & container, const std::string & key,
                                        const std::string & default_value = "")
{
  const auto it = container.find(key);
  if (it == container.end())
    return default_value;

  return it->second;
}

template <class T>
inline void set_field(T & field, ParsedCsvRow const & container, const std::string & key,
                      bool is_optional = true)
{
  const std::string key_str = get_value_or_default(container, key);
  if (!key_str.empty() || !is_optional)
    field = static_cast<T>(std::stoi(key_str));
}

inline bool set_fractional(double & field, ParsedCsvRow const & container, const std::string & key,
                           bool is_optional = true)
{
  const std::string key_str = get_value_or_default(container, key);
  if (!key_str.empty() || !is_optional)
  {
    field = std::stod(key_str);
    return true;
  }
  return false;
}

// Throw if not valid WGS84 decimal degrees.
inline void check_coordinates(double latitude, double longitude)
{
  if (latitude < -90.0 || latitude > 90.0)
    throw std::out_of_range("Latitude");

  if (longitude < -180.0 || longitude > 180.0)
    throw std::out_of_range("Longitude");
}

inline Result Feed::add_agency(ParsedCsvRow const & row)
{
  Agency agency;

  // Conditionally required id:
  agency.agency_id = get_value_or_default(row, "agency_id");

  // Required fields:
  try
  {
    agency.agency_name = row.at("agency_name");
    agency.agency_url = row.at("agency_url");
    agency.agency_timezone = row.at("agency_timezone");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }

  // Optional fields:
  agency.agency_lang = get_value_or_default(row, "agency_lang");
  agency.agency_phone = get_value_or_default(row, "agency_phone");
  agency.agency_fare_url = get_value_or_default(row, "agency_fare_url");
  agency.agency_email = get_value_or_default(row, "agency_email");

  agencies.push_back(agency);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_route(ParsedCsvRow const & row)
{
  Route route;

  try
  {
    // Required fields:
    route.route_id = row.at("route_id");
    set_field(route.route_type, row, "route_type", false);

    // Optional:
    set_field(route.route_sort_order, row, "route_sort_order");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  // Conditionally required:
  route.agency_id = get_value_or_default(row, "agency_id");

  route.route_short_name = get_value_or_default(row, "route_short_name");
  route.route_long_name = get_value_or_default(row, "route_long_name");

  if (route.route_short_name.empty() && route.route_long_name.empty())
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT,
            "'route_short_name' or 'route_long_name' must be specified"};
  }

  route.route_color = get_value_or_default(row, "route_color");
  route.route_text_color = get_value_or_default(row, "route_text_color");
  route.route_desc = get_value_or_default(row, "route_desc");
  route.route_url = get_value_or_default(row, "route_url");

  routes.push_back(route);

  return {ResultCode::OK, {}};
}

inline Result Feed::add_shape(ParsedCsvRow const & row)
{
  ShapePoint point;
  try
  {
    // Required:
    point.shape_id = row.at("shape_id");
    point.shape_pt_sequence = std::stoi(row.at("shape_pt_sequence"));

    point.shape_pt_lon = std::stod(row.at("shape_pt_lon"));
    point.shape_pt_lat = std::stod(row.at("shape_pt_lat"));
    check_coordinates(point.shape_pt_lat, point.shape_pt_lon);

    // Optional:
    set_fractional(point.shape_dist_traveled, row, "shape_dist_traveled");
    if (point.shape_dist_traveled < 0.0)
      throw std::invalid_argument("Invalid shape_dist_traveled");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  shapes.push_back(point);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_trip(ParsedCsvRow const & row)
{
  Trip trip;
  try
  {
    // Required:
    trip.route_id = row.at("route_id");
    trip.service_id = row.at("service_id");
    trip.trip_id = row.at("trip_id");

    // Optional:
    set_field(trip.direction_id, row, "direction_id");
    set_field(trip.wheelchair_accessible, row, "wheelchair_accessible");
    set_field(trip.bikes_allowed, row, "bikes_allowed");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  // Optional:
  trip.shape_id = get_value_or_default(row, "shape_id");
  trip.trip_headsign = get_value_or_default(row, "trip_headsign");
  trip.trip_short_name = get_value_or_default(row, "trip_short_name");
  trip.block_id = get_value_or_default(row, "block_id");

  trips.push_back(trip);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_stop(ParsedCsvRow const & row)
{
  Stop stop;

  try
  {
    stop.stop_id = row.at("stop_id");

    // Optional:
    bool const set_lon = set_fractional(stop.stop_lon, row, "stop_lon");
    bool const set_lat = set_fractional(stop.stop_lat, row, "stop_lat");

    if (!set_lon || !set_lat)
      stop.coordinates_present = false;
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  // Conditionally required:
  stop.stop_name = get_value_or_default(row, "stop_name");
  stop.parent_station = get_value_or_default(row, "parent_station");
  stop.zone_id = get_value_or_default(row, "zone_id");

  // Optional:
  stop.stop_code = get_value_or_default(row, "stop_code");
  stop.stop_desc = get_value_or_default(row, "stop_desc");
  stop.stop_url = get_value_or_default(row, "stop_url");
  set_field(stop.location_type, row, "location_type");
  stop.stop_timezone = get_value_or_default(row, "stop_timezone");
  stop.wheelchair_boarding = get_value_or_default(row, "wheelchair_boarding");
  stop.level_id = get_value_or_default(row, "level_id");
  stop.platform_code = get_value_or_default(row, "platform_code");

  stops.push_back(stop);

  return {ResultCode::OK, {}};
}

inline Result Feed::add_stop_time(ParsedCsvRow const & row)
{
  StopTime stop_time;

  try
  {
    // Required:
    stop_time.trip_id = row.at("trip_id");
    stop_time.stop_id = row.at("stop_id");
    stop_time.stop_sequence = std::stoi(row.at("stop_sequence"));

    // Conditionally required:
    stop_time.departure_time = Time(row.at("departure_time"));
    stop_time.arrival_time = Time(row.at("arrival_time"));

    // Optional:
    set_field(stop_time.pickup_type, row, "pickup_type");
    set_field(stop_time.drop_off_type, row, "drop_off_type");

    set_fractional(stop_time.shape_dist_traveled, row, "shape_dist_traveled");
    if (stop_time.shape_dist_traveled < 0.0)
      throw std::invalid_argument("Invalid shape_dist_traveled");

    set_field(stop_time.timepoint, row, "timepoint");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }
  catch (const InvalidFieldFormat & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  // Optional:
  stop_time.stop_headsign = get_value_or_default(row, "stop_headsign");

  stop_times.push_back(stop_time);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_calendar_item(ParsedCsvRow const & row)
{
  CalendarItem calendar_item;
  try
  {
    // Required fields:
    calendar_item.service_id = row.at("service_id");

    set_field(calendar_item.monday, row, "monday", false);
    set_field(calendar_item.tuesday, row, "tuesday", false);
    set_field(calendar_item.wednesday, row, "wednesday", false);
    set_field(calendar_item.thursday, row, "thursday", false);
    set_field(calendar_item.friday, row, "friday", false);
    set_field(calendar_item.saturday, row, "saturday", false);
    set_field(calendar_item.sunday, row, "sunday", false);

    calendar_item.start_date = Date(row.at("start_date"));
    calendar_item.end_date = Date(row.at("end_date"));
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }
  catch (const InvalidFieldFormat & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  calendar.push_back(calendar_item);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_calendar_date(ParsedCsvRow const & row)
{
  CalendarDate calendar_date;
  try
  {
    // Required fields:
    calendar_date.service_id = row.at("service_id");

    set_field(calendar_date.exception_type, row, "exception_type", false);
    calendar_date.date = Date(row.at("date"));
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }
  catch (const InvalidFieldFormat & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  calendar_dates.push_back(calendar_date);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_transfer(ParsedCsvRow const & row)
{
  Transfer transfer;
  try
  {
    // Required fields:
    transfer.from_stop_id = row.at("from_stop_id");
    transfer.to_stop_id = row.at("to_stop_id");
    set_field(transfer.transfer_type, row, "transfer_type", false);

    // Optional:
    set_field(transfer.min_transfer_time, row, "min_transfer_time");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }
  catch (const InvalidFieldFormat & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  transfers.push_back(transfer);
  return {ResultCode::OK, {}};
}

inline Result Feed::add_frequency(ParsedCsvRow const & row)
{
  Frequency frequency;
  try
  {
    // Required fields:
    frequency.trip_id = row.at("trip_id");
    frequency.start_time = Time(row.at("start_time"));
    frequency.end_time = Time(row.at("end_time"));
    set_field(frequency.headway_secs, row, "headway_secs", false);

    // Optional:
    set_field(frequency.exact_times, row, "exact_times");
  }
  catch (const std::out_of_range & ex)
  {
    return {ResultCode::ERROR_REQUIRED_FIELD_ABSENT, ex.what()};
  }
  catch (const std::invalid_argument & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }
  catch (const InvalidFieldFormat & ex)
  {
    return {ResultCode::ERROR_INVALID_FIELD_FORMAT, ex.what()};
  }

  frequencies.push_back(frequency);
  return {ResultCode::OK, {}};
}

inline Result Feed::parse_csv(const std::string & filename,
                              const std::function<Result(const ParsedCsvRow & record)> & add_entity)
{
  CsvParser parser(gtfs_directory);
  auto res_header = parser.read_header(filename);
  if (res_header.code != ResultCode::OK)
    return res_header;

  ParsedCsvRow record;
  Result res_row;
  while ((res_row = parser.read_row(record)) != ResultCode::END_OF_FILE)
  {
    if (res_row != ResultCode::OK)
      return res_row;

    if (record.empty())
      continue;

    Result res = add_entity(record);
    if (res != ResultCode::OK)
      return res;
  }

  return {ResultCode::OK, {"Parsed " + filename}};
}

inline Result Feed::read_agencies()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_agency(record); };
  return parse_csv("agency.txt", handler);
}

inline const Agencies & Feed::get_agencies() const { return agencies; }

inline std::optional<Agency> Feed::get_agency(const Id & agency_id) const
{
  // agency id is required when the dataset contains data for multiple agencies,
  // otherwise it is optional:
  if (agency_id.empty() && agencies.size() == 1)
    return agencies[0];

  const auto it =
      std::find_if(agencies.begin(), agencies.end(),
                   [&agency_id](const Agency & agency) { return agency.agency_id == agency_id; });

  if (it == agencies.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_agency(const Agency & agency) { agencies.push_back(agency); }

inline Result Feed::read_stops()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_stop(record); };
  return parse_csv("stops.txt", handler);
}

inline const Stops & Feed::get_stops() const { return stops; }

inline std::optional<Stop> Feed::get_stop(const Id & stop_id) const
{
  const auto it = std::find_if(stops.begin(), stops.end(),
                               [&stop_id](const Stop & stop) { return stop.stop_id == stop_id; });

  if (it == stops.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_stop(const Stop & stop) { stops.push_back(stop); }

inline Result Feed::read_routes()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_route(record); };
  return parse_csv("routes.txt", handler);
}

inline const Routes & Feed::get_routes() const { return routes; }

inline std::optional<Route> Feed::get_route(const Id & route_id) const
{
  const auto it = std::find_if(routes.begin(), routes.end(), [&route_id](const Route & route) {
    return route.route_id == route_id;
  });

  if (it == routes.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_route(const Route & route) { routes.push_back(route); }

inline Result Feed::read_trips()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_trip(record); };
  return parse_csv("trips.txt", handler);
}

inline const Trips & Feed::get_trips() const { return trips; }

inline std::optional<Trip> Feed::get_trip(const Id & trip_id) const
{
  const auto it = std::find_if(trips.begin(), trips.end(),
                               [&trip_id](const Trip & trip) { return trip.trip_id == trip_id; });

  if (it == trips.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_trip(const Trip & trip) { trips.push_back(trip); }

inline Result Feed::read_stop_times()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_stop_time(record); };
  return parse_csv("stop_times.txt", handler);
}

inline const StopTimes & Feed::get_stop_times() const { return stop_times; }

inline StopTimes Feed::get_stop_times_for_stop(const Id & stop_id) const
{
  StopTimes res;
  for (const auto & stop_time : stop_times)
  {
    if (stop_time.stop_id == stop_id)
      res.push_back(stop_time);
  }
  return res;
}

inline StopTimes Feed::get_stop_times_for_trip(const Id & trip_id, bool sort_by_sequence) const
{
  StopTimes res;
  for (const auto & stop_time : stop_times)
  {
    if (stop_time.trip_id == trip_id)
      res.push_back(stop_time);
  }
  if (sort_by_sequence)
  {
    std::sort(res.begin(), res.end(), [](const StopTime & t1, const StopTime & t2) {
      return t1.stop_sequence < t2.stop_sequence;
    });
  }
  return res;
}

inline void Feed::add_stop_time(const StopTime & stop_time) { stop_times.push_back(stop_time); }

inline Result Feed::read_calendar()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_calendar_item(record); };
  return parse_csv("calendar.txt", handler);
}

inline const Calendar & Feed::get_calendar() const { return calendar; }

inline std::optional<CalendarItem> Feed::get_calendar(const Id & service_id) const
{
  const auto it = std::find_if(calendar.begin(), calendar.end(),
                               [&service_id](const CalendarItem & calendar_item) {
                                 return calendar_item.service_id == service_id;
                               });

  if (it == calendar.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_calendar_item(const CalendarItem & calendar_item)
{
  calendar.push_back(calendar_item);
}

inline Result Feed::read_calendar_dates()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_calendar_date(record); };
  return parse_csv("calendar_dates.txt", handler);
}

inline const CalendarDates & Feed::get_calendar_dates() const { return calendar_dates; }

inline CalendarDates Feed::get_calendar_dates(const Id & service_id, bool sort_by_date) const
{
  CalendarDates res;
  for (const auto & calendar_date : calendar_dates)
  {
    if (calendar_date.service_id == service_id)
      res.push_back(calendar_date);
  }

  if (sort_by_date)
  {
    std::sort(res.begin(), res.end(), [](const CalendarDate & d1, const CalendarDate & d2) {
      return d1.date.get_raw_date() < d2.date.get_raw_date();
    });
  }

  return res;
}

inline void Feed::add_calendar_date(const CalendarDate & calendar_date)
{
  calendar_dates.push_back(calendar_date);
}

inline Result Feed::read_fare_rules()
{
  // TODO Read csv
  return {};
}

inline const FareRules & Feed::get_fare_rules() const { return fare_rules; }

inline std::optional<FareRule> Feed::get_fare_rule(const Id & fare_id) const
{
  const auto it =
      std::find_if(fare_rules.begin(), fare_rules.end(),
                   [&fare_id](const FareRule & fare_rule) { return fare_rule.fare_id == fare_id; });

  if (it == fare_rules.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_fare_rule(const FareRule & fare_rule) { fare_rules.push_back(fare_rule); }

inline Result Feed::read_shapes()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_shape(record); };
  return parse_csv("shapes.txt", handler);
}

inline const Shapes & Feed::get_shapes() const { return shapes; }

inline Shape Feed::get_shape(const Id & shape_id, bool sort_by_sequence) const
{
  Shape res;
  for (const auto & shape : shapes)
  {
    if (shape.shape_id == shape_id)
      res.push_back(shape);
  }
  if (sort_by_sequence)
  {
    std::sort(res.begin(), res.end(), [](const ShapePoint & s1, const ShapePoint & s2) {
      return s1.shape_pt_sequence < s2.shape_pt_sequence;
    });
  }
  return res;
}

inline void Feed::add_shape(const ShapePoint & shape) { shapes.push_back(shape); }

inline Result Feed::read_frequencies()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_frequency(record); };
  return parse_csv("frequencies.txt", handler);
}

inline const Frequencies & Feed::get_frequencies() const { return frequencies; }

inline Frequencies Feed::get_frequencies(const Id & trip_id) const
{
  Frequencies res;
  for (const auto & frequency : frequencies)
  {
    if (frequency.trip_id == trip_id)
      res.push_back(frequency);
  }
  return res;
}

inline void Feed::add_frequency(const Frequency & frequency) { frequencies.push_back(frequency); }

inline Result Feed::read_transfers()
{
  auto handler = [this](const ParsedCsvRow & record) { return this->add_transfer(record); };
  return parse_csv("transfers.txt", handler);
}

inline const Transfers & Feed::get_transfers() const { return transfers; }

inline std::optional<Transfer> Feed::get_transfer(const Id & from_stop_id,
                                                  const Id & to_stop_id) const
{
  const auto it = std::find_if(
      transfers.begin(), transfers.end(), [&from_stop_id, &to_stop_id](const Transfer & transfer) {
        return transfer.from_stop_id == from_stop_id && transfer.to_stop_id == to_stop_id;
      });

  if (it == transfers.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_transfer(const Transfer & transfer) { transfers.push_back(transfer); }

inline Result Feed::read_pathways()
{
  // TODO Read csv
  return {};
}

inline const Pathways & Feed::get_pathways() const { return pathways; }

inline std::optional<Pathway> Feed::get_pathway(const Id & pathway_id) const
{
  const auto it = std::find_if(
      pathways.begin(), pathways.end(),
      [&pathway_id](const Pathway & pathway) { return pathway.pathway_d == pathway_id; });

  if (it == pathways.end())
    return std::nullopt;

  return *it;
}

inline std::optional<Pathway> Feed::get_pathway(const Id & from_stop_id,
                                                const Id & to_stop_id) const
{
  const auto it = std::find_if(
      pathways.begin(), pathways.end(), [&from_stop_id, &to_stop_id](const Pathway & pathway) {
        return pathway.from_stop_id == from_stop_id && pathway.to_stop_id == to_stop_id;
      });

  if (it == pathways.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_pathway(const Pathway & pathway) { pathways.push_back(pathway); }

inline Result Feed::read_levels()
{
  // TODO Read csv
  return {};
}

inline const Levels & Feed::get_levels() const { return levels; }

inline std::optional<Level> Feed::get_level(const Id & level_id) const
{
  const auto it = std::find_if(levels.begin(), levels.end(), [&level_id](const Level & level) {
    return level.level_id == level_id;
  });

  if (it == levels.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_level(const Level & level) { levels.push_back(level); }

inline Result Feed::read_feed_info()
{
  // TODO Read csv
  return {};
}

inline FeedInfo Feed::get_feed_info() const { return feed_info; }

inline void Feed::set_feed_info(const FeedInfo & info) { feed_info = info; }

inline Result Feed::read_translations()
{
  // TODO Read csv
  return {};
}

inline const Translations & Feed::get_translations() const { return translations; }

inline std::optional<Translation> Feed::get_translation(const TranslationTable & table_name) const
{
  const auto it = std::find_if(translations.begin(), translations.end(),
                               [&table_name](const Translation & translation) {
                                 return translation.table_name == table_name;
                               });

  if (it == translations.end())
    return std::nullopt;

  return *it;
}

inline void Feed::add_translation(const Translation & translation)
{
  translations.push_back(translation);
}

inline Result Feed::read_attributions()
{
  // TODO Read csv
  return {};
}

inline const Attributions & Feed::get_attributions() const { return attributions; }

inline void Feed::add_attribution(const Attribution & attribution)
{
  attributions.push_back(attribution);
}
}  // namespace gtfs