Skip to content

Commit

Permalink
migrated from Zig version 0.11.0 to 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
r4gus committed Apr 20, 2024
1 parent 98efdd4 commit cb186d8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 25 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ authors:
orcid: "https://orcid.org/0009-0007-0056-602X"
repository-code: 'https://github.com/r4gus/zbor'
abstract: 'CBOR parser written in Zig'
version: 0.12.3
date-released: 2024-01-20
version: 0.13.0
date-released: 2024-04-20
keywords:
- cbor
- rfc8949
Expand Down
6 changes: 4 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) !void {
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -25,7 +25,9 @@ pub fn build(b: *std.build.Builder) !void {
//lib.install();

const zbor_module = b.addModule("zbor", .{
.source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

try b.modules.put(b.dupe("zbor"), zbor_module);
Expand Down
61 changes: 60 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
.{
.name = "zbor",
.version = "0.12.3",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.13.0",

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.12.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",

// // When this is set to `true`, a package is declared to be lazily
// // fetched. This makes the dependency only get fetched if it is
// // actually used.
// .lazy = false,
//},
},

// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package.
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"license",
"README.md",
"data",
},
}

2 changes: 1 addition & 1 deletion src/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub const Builder = struct {
try self.moveUp();
}

var s = self.stack.items[0].raw.toOwnedSlice();
const s = self.stack.items[0].raw.toOwnedSlice();
self.stack.deinit();
return s;
}
Expand Down
8 changes: 4 additions & 4 deletions src/cbor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub const DataItem = struct {
if (T != Type.ByteString and T != Type.TextString) return null;

var begin: usize = 0;
var len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;
const len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;

return self.data[begin .. begin + len];
}
Expand All @@ -106,7 +106,7 @@ pub const DataItem = struct {
if (T != Type.Array) return null;

var begin: usize = 0;
var len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;
const len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;

// Get to the end of the array
var end: usize = 0;
Expand All @@ -129,7 +129,7 @@ pub const DataItem = struct {
if (T != Type.Map) return null;

var begin: usize = 0;
var len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;
const len = if (additionalInfo(self.data, &begin)) |v| @as(usize, @intCast(v)) else return null;

// Get to the end of the map
var end: usize = 0;
Expand Down Expand Up @@ -198,7 +198,7 @@ pub const DataItem = struct {
if (T != Type.Tagged) return null;

var begin: usize = 0;
var nr = if (additionalInfo(self.data, &begin)) |v| v else return null;
const nr = if (additionalInfo(self.data, &begin)) |v| v else return null;

return Tag{
.nr = nr,
Expand Down
8 changes: 4 additions & 4 deletions src/cose.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub const Key = union(KeyTag) {
const sig = try signer.finalize();
var buffer: [EcdsaP256Sha256.Signature.der_encoded_max_length]u8 = undefined;
const der = sig.toDer(&buffer);
var mem = try allocator.alloc(u8, der.len);
const mem = try allocator.alloc(u8, der.len);
@memcpy(mem, der);
return mem;
},
Expand Down Expand Up @@ -485,7 +485,7 @@ test "es256 sign verify 1" {
const allocator = std.testing.allocator;
const msg = "Hello, World!";

var kp1 = try EcdsaP256Sha256.KeyPair.create(null);
const kp1 = try EcdsaP256Sha256.KeyPair.create(null);

// Create a signature via cose key struct
var cosep256 = Key.fromP256PrivPub(.Es256, kp1.secret_key, kp1.public_key);
Expand All @@ -509,8 +509,8 @@ test "es256 sign verify 1" {
}

test "copy secure #1" {
var kp1 = try EcdsaP256Sha256.KeyPair.create(null);
const kp1 = try EcdsaP256Sha256.KeyPair.create(null);
var cosep256 = Key.fromP256PrivPub(.Es256, kp1.secret_key, kp1.public_key);
var cpy = cosep256.copySecure();
const cpy = cosep256.copySecure();
try std.testing.expectEqual(cpy.P256.d, null);
}
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const Builder = build.Builder;
pub const ContainerType = build.ContainerType;

// TODO: can we somehow read this from build.zig.zon???
pub const VERSION: []const u8 = "0.12.3";
pub const VERSION: []const u8 = "0.13.0";

test {
_ = cbor;
Expand Down
20 changes: 10 additions & 10 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn parse(
},
.Struct => |structInfo| {
// Custom parse function overrides default behaviour
const has_parse = comptime std.meta.trait.hasFn("cborParse")(T);
const has_parse = comptime std.meta.hasFn(T, "cborParse");
if (has_parse and !options.from_callback) {
var o = options;
o.from_callback = true;
Expand Down Expand Up @@ -228,11 +228,11 @@ pub fn parse(
.ByteString, .TextString => {
if (arrayInfo.child != u8) return ParseError.UnexpectedItem;

var v = if (item.string()) |x| x else return ParseError.Malformed;
const v = if (item.string()) |x| x else return ParseError.Malformed;
var r: T = undefined;

if (v.len > r[0..].len) return ParseError.Overflow;
std.mem.copy(u8, r[0..v.len], v);
std.mem.copyForwards(u8, r[0..v.len], v);

return r;
},
Expand Down Expand Up @@ -267,7 +267,7 @@ pub fn parse(

var r: []ptrInfo.child = try allocator.alloc(ptrInfo.child, v.len + sentinel);
errdefer allocator.free(r);
std.mem.copy(ptrInfo.child, r[0..], v[0..]);
std.mem.copyForwards(ptrInfo.child, r[0..], v[0..]);
if (ptrInfo.sentinel) |some| {
const sentinel_value = @as(*align(1) const ptrInfo.child, @ptrCast(some)).*;
r[r.len - 1] = sentinel_value;
Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn parse(
},
.Union => |unionInfo| {
// Custom parse function overrides default behaviour
const has_parse = comptime std.meta.trait.hasFn("cborParse")(T);
const has_parse = comptime std.meta.hasFn(T, "cborParse");
if (has_parse and !options.from_callback) {
var o = options;
o.from_callback = true;
Expand Down Expand Up @@ -483,7 +483,7 @@ pub fn stringify(
},
.Struct => |S| {
// Custom stringify function overrides default behaviour
const has_stringify = comptime std.meta.trait.hasFn("cborStringify")(T);
const has_stringify = comptime std.meta.hasFn(T, "cborStringify");
if (has_stringify and !options.from_callback) {
// its probably better its set here otherwise people might forget
// to set it which leads to infinite loops.
Expand Down Expand Up @@ -643,7 +643,7 @@ pub fn stringify(
}
},
.Union => {
const has_stringify = comptime std.meta.trait.hasFn("cborStringify")(T);
const has_stringify = comptime std.meta.hasFn(T, "cborStringify");
if (has_stringify and !options.from_callback) {
var o = options;
o.from_callback = true;
Expand Down Expand Up @@ -914,7 +914,7 @@ test "parse pointer" {
test "parse slice" {
const allocator = std.testing.allocator;

var e1: []const u8 = &.{ 1, 2, 3, 4, 5 };
const e1: []const u8 = &.{ 1, 2, 3, 4, 5 };
const di1 = try DataItem.new("\x45\x01\x02\x03\x04\x05");
const c1 = try parse([]const u8, di1, .{ .allocator = allocator });
defer allocator.free(c1);
Expand Down Expand Up @@ -1529,8 +1529,8 @@ test "assign allocator to allocator fields #1" {
b: std.mem.Allocator,
};

var di = try DataItem.new("\xa1\x61\x61\x65\x61\x62\x63\x64\x65");
var x = try parse(S, di, .{ .allocator = allocator });
const di = try DataItem.new("\xa1\x61\x61\x65\x61\x62\x63\x64\x65");
const x = try parse(S, di, .{ .allocator = allocator });
defer allocator.free(x.a);

try std.testing.expectEqualSlices(u8, "abcde", x.a);
Expand Down

0 comments on commit cb186d8

Please sign in to comment.