Ji-Xinyou commented on code in PR #2381:
URL:
https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211732248
##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
// specific language governing permissions and limitations
// under the License.
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
- const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
- const OpendalBDDTest = struct {
- p: opendal.opendal_operator_ptr,
- scheme: c_str,
- path: c_str,
- content: c_str,
-
- pub fn init() Self {
- var self: Self = undefined;
- self.scheme = "memory";
- self.path = "test";
- self.content = "Hello, World!";
-
- var options: opendal.opendal_operator_options =
opendal.opendal_operator_options_new();
- defer opendal.opendal_operator_options_free(&options);
- opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
- // Given A new OpenDAL Blocking Operator
- self.p = opendal.opendal_operator_new(self.scheme, &options);
- std.debug.assert(self.p.ptr != null);
-
- return self;
- }
-
- pub fn deinit(self: *Self) void {
- opendal.opendal_operator_free(&self.p);
- }
-
- const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+ OK = c.OPENDAL_OK,
+ ERROR = c.OPENDAL_ERROR,
+ UNEXPECTED = c.OPENDAL_UNEXPECTED,
+ UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+ CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+ NOT_FOUND = c.OPENDAL_NOT_FOUND,
+ PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+ IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+ NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+ ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+ RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+ IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+ Unexpected,
+ Unsupported,
+ ConfigInvalid,
+ NotFound,
+ PermissionDenied,
+ IsDirectory,
+ IsNotDirectory,
+ AlreadyExists,
+ RateLimited,
+ IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+ return switch (code) {
+ c.OPENDAL_UNEXPECTED => error.Unexpected,
+ c.OPENDAL_UNSUPPORTED => error.Unsupported,
+ c.OPENDAL_NOT_FOUND => error.NotFound,
+ c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+ c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+ c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+ c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+ c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+ c.OPENDAL_RATE_LIMITED => error.RateLimited,
+ c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+ else => c.OPENDAL_ERROR,
Review Comment:
In C binding, the unknown error leads to panic, you can see it
[here](https://github.com/apache/incubator-opendal/blob/main/bindings/c/src/error.rs)
since the `opendal::Error` represents all the errors that could happen. Any
ideas? @Xuanwo
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]