Bimba.git

ref: e5c7dc933fba208aba52c3dcb305b522120b4b2c

app/src/main/java/xyz/apiote/bimba/czwek/api/Structs.kt


  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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.api

import android.os.Parcelable
import android.util.Log
import kotlinx.parcelize.Parcelize
import org.yaml.snakeyaml.Yaml
import xyz.apiote.bimba.czwek.api.structs.VehicleStatusV1
import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime
import kotlin.reflect.KClass

class TrafficFormatException(override val message: String) : IllegalArgumentException()
class UnknownResourceVersionException(resource: String, val version: ULong) :
	Exception("unknown version $version of $resource")

class UnknownResourceException(resource: String, cls: KClass<*>) :
	Exception("unknown class $cls for $resource")

data class BimbaInfo(
	val contact: Map<String, String>,
)

data class Bimba(
	val info: BimbaInfo,
	val servers: List<Map<String, String>>,
	val security: List<Map<String, List<*>>>
) {
	companion object {
		@Suppress("UNCHECKED_CAST")
		fun unmarshal(stream: InputStream): Bimba {
			val map = Yaml().load(stream) as HashMap<String, *>
			val contact =
				if (map["info"] is Map<*, *> && (map["info"] as Map<*, *>).keys.all { it is String }) {
					(map["info"] as Map<String, *>)["contact"]
				} else {
					throw TrafficFormatException("invalid info format")
				}
			val contactMap =
				if (contact is Map<*, *> && contact.all { it.key is String && it.value is String }) {
					contact as Map<String, String>
				} else {
					throw TrafficFormatException("invalid contact format")
				}
			val servers = if (map["servers"] is List<*> && (map["servers"] as List<*>).all { server ->
					server is Map<*, *> && server.all { it.key is String && it.value is String }
				}) {
				map["servers"] as List<Map<String, String>>
			} else {
				throw TrafficFormatException("invalid servers format")
			}
			val security =
				if (map["security"] is List<*> && (map["security"] as List<*>).all { security ->
						security is Map<*, *> && security.all { it.key is String && it.value is List<*> }
					}) {
					map["security"] as List<Map<String, List<*>>>
				} else {
					throw TrafficFormatException("invalid security format")
				}
			val bimba = Bimba(BimbaInfo(contactMap), servers, security)
			bimba.validate()
			return bimba
		}
	}

	fun isPrivate(): Boolean {
		return security.size == 1 && security[0]["api_key"] != null
	}

	fun isRateLimited(): Boolean {
		val items = security.foldRight(0b00) { map, acc ->
			acc or when {
				map.containsKey("api_key") -> 0b10
				map.isEmpty() -> 0b01
				else -> 0b00
			}
		}
		Log.i("Rate limited", "${this}, $items")
		return security.size == 2 && items == 0b11
	}

	private fun validate() {
		if (servers.isEmpty() || servers[0]["url"] == null) {
			throw TrafficFormatException("no server in info")
		}
		if (security.isEmpty()) {
			throw TrafficFormatException("no security")
		}
	}
}

@Parcelize
data class PositionV1(
	val latitude: Double, val longitude: Double
) : Parcelable {

	override fun toString(): String = "$latitude,$longitude"


	companion object {
		fun unmarshal(stream: InputStream): PositionV1 {
			val reader = Reader(stream)
			return PositionV1(
				reader.readFloat64(), reader.readFloat64()
			)
		}
	}
}


enum class AlertCauseV1 {
	UNKNOWN, OTHER, TECHNICAL_PROBLEM, STRIKE, DEMONSTRATION, ACCIDENT, HOLIDAY, WEATHER, MAINTENANCE,
	CONSTRUCTION, POLICE_ACTIVITY, MEDICAL_EMERGENCY;

	companion object {
		fun of(type: UInt): AlertCauseV1 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("OTHER")
				2u -> valueOf("TECHNICAL_PROBLEM")
				3u -> valueOf("STRIKE")
				4u -> valueOf("DEMONSTRATION")
				5u -> valueOf("ACCIDENT")
				6u -> valueOf("HOLIDAY")
				7u -> valueOf("WEATHER")
				8u -> valueOf("MAINTENANCE")
				9u -> valueOf("CONSTRUCTION")
				10u -> valueOf("POLICE_ACTIVITY")
				11u -> valueOf("MEDICAL_EMERGENCY")
				else -> throw UnknownResourceVersionException("AlertCause/$type", 1u)
			}
		}
	}
}

