diff --git a/build.zig b/build.zig index d84f455..76af5bf 100644 --- a/build.zig +++ b/build.zig @@ -9,10 +9,10 @@ const libscrapli_version = std.SemanticVersion{ const targets: []const std.Target.Query = &.{ .{ .cpu_arch = .aarch64, .os_tag = .macos }, - // .{ .cpu_arch = .aarch64, .os_tag = .linux }, - // .{ .cpu_arch = .x86_64, .os_tag = .macos }, - // .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }, - // .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .aarch64, .os_tag = .linux }, + .{ .cpu_arch = .x86_64, .os_tag = .macos }, + .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }, }; const all_examples: []const []const u8 = &.{ diff --git a/examples/basic-driver-usage/main.zig b/examples/basic-driver-usage/main.zig index ae05e56..99226d7 100644 --- a/examples/basic-driver-usage/main.zig +++ b/examples/basic-driver-usage/main.zig @@ -160,7 +160,7 @@ pub fn main() !void { const open_result = try d.open( allocator, - operation.NewOpenOptions(), + .{}, ); defer open_result.deinit(); @@ -177,7 +177,7 @@ pub fn main() !void { const send_input_result = try d.sendInput( allocator, "info interface *", - operation.NewSendInputOptions(), + .{}, ); defer send_input_result.deinit(); @@ -191,6 +191,6 @@ pub fn main() !void { }, ); - const close_result = try d.close(allocator, operation.NewCloseOptions()); + const close_result = try d.close(allocator, .{}); defer close_result.deinit(); } diff --git a/examples/basic-netconf-usage/main.zig b/examples/basic-netconf-usage/main.zig index 2cfe97f..a70ce12 100644 --- a/examples/basic-netconf-usage/main.zig +++ b/examples/basic-netconf-usage/main.zig @@ -118,7 +118,7 @@ pub fn main() !void { const open_result = try d.open( allocator, - operation.NewOpenOptions(), + .{}, ); defer open_result.deinit(); @@ -132,7 +132,7 @@ pub fn main() !void { }, ); - const get_result = try d.getConfig(allocator, operation.NewGetConfigOptions()); + const get_result = try d.getConfig(allocator, .{}); defer get_result.deinit(); std.debug.print( @@ -145,10 +145,9 @@ pub fn main() !void { }, ); - var get_options = operation.NewGetOptions(); - get_options.filter = ""; - - const get_config_result = try d.get(allocator, get_options); + const get_config_result = try d.get(allocator, .{ + .filter = "", + }); defer get_config_result.deinit(); std.debug.print( @@ -161,6 +160,6 @@ pub fn main() !void { }, ); - const close_result = try d.close(allocator, operation.NewCloseOptions()); + const close_result = try d.close(allocator, .{}); defer close_result.deinit(); } diff --git a/src/auth.zig b/src/auth.zig index a24f9b4..acb0724 100644 --- a/src/auth.zig +++ b/src/auth.zig @@ -27,25 +27,6 @@ pub const LookupKeyValue = struct { value: []const u8, }; -pub fn NewOptions(allocator: std.mem.Allocator) !*Options { - const o = try allocator.create(Options); - - o.* = Options{ - .allocator = allocator, - .username = null, - .password = null, - .private_key_path = null, - .private_key_passphrase = null, - .lookup_map = null, - .in_session_auth_bypass = false, - .username_pattern = default_username_pattern, - .password_pattern = default_password_pattern, - .passphrase_pattern = default_passphrase_pattern, - }; - - return o; -} - pub const OptionsInputs = struct { username: ?[]const u8 = null, password: ?[]const u8 = null, diff --git a/src/driver.zig b/src/driver.zig index 3de6169..fa86ad5 100644 --- a/src/driver.zig +++ b/src/driver.zig @@ -152,7 +152,7 @@ pub const Driver = struct { allocator: std.mem.Allocator, operation_kind: result.OperationKind, ) !*result.Result { - return result.NewResult( + return result.Result.init( allocator, self.host, self.port, @@ -166,7 +166,10 @@ pub const Driver = struct { allocator: std.mem.Allocator, options: operation.OpenOptions, ) !*result.Result { - var res = try self.NewResult(allocator, result.OperationKind.Open); + var res = try self.NewResult( + allocator, + result.OperationKind.open, + ); errdefer res.deinit(); try res.record( @@ -223,7 +226,10 @@ pub const Driver = struct { allocator: std.mem.Allocator, options: operation.CloseOptions, ) !*result.Result { - var res = try self.NewResult(allocator, result.OperationKind.Open); + var res = try self.NewResult( + allocator, + result.OperationKind.open, + ); errdefer res.deinit(); var op_buf = std.ArrayList(u8).init(allocator); @@ -267,7 +273,7 @@ pub const Driver = struct { var res = try self.NewResult( allocator, - result.OperationKind.GetPrompt, + result.OperationKind.get_prompt, ); errdefer res.deinit(); @@ -292,7 +298,7 @@ pub const Driver = struct { var res = try self.NewResult( allocator, - result.OperationKind.EnterMode, + result.OperationKind.enter_mode, ); errdefer res.deinit(); @@ -422,7 +428,7 @@ pub const Driver = struct { input: []const u8, options: operation.SendInputOptions, ) !*result.Result { - var res = try self.NewResult(allocator, result.OperationKind.SendInput); + var res = try self.NewResult(allocator, result.OperationKind.send_input); errdefer res.deinit(); var target_mode = options.requested_mode; @@ -471,12 +477,15 @@ pub const Driver = struct { var res = try self.NewResult( allocator, - result.OperationKind.SendInput, + result.OperationKind.send_input, ); errdefer res.deinit(); for (inputs) |input| { - try res.record(input, try self.session.sendInput(allocator, input, options)); + try res.record( + input, + try self.session.sendInput(allocator, input, options), + ); if (options.stop_on_indicated_failure and res.result_failure_indicated) { return res; @@ -494,7 +503,10 @@ pub const Driver = struct { response: []const u8, options: operation.SendPromptedInputOptions, ) !*result.Result { - var res = try self.NewResult(allocator, result.OperationKind.SendPromptedInput); + var res = try self.NewResult( + allocator, + result.OperationKind.send_prompted_input, + ); errdefer res.deinit(); var target_mode = options.requested_mode; diff --git a/src/ffi-driver-netconf.zig b/src/ffi-driver-netconf.zig index c198612..ec29a87 100644 --- a/src/ffi-driver-netconf.zig +++ b/src/ffi-driver-netconf.zig @@ -13,8 +13,8 @@ pub const OperationResult = struct { }; pub const OperationKind = enum { - Open, - GetConfig, + open, + get_config, }; pub const OpenOperation = struct { @@ -28,8 +28,8 @@ pub const GetConfigOperation = struct { }; pub const OperationOptions = union(OperationKind) { - Open: OpenOperation, - GetConfig: GetConfigOperation, + open: OpenOperation, + get_config: GetConfigOperation, }; pub fn NewFfiDriver( @@ -159,23 +159,23 @@ pub const FfiDriver = struct { var ret_err: ?anyerror = null; switch (op.?) { - OperationKind.Open => { - operation_id = op.?.Open.id; + OperationKind.open => |o| { + operation_id = o.id; ret_ok = self.real_driver.open( self.allocator, - op.?.Open.options, + o.options, ) catch |err| blk: { ret_err = err; break :blk null; }; }, - OperationKind.GetConfig => { - operation_id = op.?.GetConfig.id; + OperationKind.get_config => |o| { + operation_id = o.id; ret_ok = self.real_driver.getConfig( self.allocator, - op.?.GetConfig.options, + o.options, ) catch |err| blk: { ret_err = err; break :blk null; @@ -283,11 +283,11 @@ pub const FfiDriver = struct { }); switch (options) { - OperationKind.Open => { - mut_options.Open.id = operation_id; + OperationKind.open => { + mut_options.open.id = operation_id; }, - OperationKind.GetConfig => { - mut_options.GetConfig.id = operation_id; + OperationKind.get_config => { + mut_options.get_config.id = operation_id; }, } diff --git a/src/ffi.zig b/src/ffi.zig index 2399dd2..04f280b 100644 --- a/src/ffi.zig +++ b/src/ffi.zig @@ -554,7 +554,7 @@ export fn netconfOpenDriver( }; const _operation_id = d.queueOperation(ffi_driver_netconf.OperationOptions{ - .Open = ffi_driver_netconf.OpenOperation{ + .open = ffi_driver_netconf.OpenOperation{ .id = 0, .options = operation_netconf.OpenOptions{ .cancel = cancel, @@ -732,13 +732,12 @@ export fn netconfGetConfig( ) u8 { const d: *ffi_driver_netconf.FfiDriver = @ptrFromInt(d_ptr); - var opts = operation_netconf.NewGetConfigOptions(); - opts.cancel = cancel; - const _operation_id = d.queueOperation(ffi_driver_netconf.OperationOptions{ - .GetConfig = ffi_driver_netconf.GetConfigOperation{ + .get_config = ffi_driver_netconf.GetConfigOperation{ .id = 0, - .options = opts, + .options = .{ + .cancel = cancel, + }, }, }) catch |err| { d.real_driver.log.critical("error during queue getConfig {any}", .{err}); diff --git a/src/platform.zig b/src/platform.zig index 7e61155..a6d79b1 100644 --- a/src/platform.zig +++ b/src/platform.zig @@ -376,14 +376,14 @@ pub const YamlDefinition = struct { .bound_on_open_callback = if (parsed_definition.on_open_instructions) |instr| try BoundOnXCallback.init( allocator, - result.OperationKind.OnOpen, + result.OperationKind.on_open, instr, ) else null, .bound_on_close_callback = if (parsed_definition.on_close_instructions) |instr| try BoundOnXCallback.init( allocator, - result.OperationKind.OnClose, + result.OperationKind.on_close, instr, ) else null, .ntc_templates_platform = parsed_definition.ntc_templates_platform, diff --git a/src/result.zig b/src/result.zig index 06bcaa5..2b692dc 100644 --- a/src/result.zig +++ b/src/result.zig @@ -2,45 +2,16 @@ const std = @import("std"); const ascii = @import("ascii.zig"); pub const OperationKind = enum { - Open, - OnOpen, - OnClose, - Close, - EnterMode, - GetPrompt, - SendInput, - SendPromptedInput, + open, + on_open, + on_close, + close, + enter_mode, + get_prompt, + send_input, + send_prompted_input, }; -/// Returns a new Result object, this object *does not own* the failed_indicators arraylist -/// and will *not* free any of that memory! -pub fn NewResult( - allocator: std.mem.Allocator, - host: []const u8, - port: u16, - operation_kind: OperationKind, - failed_indicators: ?std.ArrayList([]const u8), -) !*Result { - const res = try allocator.create(Result); - - res.* = Result{ - .allocator = allocator, - .host = host, - .port = port, - .operation_kind = operation_kind, - .failed_indicators = failed_indicators, - .inputs = std.ArrayList([]const u8).init(allocator), - .results_raw = std.ArrayList([]const u8).init(allocator), - .results = std.ArrayList([]const u8).init(allocator), - .start_time_ns = std.time.nanoTimestamp(), - .splits_ns = std.ArrayList(i128).init(allocator), - .result_failure_indicated = false, - .result_failure_indicator = -1, - }; - - return res; -} - pub const Result = struct { allocator: std.mem.Allocator, @@ -65,6 +36,35 @@ pub const Result = struct { // memory to hold the substring, < 0 means no failure result_failure_indicator: i16, + /// Initializes a heap allocated Result object, this object *does not own* the failed_indicators + /// arraylist and will *not* free any of that memory! + pub fn init( + allocator: std.mem.Allocator, + host: []const u8, + port: u16, + operation_kind: OperationKind, + failed_indicators: ?std.ArrayList([]const u8), + ) !*Result { + const res = try allocator.create(Result); + + res.* = Result{ + .allocator = allocator, + .host = host, + .port = port, + .operation_kind = operation_kind, + .failed_indicators = failed_indicators, + .inputs = std.ArrayList([]const u8).init(allocator), + .results_raw = std.ArrayList([]const u8).init(allocator), + .results = std.ArrayList([]const u8).init(allocator), + .start_time_ns = std.time.nanoTimestamp(), + .splits_ns = std.ArrayList(i128).init(allocator), + .result_failure_indicated = false, + .result_failure_indicator = -1, + }; + + return res; + } + pub fn deinit( self: *Result, ) void {