magpie/API/codes.cpp

65 lines
2.1 KiB
C++

// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "codes.h"
#include <iostream>
constexpr std::array<const char*, uint8_t(Codes::Register::unknownError) + 1> registerErrors({
"Successfully registered an account",
"Login have not been provided",
"The login can not be empty",
"User with provided login already exists",
"Provided login doesn't comply with server policies",
"Password have not been provided",
"Password can not be empty",
"Provided password doesn't comply with server policies",
"Unknown registration error"
});
constexpr std::array<const char*, uint8_t(Codes::Login::unknownError) + 1> loginErrors({
"Successfully logged in",
"Login have not been provided",
"The login can not be empty",
"Password have not been provided",
"Password can not be empty",
"Provided login or (and) password are wrong",
"Unknown login error"
});
Codes::Register Codes::convertRegister (int source) {
if (source < 0) {
std::cerr << "Converting Register response code which is smaller than zero, falling back to unknownError" << std::endl;
return Register::unknownError;
}
if (source > (int)Register::unknownError) {
std::cerr << "Converting Register response code which is bigger than biggest known, falling back to unknownError" << std::endl;
return Register::unknownError;
}
return static_cast<Register>(source);
}
QString Codes::description(Register code) {
return registerErrors[(uint8_t)code];
}
Codes::Login Codes::convertLogin(int source) {
if (source < 0) {
std::cerr << "Converting Login response code which is smaller than zero, falling back to unknownError" << std::endl;
return Login::unknownError;
}
if (source > (int)Login::unknownError) {
std::cerr << "Converting Login response code which is bigger than biggest known, falling back to unknownError" << std::endl;
return Login::unknownError;
}
return static_cast<Login>(source);
}
QString Codes::description(Login code) {
return loginErrors[(uint8_t)code];
}