Skip to content

Commit

Permalink
Drop MultiCodeName. Rename MultiCodeCodec to Codec
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Nov 17, 2024
1 parent a6b4f4e commit d80f0ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 634 deletions.
35 changes: 2 additions & 33 deletions src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,9 @@ pub fn main() !void {
try output_file.writeAll("});\n\n");
}

// MultiCodeName
// Codecs
try output_file.writeAll(
\\pub const MultiCodeName = enum {
\\
);

lines.reset();
{
// Skip the header
_ = lines.next();

while (lines.next()) |line| {
var fields = std.mem.tokenize(u8, line, ",");

const name = std.mem.trim(u8, fields.next() orelse break, " ");

const name_replaced = try allocator.dupe(u8, name);
std.mem.replaceScalar(u8, name_replaced, '-', '_');

const string = try std.fmt.allocPrint(
allocator,
"{s},\n",
.{name_replaced},
);
defer allocator.free(string);

try output_file.writeAll(string);
}
try output_file.writeAll("};\n\n");
}

// MultiCodeCode
try output_file.writeAll(
\\pub const MultiCodeCode = enum(u32) {
\\pub const Codec = enum(u32) {
\\
);

Expand Down
24 changes: 12 additions & 12 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const table = @import("table.zig");
const muvarint = @import("muvarint");

pub fn getCodecByName(name: table.MultiCodeName) !table.Multicodec {
pub fn getCodecByName(name: table.Codec) !table.Multicodec {
return table.multicodecTable.get(@tagName(name)) orelse return error.InvalidCodecName;
}

Expand All @@ -20,7 +20,7 @@ pub fn getCodecByCode(code: anytype) !table.Multicodec {
return codec;
}

pub fn addNamePrefix(allocator: std.mem.Allocator, name: table.MultiCodeName, data: []u8) ![]u8 {
pub fn addNamePrefix(allocator: std.mem.Allocator, name: table.Codec, data: []u8) ![]u8 {
const multicodec = try getCodecByName(name);
if (multicodec.status != table.Status.Permanent) {
return error.CodecNotPermanent;
Expand Down Expand Up @@ -50,55 +50,55 @@ pub fn split(data: []u8) !struct { codec: table.Multicodec, data: []const u8 } {

test "getCodecByName" {
{
const codec = try getCodecByName(table.MultiCodeName.raw);
const codec = try getCodecByName(table.Codec.raw);
try std.testing.expect(std.mem.eql(u8, codec.name, "raw"));
}
{
const codec = try getCodecByName(table.MultiCodeName.lamport_sha3_512_priv_share);
const codec = try getCodecByName(table.Codec.lamport_sha3_512_priv_share);
try std.testing.expect(std.mem.eql(u8, codec.name, "lamport-sha3-512-priv-share"));
}
}

test "getCodecByCode" {
{
const codec = try getCodecByCode(@intFromEnum(table.MultiCodeCode.raw));
const codec = try getCodecByCode(@intFromEnum(table.Codec.raw));
try std.testing.expect(std.mem.eql(u8, codec.name, "raw"));
}
{
const codec = try getCodecByCode(@intFromEnum(table.MultiCodeCode.lamport_sha3_512_priv_share));
const codec = try getCodecByCode(@intFromEnum(table.Codec.lamport_sha3_512_priv_share));
try std.testing.expect(std.mem.eql(u8, codec.name, "lamport-sha3-512-priv-share"));
}
}

test "addNamePrefix" {
var input: [5]u8 = [_]u8{ 104, 101, 108, 108, 111 };
const value = try addNamePrefix(std.testing.allocator, table.MultiCodeName.raw, input[0..]);
const value = try addNamePrefix(std.testing.allocator, table.Codec.raw, input[0..]);
defer std.testing.allocator.free(value);
try std.testing.expect(std.mem.eql(u8, value, &[6]u8{ 85, 104, 101, 108, 108, 111 }));
}

test "getCodec" {
var input: [5]u8 = [_]u8{ 104, 101, 108, 108, 111 };
const prefixed = try addNamePrefix(std.testing.allocator, table.MultiCodeName.raw, input[0..]);
const prefixed = try addNamePrefix(std.testing.allocator, table.Codec.raw, input[0..]);
defer std.testing.allocator.free(prefixed);
const codec = try getCodec(prefixed);
try std.testing.expectEqual(codec.code, @intFromEnum(table.MultiCodeCode.raw));
try std.testing.expectEqual(codec.code, @intFromEnum(table.Codec.raw));
}

test "getData" {
var input: [5]u8 = [_]u8{ 104, 101, 108, 108, 111 };
const prefixed = try addNamePrefix(std.testing.allocator, table.MultiCodeName.raw, input[0..]);
const prefixed = try addNamePrefix(std.testing.allocator, table.Codec.raw, input[0..]);
defer std.testing.allocator.free(prefixed);
const data = try getData(prefixed);
try std.testing.expect(std.mem.eql(u8, data, &[5]u8{ 104, 101, 108, 108, 111 }));
}

test "split" {
var input: [5]u8 = [_]u8{ 104, 101, 108, 108, 111 };
const prefixed = try addNamePrefix(std.testing.allocator, table.MultiCodeName.raw, input[0..]);
const prefixed = try addNamePrefix(std.testing.allocator, table.Codec.raw, input[0..]);
defer std.testing.allocator.free(prefixed);
const codec_and_data = try split(prefixed);
try std.testing.expect(std.mem.eql(u8, prefixed, &[6]u8{ 85, 104, 101, 108, 108, 111 }));
try std.testing.expectEqual(codec_and_data.codec.code, @intFromEnum(table.MultiCodeCode.raw));
try std.testing.expectEqual(codec_and_data.codec.code, @intFromEnum(table.Codec.raw));
try std.testing.expect(std.mem.eql(u8, codec_and_data.data, &[5]u8{ 104, 101, 108, 108, 111 }));
}
Loading

0 comments on commit d80f0ab

Please sign in to comment.