magpie/API/codes.cpp

91 lines
3.0 KiB
C++
Raw Normal View History

// 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"
});
2023-12-25 20:07:51 +00:00
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"
});
constexpr std::array<const char*, uint8_t(Codes::Poll::unknownError) + 1> pollErrors({
"New data from poll",
"Couldn't authorize polling: token error",
"Other device was authorized with the same token",
"Poll timeout, no new data",
"Unknown poll 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];
}
2023-12-25 20:07:51 +00:00
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];
}
Codes::Poll Codes::convertPoll(int source) {
if (source < 0) {
std::cerr << "Converting Poll response code which is smaller than zero, falling back to unknownError" << std::endl;
return Poll::unknownError;
}
if (source > (int)Poll::unknownError) {
std::cerr << "Converting Poll response code which is bigger than biggest known, falling back to unknownError" << std::endl;
return Poll::unknownError;
}
return static_cast<Poll>(source);
}
QString Codes::description(Poll code) {
return pollErrors[(uint8_t)code];
}