enum class AlertEffectV1 {
	UNKNOWN, OTHER, NO_SERVICE, REDUCED_SERVICE, SIGNIFICANT_DELAYS, DETOUR, ADDITIONAL_SERVICE,
	MODIFIED_SERVICE, STOP_MOVED, NONE, ACCESSIBILITY_ISSUE;

	companion object {
		fun of(type: UInt): AlertEffectV1 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("OTHER")
				2u -> valueOf("NO_SERVICE")
				3u -> valueOf("REDUCED_SERVICE")
				4u -> valueOf("SIGNIFICANT_DELAYS")
				5u -> valueOf("DETOUR")
				6u -> valueOf("ADDITIONAL_SERVICE")
				7u -> valueOf("MODIFIED_SERVICE")
				8u -> valueOf("STOP_MOVED")
				9u -> valueOf("NONE")
				10u -> valueOf("ACCESSIBILITY_ISSUE")
				else -> throw UnknownResourceVersionException("AlertEffect/$type", 1u)
			}
		}
	}
}

data class AlertV1(
	val header: String,
	val Description: String,
	val Url: String,
	val Cause: AlertCauseV1,
	val Effect: AlertEffectV1
) {
	companion object {
		fun unmarshal(stream: InputStream): AlertV1 {
			val reader = Reader(stream)
			return AlertV1(
				reader.readString(),
				reader.readString(),
				reader.readString(),
				AlertCauseV1.of(reader.readUInt().toULong().toUInt()),
				AlertEffectV1.of(reader.readUInt().toULong().toUInt())
			)
		}
	}
}

data class Time(
	val Hour: UInt, val Minute: UInt, val Second: UInt, val DayOffset: Byte, val Zone: String
) {
	companion object {
		fun unmarshal(stream: InputStream): Time {
			val reader = Reader(stream)
			return Time(
				reader.readUInt().toULong().toUInt(),
				reader.readUInt().toULong().toUInt(),
				reader.readUInt().toULong().toUInt(),
				reader.readI8(),
				reader.readString()
			)
		}
	}

	fun toDateTime(): ZonedDateTime {
		return ZonedDateTime.of(
			LocalDateTime.of(
				LocalDate.now().plusDays(DayOffset.toLong()),
				LocalTime.of(Hour.toInt(), Minute.toInt(), Second.toInt())
			),
			ZoneId.of(Zone)
		)
	}
}

data class ColourV1(val R: UByte, val G: UByte, val B: UByte) {
	companion object {
		fun unmarshal(stream: InputStream): ColourV1 {
			val reader = Reader(stream)
			return ColourV1(
				reader.readU8(), reader.readU8(), reader.readU8()
			)
		}
	}
}

enum class CongestionLevelV1 {
	UNKNOWN, SMOOTH, STOP_AND_GO, SIGNIFICANT, SEVERE;

	companion object {
		fun of(type: UInt): CongestionLevelV1 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("SMOOTH")
				2u -> valueOf("STOP_AND_GO")
				3u -> valueOf("SIGNIFICANT")
				4u -> valueOf("SEVERE")
				else -> throw UnknownResourceVersionException("CongestionLevel/$type", 1u)
			}
		}
	}
}

enum class OccupancyStatusV1 {
	UNKNOWN, EMPTY, MANY_AVAILABLE, FEW_AVAILABLE, STANDING_ONLY, CRUSHED, FULL, NOT_ACCEPTING;

	companion object {
		fun of(type: UInt): OccupancyStatusV1 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("EMPTY")
				2u -> valueOf("MANY_AVAILABLE")
				3u -> valueOf("FEW_AVAILABLE")
				4u -> valueOf("STANDING_ONLY")
				5u -> valueOf("CRUSHED")
				6u -> valueOf("FULL")
				7u -> valueOf("NOT_ACCEPTING")
				else -> throw UnknownResourceVersionException("OccupancyStatus/$type", 1u)
			}
		}
	}
}

data class VehicleV1(
	val ID: String,
	val Position: PositionV1,
	val Capabilities: UShort,
	val Speed: Float,
	val Line: LineStubV1,
	val Headsign: String,
	val CongestionLevel: CongestionLevelV1,
	val OccupancyStatus: OccupancyStatusV1
) : LocatableV1 {
	companion object {
		fun unmarshal(stream: InputStream): VehicleV1 {
			val reader = Reader(stream)
			return VehicleV1(
				reader.readString(),
				PositionV1.unmarshal(stream),
				reader.readU16(),
				reader.readFloat32(),
				LineStubV1.unmarshal(stream),
				reader.readString(),
				CongestionLevelV1.of(reader.readUInt().toULong().toUInt()),
				OccupancyStatusV1.of(reader.readUInt().toULong().toUInt())
			)
		}
	}
}

