bootstrap/include/validator.h

78 lines
1.7 KiB
C
Raw Normal View History

/**
@file
@brief `spec.json` validator.
*/
2023-11-24 17:27:44 +00:00
#ifndef _BOOTSTRAP_VALIDATOR_H
#define _BOOTSTRAP_VALIDATOR_H
#include <stdbool.h>
2023-11-24 17:27:44 +00:00
#include "cJSON.h"
2023-11-26 13:30:44 +00:00
#include "config.h"
2023-11-24 17:27:44 +00:00
#include "dyn_array.h"
#include "error.h"
2023-11-24 17:27:44 +00:00
/**
@brief The types of fields `bootstrap` can handle.
*/
enum FieldType {
2023-11-27 12:03:05 +00:00
FT_LINE = 1,
2023-11-24 17:27:44 +00:00
};
/**
@brief A container for relevant fields of a `cJSON`-parsed field.
A @ref Field refers to the top-level key-value pairs found in the `spec.json`
file. For instance, the fields of:
```json
{
"abc": {
2023-11-27 12:03:05 +00:00
"type": "line",
2023-11-25 22:25:01 +00:00
"prompt": "ABC> "
},
"def": {
2023-11-27 12:03:05 +00:00
"type": "line",
2023-11-25 22:25:01 +00:00
"prompt": "DEF> "
},
}
```
are `abc` and `def`.
*/
struct Field {
/// @brief The type of field. Denotes what prompt should be displayed prior to
/// evaluation.
enum FieldType type;
/// @brief Indicates the field is required.
bool required;
/// @brief A reference to the name of the field. Does not take ownership of
/// this value.
2023-11-24 17:27:44 +00:00
const char *key;
/// @brief A reference to the supplied prompt. Does not take ownership of this
/// value.
const char *prompt;
2023-11-24 17:27:44 +00:00
};
/**
@brief Verify the `spec.json` file is formatted correctly.
2023-11-26 13:30:44 +00:00
@param config
A reference to the parameters describing the desired spec.
@param parsed
A possible null pointer to the parsed `spec.json` file. If null, this method
simply sets *fields to a null pointer.
@param fields
The array of `Field`s defined in the top-level `spec.json` file, provided
validation was successful.
@return
A null pointer if no error occurs. Otherwise an @ref Error pointer.
*/
struct Error *validate_spec_json(
2023-11-26 13:30:44 +00:00
const struct Config *const config,
const cJSON *const parsed,
struct DynArray **fields
2023-11-25 02:35:22 +00:00
);
2023-11-24 17:27:44 +00:00
#endif /* _BOOTSTRAP_VALIDATOR_H */