Lightweight 0.20251202.0
Loading...
Searching...
No Matches
ZipError.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include <cstdint>
6#include <format>
7#include <string>
8
9#if defined(__clang__)
10 #pragma clang diagnostic push
11 #pragma clang diagnostic ignored "-Wnullability-extension"
12#endif
13#include <zip.h>
14#if defined(__clang__)
15 #pragma clang diagnostic pop
16#endif
17
18namespace Lightweight::Zip
19{
20
21/// Error codes for ZIP archive operations.
22enum class ZipErrorCode : uint8_t
23{
24 OpenFailed, ///< Failed to open the archive
25 CloseFailed, ///< Failed to close the archive
26 EntryNotFound, ///< Requested entry was not found
27 ReadFailed, ///< Failed to read from archive or entry
28 WriteFailed, ///< Failed to write to archive
29 SourceCreationFailed, ///< Failed to create a ZIP source for adding data
30};
31
32/// Represents an error that occurred during a ZIP operation.
34{
35 ZipErrorCode code {}; ///< The error code
36 int libzipError {}; ///< Original libzip error code (0 if not from libzip)
37 std::string message; ///< Human-readable error message
38
39 /// Creates a ZipError from the current error state of a ZIP archive.
40 ///
41 /// @param zip The ZIP archive handle to extract the error from.
42 /// @param code The error code to assign.
43 /// @return A ZipError populated with the libzip error information.
44 [[nodiscard]] static ZipError FromArchive(zip_t* zip, ZipErrorCode code)
45 {
46 zip_error_t* error = zip_get_error(zip);
47 return ZipError {
48 .code = code,
49 .libzipError = zip_error_code_zip(error),
50 .message = zip_error_strerror(error),
51 };
52 }
53
54 /// Creates a ZipError from an open error code.
55 ///
56 /// @param errorCode The libzip error code from zip_open.
57 /// @param code The error code to assign.
58 /// @return A ZipError populated with the error information.
59 [[nodiscard]] static ZipError FromOpenError(int errorCode, ZipErrorCode code)
60 {
61 zip_error_t error;
62 zip_error_init_with_code(&error, errorCode);
63 auto message = std::string(zip_error_strerror(&error));
64 zip_error_fini(&error);
65 return ZipError {
66 .code = code,
67 .libzipError = errorCode,
68 .message = std::move(message),
69 };
70 }
71
72 /// Creates a custom ZipError with a user-defined message.
73 ///
74 /// @param code The error code.
75 /// @param msg The error message.
76 /// @return A ZipError with the specified code and message.
77 [[nodiscard]] static ZipError Custom(ZipErrorCode code, std::string msg)
78 {
79 return ZipError {
80 .code = code,
81 .libzipError = 0,
82 .message = std::move(msg),
83 };
84 }
85};
86
87} // namespace Lightweight::Zip
88
89template <>
90struct std::formatter<Lightweight::Zip::ZipErrorCode>: std::formatter<std::string_view>
91{
92 auto format(Lightweight::Zip::ZipErrorCode code, format_context& ctx) const -> format_context::iterator
93 {
94 using Lightweight::Zip::ZipErrorCode;
95 std::string_view name;
96 switch (code)
97 {
98 case ZipErrorCode::OpenFailed:
99 name = "OpenFailed";
100 break;
101 case ZipErrorCode::CloseFailed:
102 name = "CloseFailed";
103 break;
104 case ZipErrorCode::EntryNotFound:
105 name = "EntryNotFound";
106 break;
107 case ZipErrorCode::ReadFailed:
108 name = "ReadFailed";
109 break;
110 case ZipErrorCode::WriteFailed:
111 name = "WriteFailed";
112 break;
113 case ZipErrorCode::SourceCreationFailed:
114 name = "SourceCreationFailed";
115 break;
116 }
117 return std::formatter<std::string_view>::format(name, ctx);
118 }
119};
120
121template <>
122struct std::formatter<Lightweight::Zip::ZipError>: std::formatter<std::string>
123{
124 auto format(Lightweight::Zip::ZipError const& error, format_context& ctx) const -> format_context::iterator
125 {
126 return std::formatter<std::string>::format(
127 std::format("{}: {} (libzip error: {})", error.code, error.message, error.libzipError), ctx);
128 }
129};
Represents an error that occurred during a ZIP operation.
Definition ZipError.hpp:34
ZipErrorCode code
The error code.
Definition ZipError.hpp:35
static ZipError FromOpenError(int errorCode, ZipErrorCode code)
Definition ZipError.hpp:59
std::string message
Human-readable error message.
Definition ZipError.hpp:37
static ZipError Custom(ZipErrorCode code, std::string msg)
Definition ZipError.hpp:77
static ZipError FromArchive(zip_t *zip, ZipErrorCode code)
Definition ZipError.hpp:44
int libzipError
Original libzip error code (0 if not from libzip)
Definition ZipError.hpp:36