data class VehicleV2(
	val ID: String,
	val Position: PositionV1,
	val Capabilities: UShort,
	val Speed: Float,
	val Line: LineStubV2,
	val Headsign: String,
	val CongestionLevel: CongestionLevelV1,
	val OccupancyStatus: OccupancyStatusV1
) : LocatableV2 {
	companion object {
		fun unmarshal(stream: InputStream): VehicleV2 {
			val reader = Reader(stream)
			return VehicleV2(
				reader.readString(),
				PositionV1.unmarshal(stream),
				reader.readU16(),
				reader.readFloat32(),
				LineStubV2.unmarshal(stream),
				reader.readString(),
				CongestionLevelV1.of(reader.readUInt().toULong().toUInt()),
				OccupancyStatusV1.of(reader.readUInt().toULong().toUInt())
			)
		}
	}
}

data class VehicleV3(
	val ID: String,
	val Position: PositionV1,
	val Capabilities: UShort,
	val Speed: Float,
	val Line: LineStubV3,
	val Headsign: String,
	val CongestionLevel: CongestionLevelV1,
	val OccupancyStatus: OccupancyStatusV1
) : LocatableV3 {
	companion object {
		fun unmarshal(stream: InputStream): VehicleV3 {
			val reader = Reader(stream)
			return VehicleV3(
				reader.readString(),
				PositionV1.unmarshal(stream),
				reader.readU16(),
				reader.readFloat32(),
				LineStubV3.unmarshal(stream),
				reader.readString(),
				CongestionLevelV1.of(reader.readUInt().toULong().toUInt()),
				OccupancyStatusV1.of(reader.readUInt().toULong().toUInt())
			)
		}
	}
}

data class LineStubV1(
	val name: String, val kind: LineTypeV1, val colour: ColourV1
) {
	companion object {
		fun unmarshal(stream: InputStream): LineStubV1 {
			val reader = Reader(stream)
			return LineStubV1(
				reader.readString(),
				LineTypeV1.of(reader.readUInt().toULong().toUInt()),
				ColourV1.unmarshal(stream)
			)
		}
	}
}

data class LineStubV2(
	val name: String, val kind: LineTypeV2, val colour: ColourV1
) {
	companion object {
		fun unmarshal(stream: InputStream): LineStubV2 {
			val reader = Reader(stream)
			return LineStubV2(
				reader.readString(),
				LineTypeV2.of(reader.readUInt().toULong().toUInt()),
				ColourV1.unmarshal(stream)
			)
		}
	}
}

data class LineStubV3(
	val name: String, val kind: LineTypeV3, val colour: ColourV1
) {
	companion object {
		fun unmarshal(stream: InputStream): LineStubV3 {
			val reader = Reader(stream)
			return LineStubV3(
				reader.readString(),
				LineTypeV3.of(reader.readUInt().toULong().toUInt()),
				ColourV1.unmarshal(stream)
			)
		}
	}
}

data class DepartureV1(
	val ID: String,
	val time: Time,
	val status: ULong,
	val isRealtime: Boolean,
	val vehicle: VehicleV1,
	val boarding: UByte
) {

	companion object {
		fun unmarshal(stream: InputStream): DepartureV1 {
			val reader = Reader(stream)
			val id = reader.readString()
			val time = Time.unmarshal(stream)
			val status = reader.readUInt().toULong()
			val isRealtime = reader.readBoolean()
			val vehicle = VehicleV1.unmarshal(stream)
			val boarding = reader.readU8()
			return DepartureV1(id, time, status, isRealtime, vehicle, boarding)
		}
	}
}

data class DepartureV2(
	val ID: String,
	val time: Time,
	val status: ULong,
	val isRealtime: Boolean,
	val vehicle: VehicleV2,
	val boarding: UByte
) {

	companion object {
		fun unmarshal(stream: InputStream): DepartureV2 {
			val reader = Reader(stream)
			val id = reader.readString()
			val time = Time.unmarshal(stream)
			val status = reader.readUInt().toULong()
			val isRealtime = reader.readBoolean()
			val vehicle = VehicleV2.unmarshal(stream)
			val boarding = reader.readU8()
			return DepartureV2(id, time, status, isRealtime, vehicle, boarding)
		}
	}
}

