Skip to content

Commit

Permalink
Simplify error handling
Browse files Browse the repository at this point in the history
No need for message strings bc only 2 simple errors can occure

Signed-off-by: Dylan Turner <dylan.turner@tutanota.com>
  • Loading branch information
blueOkiris committed Oct 3, 2024
1 parent 4d3e4bb commit c66b018
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 371 deletions.
35 changes: 4 additions & 31 deletions examples/hworld/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,17 @@ int main(int argc, char **argv) {

string_result_t new_string_res = string_from("Hello, world!", CHAR_DEF_ALLOC);
if (new_string_res.is_err) {
switch (new_string_res.err.code) {
case PKLSTR_ERR_ALLOC_FAILED:
fprintf(stderr, "Error allocating space for string_t\n");
return 1;
default:
fprintf(
stderr, "An unhandled error occurred: %lu",
(size_t) new_string_res.err.code
);
if (new_string_res.err.msg) {
fprintf(stderr, " - %s", new_string_res.err.msg);
}
fprintf(stderr, "\n");
return 1;
}
fprintf(stderr, "Error occurred: %lu\n", (size_t) new_string_res.err);
return 1;
}
string_t new_string = new_string_res.ok;
printf("English: %s\n", new_string.str);
string_free(&new_string);

wstring_result_t new_wstring_res = wstring_from(L"こんにちは世界!", WCHAR_DEF_ALLOC);
if (new_wstring_res.is_err) {
printf("Error!\n");
switch (new_wstring_res.err.code) {
case PKLSTR_ERR_ALLOC_FAILED:
fprintf(stderr, "Error allocating space for wstring_t\n");
return 1;
default:
fprintf(
stderr, "An unhandled error occurred: %lu",
(size_t) new_wstring_res.err.code
);
if (new_wstring_res.err.msg) {
fprintf(stderr, " - %s", new_wstring_res.err.msg);
}
fprintf(stderr, "\n");
return 1;
}
fprintf(stderr, "Error occurred: %lu\n", (size_t) new_string_res.err);
return 1;
}
wstring_t new_wstring = new_wstring_res.ok;
printf("Japanese: %ls\n", new_wstring.str);
Expand Down
121 changes: 33 additions & 88 deletions examples/test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ int main(int argc, char **argv) {
printf("Testing allocation...\n");
string_result_t string_res = string_from("Hello, world", CHAR_DEF_ALLOC);
if (string_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string_res.err.code);
if (string_res.err.msg) {
fprintf(stderr, " - %s", string_res.err.msg);
}
fprintf(stderr, "\n");
fprintf(stderr, "Error occurred: %lu\n", (size_t) string_res.err);
return 1;
}
string_t string = string_res.ok;
Expand All @@ -21,13 +17,9 @@ int main(int argc, char **argv) {
);

printf("Testing char append...\n");
result_t char_append_res = string_append_char(&string, '!');
if (char_append_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) char_append_res.err.code);
if (string_res.err.msg) {
fprintf(stderr, " - %s", char_append_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t char_append_res = string_append_char(&string, '!');
if (char_append_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) char_append_res.some);
return 1;
}
printf(
Expand All @@ -36,13 +28,9 @@ int main(int argc, char **argv) {
);

printf("Testing str append (no resize)...\n");
result_t str_append_res1 = string_append_str(&string, " More!");
if (str_append_res1.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) str_append_res1.err.code);
if (str_append_res1.err.msg) {
fprintf(stderr, " - %s", str_append_res1.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t str_append_res1 = string_append_str(&string, " More!");
if (str_append_res1.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) str_append_res1.some);
return 1;
}
printf(
Expand All @@ -51,13 +39,9 @@ int main(int argc, char **argv) {
);

printf("Testing str append (double)...\n");
result_t str_append_res2 = string_append_str(&string, " More!");
if (str_append_res2.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) str_append_res2.err.code);
if (str_append_res2.err.msg) {
fprintf(stderr, " - %s", str_append_res2.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t str_append_res2 = string_append_str(&string, " More!");
if (str_append_res2.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) str_append_res2.some);
return 1;
}
printf(
Expand All @@ -66,13 +50,10 @@ int main(int argc, char **argv) {
);

printf("Testing str append (more than double)...\n");
result_t str_append_res3 = string_append_str(&string, " Way Way Way Way Way Way More!");
if (str_append_res3.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) str_append_res3.err.code);
if (str_append_res3.err.msg) {
fprintf(stderr, " - %s", str_append_res3.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t str_append_res3 =
string_append_str(&string, " Way Way Way Way Way Way More!");
if (str_append_res3.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) str_append_res3.some);
return 1;
}
printf(
Expand All @@ -83,21 +64,13 @@ int main(int argc, char **argv) {
printf("Testing string append...\n");
string_result_t string_res2 = string_from(" Even More", CHAR_DEF_ALLOC);
if (string_res2.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string_res2.err.code);
if (string_res2.err.msg) {
fprintf(stderr, " - %s", string_res2.err.msg);
}
fprintf(stderr, "\n");
fprintf(stderr, "Error occurred: %lu\n", (size_t) string_res2.err);
return 1;
}
string_t string2 = string_res2.ok;
result_t string_append_res = string_append_string(&string, &string2);
if (string_append_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string_append_res.err.code);
if (string_append_res.err.msg) {
fprintf(stderr, " - %s", string_append_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t string_append_res = string_append_string(&string, &string2);
if (string_append_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) string_append_res.some);
return 1;
}
string_free(&string2);
Expand All @@ -110,11 +83,7 @@ int main(int argc, char **argv) {
printf("Resetting to a new string...\n");
string_result_t string2_res = string_from("Hello world!", CHAR_DEF_ALLOC);
if (string2_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string2_res.err.code);
if (string2_res.err.msg) {
fprintf(stderr, " - %s", string2_res.err.msg);
}
fprintf(stderr, "\n");
fprintf(stderr, "Error occurred: %lu\n", (size_t) string2_res.err);
return 1;
}
string_t string3 = string2_res.ok;
Expand All @@ -124,13 +93,9 @@ int main(int argc, char **argv) {
);

printf("Inserting a char...\n");
result_t char_ins_res = string_insert_char_at(&string3, ',', 5);
if (char_ins_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) char_ins_res.err.code);
if (char_ins_res.err.msg) {
fprintf(stderr, " - %s", char_ins_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t char_ins_res = string_insert_char_at(&string3, ',', 5);
if (char_ins_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) char_ins_res.some);
return 1;
}
printf(
Expand All @@ -139,13 +104,9 @@ int main(int argc, char **argv) {
);

printf("Inserting a str...\n");
result_t str_ins_res = string_insert_str_at(&string3, " beautiful", 6);
if (str_ins_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) str_ins_res.err.code);
if (str_ins_res.err.msg) {
fprintf(stderr, " - %s", str_ins_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t str_ins_res = string_insert_str_at(&string3, " beautiful", 6);
if (str_ins_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) str_ins_res.some);
return 1;
}
printf(
Expand All @@ -156,21 +117,13 @@ int main(int argc, char **argv) {
printf("Inserting a string...\n");
string_result_t string4_res = string_from("It's a great day! ", CHAR_DEF_ALLOC);
if (string4_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string4_res.err.code);
if (string4_res.err.msg) {
fprintf(stderr, " - %s", string4_res.err.msg);
}
fprintf(stderr, "\n");
fprintf(stderr, "Error occurred: %lu\n", (size_t) string4_res.err);
return 1;
}
string_t string4 = string4_res.ok;
result_t string_ins_res = string_insert_string_at(&string3, &string4, 0);
if (string_ins_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) string_ins_res.err.code);
if (string_ins_res.err.msg) {
fprintf(stderr, " - %s", string_ins_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t string_ins_res = string_insert_string_at(&string3, &string4, 0);
if (string_ins_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) string_ins_res.some);
return 1;
}
string_free(&string4);
Expand All @@ -180,13 +133,9 @@ int main(int argc, char **argv) {
);

printf("Removing a piece...\n");
result_t rm_res = string_remove_at(&string3, 24, 10);
if (rm_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) rm_res.err.code);
if (rm_res.err.msg) {
fprintf(stderr, " - %s", rm_res.err.msg);
}
fprintf(stderr, "\n");
pklstr_err_option_t rm_res = string_remove_at(&string3, 24, 10);
if (rm_res.is_some) {
fprintf(stderr, "Error occurred: %lu\n", (size_t) rm_res.some);
return 1;
}
printf(
Expand All @@ -197,11 +146,7 @@ int main(int argc, char **argv) {
printf("Grab a substring...\n");
string_result_t sub_res = string_substring(&string3, 18, 5);
if (sub_res.is_err) {
fprintf(stderr, "Error occurred: %lu", (size_t) sub_res.err.code);
if (sub_res.err.msg) {
fprintf(stderr, " - %s", sub_res.err.msg);
}
fprintf(stderr, "\n");
fprintf(stderr, "Error occurred: %lu\n", (size_t) sub_res.err);
return 1;
}
string_free(&string3);
Expand Down
Loading

0 comments on commit c66b018

Please sign in to comment.