From ae2d5063d760a6f383ca195d766e44e5f8bcc6ed Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Tue, 19 Jun 2018 14:29:29 +0200 Subject: Updated to newest version of zig --- src/core.zig | 65 ++++++++++++++++++++++++++++---------------------------- src/extended.zig | 51 ++++++++++++++++++-------------------------- 2 files changed, 53 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/core.zig b/src/core.zig index d870e8c..a0f794c 100644 --- a/src/core.zig +++ b/src/core.zig @@ -102,12 +102,10 @@ pub fn Param(comptime Id: type) type { pub fn init(id: Id, takes_value: bool, names: *const Names) Self { // Assert, that if the param have no name, then it has to take // a value. - debug.assert( - names.bare != null or + debug.assert(names.bare != null or names.long != null or names.short != null or - takes_value - ); + takes_value); return Self{ .id = id, @@ -127,7 +125,7 @@ pub fn Arg(comptime Id: type) type { value: ?[]const u8, pub fn init(param: *const Param(Id), value: ?[]const u8) Self { - return Self { + return Self{ .param = param, .value = value, }; @@ -141,7 +139,7 @@ pub fn ArgIterator(comptime E: type) type { const Self = this; const Error = E; - nextFn: fn(iter: *Self) Error!?[]const u8, + nextFn: fn (iter: *Self) Error!?[]const u8, pub fn next(iter: *Self) Error!?[]const u8 { return iter.nextFn(iter); @@ -159,12 +157,10 @@ pub const ArgSliceIterator = struct { iter: ArgIterator(Error), pub fn init(args: []const []const u8) ArgSliceIterator { - return ArgSliceIterator { + return ArgSliceIterator{ .args = args, .index = 0, - .iter = ArgIterator(Error) { - .nextFn = nextFn, - }, + .iter = ArgIterator(Error){ .nextFn = nextFn }, }; } @@ -188,12 +184,10 @@ pub const OsArgIterator = struct { iter: ArgIterator(Error), pub fn init(allocator: *mem.Allocator) OsArgIterator { - return OsArgIterator { + return OsArgIterator{ .arena = heap.ArenaAllocator.init(allocator), .args = os.args(), - .iter = ArgIterator(Error) { - .nextFn = nextFn, - }, + .iter = ArgIterator(Error){ .nextFn = nextFn }, }; } @@ -204,7 +198,7 @@ pub const OsArgIterator = struct { fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { const self = @fieldParentPtr(OsArgIterator, "iter", iter); if (builtin.os == builtin.Os.windows) { - return try self.args.next(self.allocator) ?? return null; + return try self.args.next(self.allocator) orelse return null; } else { return self.args.nextPosix(); } @@ -233,7 +227,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { state: State, pub fn init(params: []const Param(Id), iter: *ArgIterator(ArgError)) Self { - var res = Self { + var res = Self{ .params = params, .iter = iter, .state = State.Normal, @@ -245,7 +239,11 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { /// Get the next ::Arg that matches a ::Param. pub fn next(clap: *Self) !?Arg(Id) { const ArgInfo = struct { - const Kind = enum { Long, Short, Bare }; + const Kind = enum { + Long, + Short, + Bare, + }; arg: []const u8, kind: Kind, @@ -253,7 +251,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { switch (clap.state) { State.Normal => { - const full_arg = (try clap.iter.next()) ?? return null; + const full_arg = (try clap.iter.next()) orelse return null; const arg_info = blk: { var arg = full_arg; var kind = ArgInfo.Kind.Bare; @@ -271,7 +269,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { if (kind != ArgInfo.Kind.Long and arg.len == 0) return error.InvalidArgument; - break :blk ArgInfo { .arg = arg, .kind = kind }; + break :blk ArgInfo{ .arg = arg, .kind = kind }; }; const arg = arg_info.arg; @@ -279,16 +277,15 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { const eql_index = mem.indexOfScalar(u8, arg, '='); switch (kind) { - ArgInfo.Kind.Bare, - ArgInfo.Kind.Long => { + ArgInfo.Kind.Bare, ArgInfo.Kind.Long => { for (clap.params) |*param| { const match = switch (kind) { - ArgInfo.Kind.Bare => param.names.bare ?? continue, - ArgInfo.Kind.Long => param.names.long ?? continue, + ArgInfo.Kind.Bare => param.names.bare orelse continue, + ArgInfo.Kind.Long => param.names.long orelse continue, else => unreachable, }; const name = if (eql_index) |i| arg[0..i] else arg; - const maybe_value = if (eql_index) |i| arg[i + 1..] else null; + const maybe_value = if (eql_index) |i| arg[i + 1 ..] else null; if (!mem.eql(u8, name, match)) continue; @@ -303,14 +300,14 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { if (maybe_value) |v| break :blk v; - break :blk (try clap.iter.next()) ?? return error.MissingValue; + break :blk (try clap.iter.next()) orelse return error.MissingValue; }; return Arg(Id).init(param, value); } }, ArgInfo.Kind.Short => { - return try clap.chainging(State.Chaining { + return try clap.chainging(State.Chaining{ .arg = full_arg, .index = (full_arg.len - arg.len), }); @@ -340,7 +337,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { const next_index = index + 1; for (clap.params) |*param| { - const short = param.names.short ?? continue; + const short = param.names.short orelse continue; if (short != arg[index]) continue; @@ -349,10 +346,12 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { if (arg.len <= next_index or param.takes_value) { clap.state = State.Normal; } else { - clap.state = State { .Chaining = State.Chaining { - .arg = arg, - .index = next_index, - }}; + clap.state = State{ + .Chaining = State.Chaining{ + .arg = arg, + .index = next_index, + }, + }; } } @@ -360,12 +359,12 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { return Arg(Id).init(param, null); if (arg.len <= next_index) { - const value = (try clap.iter.next()) ?? return error.MissingValue; + const value = (try clap.iter.next()) orelse return error.MissingValue; return Arg(Id).init(param, value); } if (arg[next_index] == '=') { - return Arg(Id).init(param, arg[next_index + 1..]); + return Arg(Id).init(param, arg[next_index + 1 ..]); } return Arg(Id).init(param, arg[next_index..]); diff --git a/src/extended.zig b/src/extended.zig index ec8310e..09e91dd 100644 --- a/src/extended.zig +++ b/src/extended.zig @@ -1,12 +1,12 @@ pub const core = @import("core.zig"); const builtin = @import("builtin"); -const std = @import("std"); +const std = @import("std"); -const mem = std.mem; -const fmt = std.fmt; +const mem = std.mem; +const fmt = std.fmt; const debug = std.debug; -const io = std.io; +const io = std.io; const assert = debug.assert; @@ -92,7 +92,7 @@ pub const Parser = struct { func: UnsafeFunction, pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser { - return Parser { + return Parser{ .FieldType = FieldType, .Errors = Errors, .func = @ptrCast(UnsafeFunction, func), @@ -104,7 +104,7 @@ pub const Parser = struct { } fn ParseFunc(comptime FieldType: type, comptime Errors: type) type { - return fn(*FieldType, []const u8) Errors!void; + return fn (*FieldType, []const u8) Errors!void; } pub fn int(comptime Int: type, comptime radix: u8) Parser { @@ -113,22 +113,14 @@ pub const Parser = struct { field_ptr.* = try fmt.parseInt(Int, arg, radix); } }.i; - return Parser.init( - Int, - @typeOf(func).ReturnType.ErrorSet, - func - ); + return Parser.init(Int, @typeOf(func).ReturnType.ErrorSet, func); } - const string = Parser.init( - []const u8, - error{}, - struct { - fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { - field_ptr.* = arg; - } - }.s - ); + const string = Parser.init([]const u8, error{}, struct { + fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { + field_ptr.* = arg; + } + }.s); }; pub fn Clap(comptime Result: type) type { @@ -174,7 +166,7 @@ pub fn Clap(comptime Result: type) type { for (command.params) |p, i| { const id = i; - res[id] = core.Param(usize) { + res[id] = core.Param(usize){ .id = id, .takes_value = p.kind == Param.Kind.Option, .names = p.names, @@ -186,10 +178,9 @@ pub fn Clap(comptime Result: type) type { var pos: usize = 0; - arg_loop: - while (try clap.next()) |arg| : (pos += 1) { - inline for(command.params) |param, i| { - if (arg.param.id == i and (param.settings.position ?? pos) == pos) { + arg_loop: while (try clap.next()) |arg| : (pos += 1) { + inline for (command.params) |param, i| { + if (arg.param.id == i and (param.settings.position orelse pos) == pos) { handled[i] = true; switch (param.kind) { @@ -197,7 +188,7 @@ pub fn Clap(comptime Result: type) type { getFieldPtr(&result, param.field).* = true; }, Param.Kind.Option => |parser| { - try parser.parse(getFieldPtr(&result, param.field), ??arg.value); + try parser.parse(getFieldPtr(&result, param.field), arg.value.?); }, Param.Kind.Subcommand => |sub_command| { getFieldPtr(&result, param.field).* = try sub_command.parseHelper(Error, clap); @@ -223,19 +214,19 @@ pub fn Clap(comptime Result: type) type { fn GetFieldPtrReturn(comptime Struct: type, comptime field: []const u8) type { var inst: Struct = undefined; - const dot_index = comptime mem.indexOfScalar(u8, field, '.') ?? { + const dot_index = comptime mem.indexOfScalar(u8, field, '.') orelse { return @typeOf(&@field(inst, field)); }; - return GetFieldPtrReturn(@typeOf(@field(inst, field[0..dot_index])), field[dot_index + 1..]); + return GetFieldPtrReturn(@typeOf(@field(inst, field[0..dot_index])), field[dot_index + 1 ..]); } fn getFieldPtr(curr: var, comptime field: []const u8) GetFieldPtrReturn(@typeOf(curr).Child, field) { - const dot_index = comptime mem.indexOfScalar(u8, field, '.') ?? { + const dot_index = comptime mem.indexOfScalar(u8, field, '.') orelse { return &@field(curr, field); }; - return getFieldPtr(&@field(curr, field[0..dot_index]), field[dot_index + 1..]); + return getFieldPtr(&@field(curr, field[0..dot_index]), field[dot_index + 1 ..]); } }; } -- cgit v1.2.3