data class DepartureV3(
	val ID: String,
	val time: Time,
	val status: VehicleStatusV1,
	val isRealtime: Boolean,
	val vehicle: VehicleV3,
	val boarding: UByte
) {

	companion object {
		fun unmarshal(stream: InputStream): DepartureV3 {
			val reader = Reader(stream)
			val id = reader.readString()
			val time = Time.unmarshal(stream)
			val status = VehicleStatusV1.of(reader.readUInt().toULong().toUInt())
			val isRealtime = reader.readBoolean()
			val vehicle = VehicleV3.unmarshal(stream)
			val boarding = reader.readU8()
			return DepartureV3(id, time, status, isRealtime, vehicle, boarding)
		}
	}
}

@Parcelize
data class StopV2(
	val code: String,
	val name: String,
	val nodeName: String,
	val zone: String,
	val feedID: String,
	val position: PositionV1,
	val changeOptions: List<ChangeOptionV1>
) : QueryableV2, Parcelable, LocatableV2, QueryableV3, LocatableV3 {
	companion object {
		fun unmarshal(stream: InputStream): StopV2 {
			val reader = Reader(stream)
			val code = reader.readString()
			val name = reader.readString()
			val nodeName = reader.readString()
			val zone = reader.readString()
			val feedID = reader.readString()
			val position = PositionV1.unmarshal(stream)
			val chOptionsNum = reader.readUInt().toULong()
			val changeOptions = mutableListOf<ChangeOptionV1>()
			for (i in 0UL until chOptionsNum) {
				changeOptions.add(ChangeOptionV1.unmarshal(stream))
			}
			return StopV2(
				name = name,
				nodeName = nodeName,
				code = code,
				zone = zone,
				position = position,
				feedID = feedID,
				changeOptions = changeOptions
			)
		}
	}
}

@Parcelize
data class StopV1(
	val code: String,
	val name: String,
	val zone: String,
	val position: PositionV1,
	val changeOptions: List<ChangeOptionV1>
) : QueryableV1, Parcelable, LocatableV1 {
	companion object {
		fun unmarshal(stream: InputStream): StopV1 {
			val reader = Reader(stream)
			val code = reader.readString()
			val name = reader.readString()
			val zone = reader.readString()
			val position = PositionV1.unmarshal(stream)
			val chOptionsNum = reader.readUInt().toULong()
			val changeOptions = mutableListOf<ChangeOptionV1>()
			for (i in 0UL until chOptionsNum) {
				changeOptions.add(ChangeOptionV1.unmarshal(stream))
			}
			return StopV1(
				name = name, code = code, zone = zone, position = position, changeOptions = changeOptions
			)
		}
	}
}

data class LineV1(
	val name: String,
	val colour: ColourV1,
	val type: LineTypeV2,
	val feedID: String,
	val headsigns: List<List<String>>,
	val graphs: List<LineGraph>,
) : QueryableV2 {
	override fun toString(): String {
		return "$name ($type) [$colour]\n${headsigns.map { "-> ${it.joinToString()}" }}"
	}

	companion object {
		fun unmarshal(stream: InputStream): LineV1 {
			val reader = Reader(stream)
			val name = reader.readString()
			val colour = ColourV1.unmarshal(stream)
			val type = reader.readUInt()
			val feedID = reader.readString()
			var directionsNum = reader.readUInt().toULong()
			val headsigns = (0UL until directionsNum).map {
				val headsignsNum = reader.readUInt().toULong()
				val headsignsDir = mutableListOf<String>()
				for (j in 0UL until headsignsNum) {
					headsignsDir.add(reader.readString())
				}
				headsignsDir
			}
			directionsNum = reader.readUInt().toULong()
			val graphs = mutableListOf<LineGraph>()
			for (i in 0UL until directionsNum) {
				graphs.add(LineGraph.unmarshal(stream))
			}
			return LineV1(
				name = name,
				colour = colour,
				type = LineTypeV2.of(type.toULong().toUInt()),
				feedID = feedID,
				headsigns = headsigns,
				graphs = graphs
			)
		}
	}
}

