Skip to content

Commit

Permalink
Simplify static error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl1337 committed Mar 7, 2024
1 parent fdd18c7 commit bc81744
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
34 changes: 8 additions & 26 deletions ckiwi/ckiwi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,44 +55,26 @@ const KiwiErr* wrap_err(F&& f) {
try {
f();
} catch (const UnsatisfiableConstraint& ex) {
static const constexpr KiwiErr err {
KiwiErrUnsatisfiableConstraint,
"The constraint cannot be satisfied."
};
static const constexpr KiwiErr err {KiwiErrUnsatisfiableConstraint};
return &err;
} catch (const UnknownConstraint& ex) {
static const constexpr KiwiErr err {
KiwiErrUnknownConstraint,
"The constraint has not been added to the solver."
};
static const constexpr KiwiErr err {KiwiErrUnknownConstraint};
return &err;

} catch (const DuplicateConstraint& ex) {
static const constexpr KiwiErr err {
KiwiErrDuplicateConstraint,
"The constraint has already been added to the solver."
};
static const constexpr KiwiErr err {KiwiErrDuplicateConstraint};
return &err;

} catch (const UnknownEditVariable& ex) {
static const constexpr KiwiErr err {
KiwiErrUnknownEditVar,
"The edit variable has not been added to the solver."
};
static const constexpr KiwiErr err {KiwiErrUnknownEditVar};
return &err;

} catch (const DuplicateEditVariable& ex) {
static const constexpr KiwiErr err {
KiwiErrDuplicateEditVar,
"The edit variable has already been added to the solver."
};
static const constexpr KiwiErr err {KiwiErrDuplicateEditVar};
return &err;

} catch (const BadRequiredStrength& ex) {
static const constexpr KiwiErr err {
KiwiErrBadRequiredStrength,
"A required strength cannot be used in this context."
};
static const constexpr KiwiErr err {KiwiErrBadRequiredStrength};
return &err;

} catch (const InternalSolverError& ex) {
Expand Down Expand Up @@ -161,9 +143,9 @@ void kiwi_str_release(char* str) {
std::free(str);
}

void kiwi_err_release(KiwiErr* err) {
void kiwi_err_release(const KiwiErr* err) {
if (lk_likely(err && err->must_release))
std::free(err);
std::free(const_cast<KiwiErr*>(err));
}

KiwiVar* kiwi_var_new(const char* name) {
Expand Down
2 changes: 1 addition & 1 deletion ckiwi/ckiwi.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct KiwiSolver;
LJKIWI_EXP void kiwi_solver_type_info(unsigned sz_align[2]);

LJKIWI_EXP void kiwi_str_release(char* str);
LJKIWI_EXP void kiwi_err_release(KiwiErr* err);
LJKIWI_EXP void kiwi_err_release(const KiwiErr* err);

LJKIWI_EXP KiwiVar* kiwi_var_new(const char* name);
LJKIWI_EXP void kiwi_var_release(KiwiVar* var);
Expand Down
23 changes: 18 additions & 5 deletions kiwi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef struct KiwiErr {
struct KiwiSolver;
void kiwi_str_release(char *);
void kiwi_err_release(KiwiErr *);
void kiwi_err_release(const KiwiErr *);
KiwiVar* kiwi_var_new(const char* name);
void kiwi_var_release(KiwiVar* var);
Expand Down Expand Up @@ -438,8 +438,7 @@ do

if RUST then
function Var_mt:__new(name)
--return ffi_gc(ljkiwi.kiwi_var_new(name)[0], ljkiwi.kiwi_var_release)
return ljkiwi.kiwi_var_new(name)[0]
return ffi_gc(ljkiwi.kiwi_var_new(name)[0], ljkiwi.kiwi_var_release)
end

--- Get the name of the variable.
Expand Down Expand Up @@ -1020,6 +1019,18 @@ do
}, Error_mt)
end

local ERR_MESSAGES = {
"The constraint cannot be satisfied.",
"The constraint has not been added to the solver.",
"The constraint has already been added to the solver.",
"The edit variable has not been added to the solver.",
"The edit variable has already been added to the solver.",
"A required strength cannot be used in this context.",
"An internal solver error occurred.",
"A memory allocation failed.",
"null object passed as argument.",
"An unknown error occurred.",
}
---@generic T
---@param f fun(solver: kiwi.Solver, item: T, ...): kiwi.KiwiErr?
---@param solver kiwi.Solver
Expand All @@ -1029,10 +1040,12 @@ do
local err = f(solver, item, ...)
if err ~= nil then
if err.must_release then
ffi_gc(err, ljkiwi.kiwi_error_release)
ffi_gc(err, ljkiwi.kiwi_err_release)
end
local kind = err.kind
local message = err.message ~= nil and ffi_string(err.message) or ""
local message = err.message ~= nil and ffi_string(err.message)
or ERR_MESSAGES[tonumber(err.kind)]
or ""
local errdata = new_error(kind, message, solver, item)
local error_mask = ljkiwi.kiwi_solver_get_error_mask(solver)
return item,
Expand Down

0 comments on commit bc81744

Please sign in to comment.