Rename loader -> parser.

pull/9/head
Joshua Potter 2023-11-24 09:09:18 -07:00
parent b43b1e8122
commit 1194d70975
5 changed files with 36 additions and 33 deletions

View File

@ -1,14 +1,14 @@
#ifndef _BOOTSTRAP_LOADER_H #ifndef _BOOTSTRAP_PARSER_H
#define _BOOTSTRAP_LOADER_H #define _BOOTSTRAP_PARSER_H
#include "cJSON.h" #include "cJSON.h"
#include "config.h" #include "config.h"
enum SpecParseError { enum SpecParseError {
// The `spec.json` file exists but cannot be open. // The `spec.json` file exists but cannot be open.
SPE_PARSE_CANNOT_OPEN = 1, SPE_CANNOT_OPEN = 1,
// The JSON of the corresponding file is not syntactically valid JSON. // The JSON of the corresponding file is not syntactically valid.
SPE_PARSE_INVALID, SPE_INVALID_SYNTAX,
}; };
/* /*
@ -20,6 +20,6 @@ cannot be found, the @parsed pointer is set to NULL with a success return code.
@return: 0 on success and a @SpecParseError otherwise. @return: 0 on success and a @SpecParseError otherwise.
*/ */
enum SpecParseError enum SpecParseError
read_spec_json(const struct Config *const config, cJSON **parsed); parse_spec_json(const struct Config *const config, cJSON **parsed);
#endif /* _BOOTSTRAP_LOADER_H */ #endif /* _BOOTSTRAP_PARSER_H */

10
main.c
View File

