Use COUNT member in enums

This commit is contained in:
Camden Dixie O'Brien 2025-03-03 11:17:52 +00:00
parent e81a47d83f
commit c8f2dbaadc
2 changed files with 9 additions and 5 deletions

View File

@ -7,7 +7,9 @@ namespace StudySystemClient {
public enum ActivityType {
READING = 0,
EXERCISES = 1;
EXERCISES = 1,
COUNT;
public string to_string() {
switch (this) {

View File

@ -67,7 +67,9 @@ namespace StudySystemClient.Response {
public enum Value {
INVALID_REQUEST = 0,
INVALID_ARGUMENTS = 1,
SERVER_ERROR = 2;
SERVER_ERROR = 2,
COUNT;
public string to_string() {
switch (this) {
@ -91,9 +93,9 @@ namespace StudySystemClient.Response {
throw new DecodeError.INVALID_BODY(
"Error was not an ENUMERATED");
}
if (enumerated.value < 0 || enumerated.value > 2) {
if (enumerated.value < 0 || enumerated.value >= Value.COUNT) {
throw new DecodeError.INVALID_BODY(
"Error type was not in range 0..2");
"Error type was not in range 0..$(Value.COUNT)");
}
value = (Value)enumerated.value;
}
@ -162,7 +164,7 @@ namespace StudySystemClient.Response {
string name, Der.Datum datum) throws DecodeError {
if (datum is Der.Enumerated) {
var value = datum.value;
if (0 <= value <= 1)
if (0 <= value < ActivityType.COUNT)
return (ActivityType)value;
throw new DecodeError.INVALID_BODY(
"Invalid value for ActivityType: %lld", value);