#include "livekit/rtc_error.h"
#include <iomanip>
#include <sstream>
#include <string>
namespace livekit_ffi {
RtcError to_error(const webrtc::RTCError& error) {
RtcError lk_error;
lk_error.error_detail = static_cast<RtcErrorDetailType>(error.error_detail());
lk_error.error_type = static_cast<RtcErrorType>(error.type());
lk_error.has_sctp_cause_code = error.sctp_cause_code().has_value();
lk_error.sctp_cause_code = error.sctp_cause_code().value_or(0);
lk_error.message = error.message();
return lk_error;
}
std::string serialize_error(const RtcError& error) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(8) << (uint32_t)error.error_type;
ss << std::setw(8) << (uint32_t)error.error_detail;
ss << std::setw(2) << (uint16_t)error.has_sctp_cause_code;
ss << std::setw(4) << (uint16_t)error.sctp_cause_code;
ss << std::dec << std::setw(1) << std::string(error.message);
return ss.str();
}
#ifdef LIVEKIT_TEST
rust::String serialize_deserialize() {
RtcError lk_error;
lk_error.error_type = RtcErrorType::InternalError;
lk_error.error_detail = RtcErrorDetailType::DataChannelFailure;
lk_error.has_sctp_cause_code = true;
lk_error.sctp_cause_code = 24;
lk_error.message = "this is not a test, I repeat, this is not a test";
return serialize_error(lk_error);
}
#endif
}