@ -4,13 +4,11 @@
#include "cJSON.h" #include "cJSON.h"
#include "config.h" #include "config.h"
#include "loader.h" #include "parser.h"
const char *ENV_BOOTSTRAP_ROOT_DIR = "BOOTSTRAP_ROOT_DIR"; const char *ENV_BOOTSTRAP_ROOT_DIR = "BOOTSTRAP_ROOT_DIR";
int main(int argc, char **argv) { int main(int argc, char **argv) {
int num = 0;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: bootstrap <spec>\n"); fprintf(stderr, "Usage: bootstrap <spec>\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -42,12 +40,12 @@ int main(int argc, char **argv) {
// `config` must be free'd. // `config` must be free'd.
cJSON *parsed = 0; cJSON *parsed = 0;
switch (read_spec_json(config, &parsed)) { switch (parse_spec_json(config, &parsed)) {
case SPE_PARSE_CANNOT_OPEN: case SPE_CANNOT_OPEN:
fprintf(stderr, "Found `spec.json` but could not open."); fprintf(stderr, "Found `spec.json` but could not open.");
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
goto config_cleanup; goto config_cleanup;
case SPE_PARSE_INVALID: case SPE_INVALID_SYNTAX:
fprintf(stderr, "`spec.json` does not conform to bootstrap format."); fprintf(stderr, "`spec.json` does not conform to bootstrap format.");
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
goto config_cleanup; goto config_cleanup;

View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "loader.h" #include "parser.h"
#include "path.h" #include "path.h"
/* /*
@ -28,12 +28,12 @@ static int find_spec_json(const struct Config *const config, FILE **handle) {
} }
enum SpecParseError enum SpecParseError
read_spec_json(const struct Config *const config, cJSON **parsed) { parse_spec_json(const struct Config *const config, cJSON **parsed) {
FILE *handle = 0; FILE *handle = 0;
int retval = find_spec_json(config, &handle); int retval = find_spec_json(config, &handle);
if (retval != 0) { if (retval != 0) {
return SPE_PARSE_CANNOT_OPEN; return SPE_CANNOT_OPEN;
} }
// The `spec.json` file does not exist. // The `spec.json` file does not exist.
@ -57,7 +57,7 @@ read_spec_json(const struct Config *const config, cJSON **parsed) {
// Can use `cJSON_GetErrorPtr()` to get the actual error message. // Can use `cJSON_GetErrorPtr()` to get the actual error message.
if (!*parsed) { if (!*parsed) {
return SPE_PARSE_INVALID; return SPE_INVALID_SYNTAX;
} }
return 0; return 0;

View File

@ -1,7 +1,7 @@
#include "sput.h" #include "sput.h"
#include "test_config.h" #include "test_config.h"
#include "test_dyn_array.h" #include "test_dyn_array.h"
#include "test_loader.h" #include "test_parser.h"
#include "test_path.h" #include "test_path.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -20,10 +20,10 @@ int main(int argc, char *argv[]) {
sput_run_test(test_join_path_single_segments); sput_run_test(test_join_path_single_segments);
sput_run_test(test_join_path_multiple_segments); sput_run_test(test_join_path_multiple_segments);
sput_enter_suite("loader"); sput_enter_suite("parser");
sput_run_test(test_read_spec_json_missing); sput_run_test(test_parse_spec_json_missing);
sput_run_test(test_read_spec_json_minimal); sput_run_test(test_parse_spec_json_minimal);
sput_run_test(test_read_spec_json_invalid); sput_run_test(test_parse_spec_json_invalid);
sput_finish_testing(); sput_finish_testing();

View File

@ -5,7 +5,7 @@
#include "cJSON.h" #include "cJSON.h"
#include "config.h" #include "config.h"
#include "loader.h" #include "parser.h"
#include "path.h" #include "path.h"
#include "sput.h" #include "sput.h"
@ -13,11 +13,12 @@
A missing `spec.json` file is not an error. Our parsed @cJSON instance should A missing `spec.json` file is not an error. Our parsed @cJSON instance should
be set to NULL in this case. be set to NULL in this case.
*/ */
static void test_read_spec_json_missing() { static void test_parse_spec_json_missing() {
char *cwd = getcwd(0, 0); char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"}; const char *segments[] = {cwd, "test", "specs"};
char *root_dir = join_path_segments(sizeof(segments) / sizeof(char *), segments); char *root_dir =
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Config config = { struct Config config = {
cwd, cwd,
@ -26,7 +27,7 @@ static void test_read_spec_json_missing() {
}; };
cJSON *parsed = 0; cJSON *parsed = 0;
enum SpecParseError retval = read_spec_json(&config, &parsed); enum SpecParseError retval = parse_spec_json(&config, &parsed);
sput_fail_unless(retval == 0, "no spec.json, success"); sput_fail_unless(retval == 0, "no spec.json, success");
sput_fail_unless(parsed == 0, "no spec.json, no parsed"); sput_fail_unless(parsed == 0, "no spec.json, no parsed");
@ -34,11 +35,12 @@ static void test_read_spec_json_missing() {
free(root_dir); free(root_dir);
} }
static void test_read_spec_json_minimal() { static void test_parse_spec_json_minimal() {
char *cwd = getcwd(0, 0); char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"}; const char *segments[] = {cwd, "test", "specs"};
char *root_dir = join_path_segments(sizeof(segments) / sizeof(char *), segments); char *root_dir =
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Config config = { struct Config config = {
cwd, cwd,
@ -47,7 +49,7 @@ static void test_read_spec_json_minimal() {
}; };
cJSON *parsed = 0; cJSON *parsed = 0;
enum SpecParseError retval = read_spec_json(&config, &parsed); enum SpecParseError retval = parse_spec_json(&config, &parsed);
sput_fail_unless(retval == 0, "minimal spec.json, success"); sput_fail_unless(retval == 0, "minimal spec.json, success");
sput_fail_unless(parsed != 0, "minimal spec.json, parsed"); sput_fail_unless(parsed != 0, "minimal spec.json, parsed");
@ -55,11 +57,12 @@ static void test_read_spec_json_minimal() {
free(root_dir); free(root_dir);
} }
static void test_read_spec_json_invalid() { static void test_parse_spec_json_invalid() {
char *cwd = getcwd(0, 0); char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"}; const char *segments[] = {cwd, "test", "specs"};
char *root_dir = join_path_segments(sizeof(segments) / sizeof(char *), segments); char *root_dir =
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Config config = { struct Config config = {
cwd, cwd,
@ -68,8 +71,10 @@ static void test_read_spec_json_invalid() {
}; };
cJSON *parsed = 0; cJSON *parsed = 0;
enum SpecParseError retval = read_spec_json(&config, &parsed); enum SpecParseError retval = parse_spec_json(&config, &parsed);
sput_fail_unless(retval == SPE_PARSE_INVALID, "invalid spec.json, JSON_INVALID"); sput_fail_unless(
retval == SPE_INVALID_SYNTAX, "invalid spec.json, INVALID_SYNTAX"
);
sput_fail_unless(parsed == 0, "invalid spec.json, not parsed"); sput_fail_unless(parsed == 0, "invalid spec.json, not parsed");
free(cwd); free(cwd);