data class LineV2(
	val name: String,
	val colour: ColourV1,
	val type: LineTypeV3,
	val feedID: String,
	val headsigns: List<List<String>>,
	val graphs: List<LineGraph>,
) : QueryableV3 {
	override fun toString(): String {
		return "$name ($type) [$colour]\n${headsigns.map { "-> ${it.joinToString()}" }}"
	}

	companion object {
		fun unmarshal(stream: InputStream): LineV2 {
			val reader = Reader(stream)
			val name = reader.readString()
			val colour = ColourV1.unmarshal(stream)
			val type = reader.readUInt()
			val feedID = reader.readString()
			var directionsNum = reader.readUInt().toULong()
			val headsigns = (0UL until directionsNum).map {
				val headsignsNum = reader.readUInt().toULong()
				val headsignsDir = mutableListOf<String>()
				for (j in 0UL until headsignsNum) {
					headsignsDir.add(reader.readString())
				}
				headsignsDir
			}
			directionsNum = reader.readUInt().toULong()
			val graphs = mutableListOf<LineGraph>()
			for (i in 0UL until directionsNum) {
				graphs.add(LineGraph.unmarshal(stream))
			}
			return LineV2(
				name = name,
				colour = colour,
				type = LineTypeV3.of(type.toULong().toUInt()),
				feedID = feedID,
				headsigns = headsigns,
				graphs = graphs
			)
		}
	}
}

enum class LineTypeV1 {
	UNKNOWN, TRAM, BUS;

	companion object {
		fun of(type: UInt): LineTypeV1 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("TRAM")
				2u -> valueOf("BUS")
				else -> throw UnknownResourceVersionException("LineType/$type", 1u)
			}
		}
	}
}

enum class LineTypeV2 {
	UNKNOWN, TRAM, BUS, TROLLEYBUS;

	companion object {
		fun of(type: UInt): LineTypeV2 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("TRAM")
				2u -> valueOf("BUS")
				3u -> valueOf("TROLLEYBUS")
				else -> throw UnknownResourceVersionException("LineType/$type", 2u)
			}
		}
	}
}

enum class LineTypeV3 {
	UNKNOWN, TRAM, BUS, TROLLEYBUS, METRO, RAIL, FERRY, CABLE_TRAM, CABLE_CAR, FUNICULAR, MONORAIL;

	companion object {
		fun of(type: UInt): LineTypeV3 {
			return when (type) {
				0u -> valueOf("UNKNOWN")
				1u -> valueOf("TRAM")
				2u -> valueOf("BUS")
				3u -> valueOf("TROLLEYBUS")
				4u -> valueOf("METRO")
				5u -> valueOf("RAIL")
				6u -> valueOf("FERRY")
				7u -> valueOf("CABLE_TRAM")
				8u -> valueOf("CABLE_CAR")
				9u -> valueOf("FUNICULAR")
				10u -> valueOf("MONORAIL")
				else -> throw UnknownResourceVersionException("LineType/$type", 3u)
			}
		}
	}
}

@Parcelize
data class ChangeOptionV1(val line: String, val headsign: String) : Parcelable {
	companion object {
		fun unmarshal(stream: InputStream): ChangeOptionV1 {
			val reader = Reader(stream)
			return ChangeOptionV1(line = reader.readString(), headsign = reader.readString())
		}
	}
}

data class LineGraph(
	val stops: List<StopStub>, val nextNodes: Map<Long, List<Long>>
) {
	companion object {
		fun unmarshal(stream: InputStream): LineGraph {
			val reader = Reader(stream)
			val stopsNum = reader.readUInt().toULong()
			val stops = mutableListOf<StopStub>()
			for (i in 0UL until stopsNum) {
				stops.add(StopStub.unmarshal(stream))
			}
			val nextNodesNum = reader.readUInt().toULong()
			val nextNodes = mutableMapOf<Long, List<Long>>()
			for (i in 0UL until nextNodesNum) {
				val from = reader.readInt().toLong()
				val toNum = reader.readUInt().toULong()
				val to = mutableListOf<Long>()
				for (j in 0UL until toNum) {
					to.add(reader.readInt().toLong())
				}
				nextNodes[from] = to
			}

			return LineGraph(stops = stops, nextNodes = nextNodes)
		}
	}
}

data class StopStub(
	val code: String, val name: String, val nodeName: String, val zone: String, val onDemand: Boolean
) {
	companion object {
		fun unmarshal(stream: InputStream): StopStub {
			val reader = Reader(stream)
			return StopStub(
				code = reader.readString(),
				name = reader.readString(),
				nodeName = reader.readString(),
				zone = reader.readString(),
				onDemand = reader.readBoolean()
			)
		}
	}
}