diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-07 14:35:57 -0400 |
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-07 14:35:57 -0400 |
| commit | cd852acd7e1feb5c35a37f9365e1e1b4658ca2f7 (patch) | |
| tree | 5bdf36fa44d6efcb4abd51df27465260174c9ee7 | |
| parent | 3828e54c2c85fa377fb27f38ffd609940f5f19f8 (diff) | |
| download | stasis-cd852acd7e1feb5c35a37f9365e1e1b4658ca2f7.tar.gz | |
Add enum for wheel_package error handling
| -rw-r--r-- | src/lib/core/include/wheel.h | 32 | ||||
| -rw-r--r-- | src/lib/core/wheel.c | 19 | ||||
| -rw-r--r-- | tests/test_wheel.c | 8 |
3 files changed, 49 insertions, 10 deletions
diff --git a/src/lib/core/include/wheel.h b/src/lib/core/include/wheel.h index 5831f46..2b3d5f1 100644 --- a/src/lib/core/include/wheel.h +++ b/src/lib/core/include/wheel.h @@ -176,8 +176,40 @@ struct WheelValue { void *data; }; +enum { + WHEEL_PACKAGE_E_SUCCESS=0, + WHEEL_PACKAGE_E_FILENAME=-1, + WHEEL_PACKAGE_E_ALLOC=-2, + WHEEL_PACKAGE_E_GET=-3, + WHEEL_PACKAGE_E_GET_METADATA=-4, + WHEEL_PACKAGE_E_GET_TOP_LEVEL=-5, + WHEEL_PACKAGE_E_GET_RECORDS=-6, + WHEEL_PACKAGE_E_GET_ENTRY_POINT=-7, +}; + +/** + * Populate a `Wheel` structure using a Python wheel file as input. + * + * @param pkg pointer to a `Wheel` (may be initialized to `NULL`) + * @param filename path to a Python wheel file + * @return a WHEEL_PACKAGE_E_ error code + */ int wheel_package(struct Wheel **pkg, const char *filename); + +/** + * Frees a `Wheel` structure + * @param pkg pointer to an initialized `Wheel` + */ void wheel_package_free(struct Wheel **pkg); + + +/** + * Get wheel data by name + * @param pkg pointer to an initialized `Wheel` + * @param from + * @param key + * @return + */ struct WheelValue wheel_get_value_by_name(const struct Wheel *pkg, int from, const char *key); struct WheelValue wheel_get_value_by_id(const struct Wheel *pkg, int from, ssize_t id); int wheel_value_error(struct WheelValue const *val); diff --git a/src/lib/core/wheel.c b/src/lib/core/wheel.c index 1bce806..78209f1 100644 --- a/src/lib/core/wheel.c +++ b/src/lib/core/wheel.c @@ -1314,42 +1314,43 @@ int wheel_show_info(const struct Wheel *wheel) { } return 0; } + int wheel_package(struct Wheel **pkg, const char *filename) { if (!filename) { - return -1; + return WHEEL_PACKAGE_E_FILENAME; } if (!*pkg) { *pkg = calloc(1, sizeof(**pkg)); if (!*pkg) { - return -1; + return WHEEL_PACKAGE_E_ALLOC; } (*pkg)->metadata = calloc(1, sizeof(*(*pkg)->metadata)); if (!(*pkg)->metadata) { guard_free(*pkg); - return -1; + return WHEEL_PACKAGE_E_ALLOC; } } if (wheel_get(pkg, filename) < 0) { - return -1; + return WHEEL_PACKAGE_E_GET; } if (wheel_metadata_get(*pkg, filename) < 0) { - return -1; + return WHEEL_PACKAGE_E_GET_METADATA; } if (wheel_get_top_level(*pkg, filename) < 0) { - return -1; + return WHEEL_PACKAGE_E_GET_TOP_LEVEL; } if (wheel_get_records(*pkg, filename) < 0) { - return -1; + return WHEEL_PACKAGE_E_GET_RECORDS; } if (wheel_get_entry_point(*pkg, filename) < 0) { - return -1; + return WHEEL_PACKAGE_E_GET_ENTRY_POINT; } // Optional marker wheel_get_zip_safe(*pkg, filename); - return 0; + return WHEEL_PACKAGE_E_SUCCESS; } diff --git a/tests/test_wheel.c b/tests/test_wheel.c index ee089c6..089bac1 100644 --- a/tests/test_wheel.c +++ b/tests/test_wheel.c @@ -14,7 +14,13 @@ static const char *testpkg_filename = "testpkg/dist/testpkg-1.0.0-py3-none-any.w static void test_wheel_package() { const char *filename = testpkg_filename; struct Wheel *wheel = NULL; - wheel_package(&wheel, filename); + int state = wheel_package(&wheel, filename); + STASIS_ASSERT(state != WHEEL_PACKAGE_E_ALLOC, "Cannot fail to allocate memory for package structure"); + STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET, "Cannot fail to parse wheel"); + STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_METADATA, "Cannot fail to read wheel metadata"); + STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_RECORDS, "Cannot fail reading wheel path records"); + STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_ENTRY_POINT, "Cannot fail reading wheel entry points"); + STASIS_ASSERT(state == WHEEL_PACKAGE_E_SUCCESS, "Wheel file should be usable"); STASIS_ASSERT(wheel != NULL, "wheel cannot be NULL"); STASIS_ASSERT(wheel != NULL, "wheel_package failed to initialize wheel struct"); STASIS_ASSERT(wheel->record != NULL, "Record cannot be NULL"); |
