Bimba.git

ref: 459b6294c1a664dfb1350d50e88013f5852028da

app/src/main/java/ml/adamsprogs/bimba/api/Responses.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
package ml.adamsprogs.bimba.api

import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream

interface DeparturesResponse {
	companion object {
		fun printable(it: String) = it.map {
			when (Character.getType(it).toByte()) {
				Character.CONTROL, Character.FORMAT, Character.PRIVATE_USE,
				Character.SURROGATE, Character.UNASSIGNED, Character.OTHER_SYMBOL
				-> "\\u%04x".format(it.code)
				else -> it.toString()
			}
		}.joinToString("")

		fun unmarshall(stream: InputStream): DeparturesResponse {
			val reader = Reader(stream)
			when (reader.readUInt()) {
				0UL -> {
					TODO("error response")
				}
				1UL -> {
					return DeparturesSuccess.unmarshall(stream)
				}
				else -> {
					TODO("throw unknown tag")
				}
			}
		}
	}
}

data class DeparturesSuccess(
	val alerts: List<Alert>,
	val departures: List<Departure>,
	val stop: Stop
) : DeparturesResponse {
	companion object {
		fun unmarshall(stream: InputStream): DeparturesSuccess {
			val alerts = mutableListOf<Alert>()
			val departures = mutableListOf<Departure>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt()
			for (i in 0UL until alertsNum) {
				val alert = Alert.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt()
			for (i in 0UL until departuresNum) {
				val departure = Departure.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesSuccess(alerts, departures, Stop.unmarshal(stream))
		}
	}
}

interface ItemsResponse {
	companion object {
		fun unmarshall(stream: InputStream): ItemsResponse {
			val reader = Reader(stream)
			when (reader.readUInt()) {
				0UL -> {
					TODO("error response")
				}
				1UL -> {
					return ItemsSuccess.unmarshall(stream)
				}
				else -> {
					TODO("throw unknown tag")
				}
			}
		}
	}
}

data class ItemsSuccess(val items: List<Item>) : ItemsResponse {
	companion object {
		fun unmarshall(stream: InputStream): ItemsSuccess {
			val items = mutableListOf<Item>()
			val reader = Reader(stream)
			val itemsNum = reader.readUInt()
			for (i in 0UL until itemsNum) {
				when (reader.readUInt()) {
					0UL -> {
						items.add(Stop.unmarshal(stream))
					}
					1UL -> {
						items.add(Line.unmarshal(stream))
					}
					else -> {
						TODO("throw unknown tag")
					}
				}
			}
			return ItemsSuccess(items)
		}
	}
}

interface FeedsResponse {
	companion object {
		fun unmarshal(stream: InputStream): FeedsResponse {
			val reader = Reader(stream)
			when (reader.readUInt()) {
				0UL -> {
					TODO("error response")
				}
				1UL -> {
					return FeedsSuccess.unmarshal(stream)
				}
				else -> {
					TODO("throw unknown tag")
				}
			}
		}
	}
}

data class FeedsSuccess(
	val adminContact: String,
	val rateLimited: Boolean,
	val private: Boolean,
	val feeds: List<FeedInfo>
) : FeedsResponse {
	companion object {
		fun unmarshal(stream: InputStream): FeedsSuccess {
			val feeds = mutableListOf<FeedInfo>()
			val reader = Reader(stream)
			val adminContact = reader.readString()
			val rateLimited = reader.readBoolean()
			val private = reader.readBoolean()
			val itemsNum = reader.readUInt()
			for (i in 0UL until itemsNum) {
				feeds.add(FeedInfo.unmarshal(stream))
			}
			return FeedsSuccess(adminContact, rateLimited, private, feeds)
		}
	}
}

data class Error(val message: String) : ItemsResponse, DeparturesResponse {

}