Skip to content

Commit

Permalink
chore: fixing more ename cases, removing "New" funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Mar 9, 2025
1 parent c8664f7 commit 52b4829
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 101 deletions.
8 changes: 4 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &.{
Expand Down
6 changes: 3 additions & 3 deletions examples/basic-driver-usage/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn main() !void {

const open_result = try d.open(
allocator,
operation.NewOpenOptions(),
.{},
);
defer open_result.deinit();

Expand All @@ -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();

Expand All @@ -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();
}
13 changes: 6 additions & 7 deletions examples/basic-netconf-usage/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn main() !void {

const open_result = try d.open(
allocator,
operation.NewOpenOptions(),
.{},
);
defer open_result.deinit();

Expand All @@ -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(
Expand All @@ -145,10 +145,9 @@ pub fn main() !void {
},
);

var get_options = operation.NewGetOptions();
get_options.filter = "<system><aaa><authentication></authentication></aaa></system>";

const get_config_result = try d.get(allocator, get_options);
const get_config_result = try d.get(allocator, .{
.filter = "<system><aaa><authentication></authentication></aaa></system>",
});
defer get_config_result.deinit();

std.debug.print(
Expand All @@ -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();
}
19 changes: 0 additions & 19 deletions src/auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 21 additions & 9 deletions src/driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -267,7 +273,7 @@ pub const Driver = struct {

var res = try self.NewResult(
allocator,
result.OperationKind.GetPrompt,
result.OperationKind.get_prompt,
);
errdefer res.deinit();

Expand All @@ -292,7 +298,7 @@ pub const Driver = struct {

var res = try self.NewResult(
allocator,
result.OperationKind.EnterMode,
result.OperationKind.enter_mode,
);
errdefer res.deinit();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 14 additions & 14 deletions src/ffi-driver-netconf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub const OperationResult = struct {
};

pub const OperationKind = enum {
Open,
GetConfig,
open,
get_config,
};

pub const OpenOperation = struct {
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
},
}

Expand Down
11 changes: 5 additions & 6 deletions src/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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});
Expand Down
4 changes: 2 additions & 2 deletions src/platform.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
74 changes: 37 additions & 37 deletions src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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 {
Expand Down

0 comments on commit 52b4829

Please sign in to comment.