86 lines
3.6 KiB
Zig
86 lines
3.6 KiB
Zig
const std = @import("std");
|
|
|
|
// Source-patched coding symbol rail table.
|
|
// req: input/003, governance/001, testing/001, testing/002, testing/003, testing/004
|
|
|
|
test {
|
|
_ = lookup;
|
|
}
|
|
|
|
pub const Symbol = enum {
|
|
parens,
|
|
brackets,
|
|
braces,
|
|
slash,
|
|
pipe,
|
|
double_quote,
|
|
single_quote,
|
|
backtick,
|
|
underscore,
|
|
};
|
|
|
|
pub const Entry = struct {
|
|
key: []const u8,
|
|
symbol: Symbol,
|
|
label: []const u8,
|
|
protocol_command: []const u8,
|
|
};
|
|
|
|
pub const entries = [_]Entry{
|
|
.{ .key = "p", .symbol = .parens, .label = "()", .protocol_command = "command pair parens" },
|
|
.{ .key = "b", .symbol = .brackets, .label = "[]", .protocol_command = "command pair brackets" },
|
|
.{ .key = "c", .symbol = .braces, .label = "{}", .protocol_command = "command pair braces" },
|
|
.{ .key = "s", .symbol = .slash, .label = "/", .protocol_command = "command insert /" },
|
|
.{ .key = "v", .symbol = .pipe, .label = "|", .protocol_command = "command insert |" },
|
|
.{ .key = "q", .symbol = .double_quote, .label = "\"\"", .protocol_command = "command pair double_quote" },
|
|
.{ .key = "e", .symbol = .single_quote, .label = "''", .protocol_command = "command pair single_quote" },
|
|
.{ .key = "t", .symbol = .backtick, .label = "``", .protocol_command = "command pair backtick" },
|
|
.{ .key = "u", .symbol = .underscore, .label = "_", .protocol_command = "command insert _" },
|
|
};
|
|
|
|
pub const rail_status = "symbols: p () b [] c {} s / v | q \"\" e '' t `` u _";
|
|
|
|
pub fn lookup(key: []const u8) ?Entry {
|
|
for (entries) |entry| {
|
|
if (std.mem.eql(u8, key, entry.key)) return entry;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn protocolCommand(symbol: Symbol) []const u8 {
|
|
for (entries) |entry| {
|
|
if (entry.symbol == symbol) return entry.protocol_command;
|
|
}
|
|
unreachable;
|
|
}
|
|
|
|
test "regular: symbol rail maps mobile-friendly letters to coding punctuation" {
|
|
try std.testing.expectEqual(Symbol.parens, lookup("p").?.symbol);
|
|
try std.testing.expectEqual(Symbol.brackets, lookup("b").?.symbol);
|
|
try std.testing.expectEqual(Symbol.braces, lookup("c").?.symbol);
|
|
try std.testing.expectEqual(Symbol.slash, lookup("s").?.symbol);
|
|
try std.testing.expectEqual(Symbol.pipe, lookup("v").?.symbol);
|
|
try std.testing.expectEqual(Symbol.double_quote, lookup("q").?.symbol);
|
|
try std.testing.expectEqual(Symbol.single_quote, lookup("e").?.symbol);
|
|
try std.testing.expectEqual(Symbol.backtick, lookup("t").?.symbol);
|
|
try std.testing.expectEqual(Symbol.underscore, lookup("u").?.symbol);
|
|
}
|
|
|
|
test "regular: symbol rail protocol commands stay tiny and explicit" {
|
|
try std.testing.expectEqualStrings("command pair parens", protocolCommand(.parens));
|
|
try std.testing.expectEqualStrings("command pair brackets", protocolCommand(.brackets));
|
|
try std.testing.expectEqualStrings("command pair braces", protocolCommand(.braces));
|
|
try std.testing.expectEqualStrings("command insert /", protocolCommand(.slash));
|
|
try std.testing.expectEqualStrings("command insert |", protocolCommand(.pipe));
|
|
try std.testing.expectEqualStrings("command pair double_quote", protocolCommand(.double_quote));
|
|
try std.testing.expectEqualStrings("command pair single_quote", protocolCommand(.single_quote));
|
|
try std.testing.expectEqualStrings("command pair backtick", protocolCommand(.backtick));
|
|
try std.testing.expectEqualStrings("command insert _", protocolCommand(.underscore));
|
|
}
|
|
|
|
test "adversarial: unknown symbol key does not map to a symbol" {
|
|
try std.testing.expectEqual(@as(?Entry, null), lookup("x"));
|
|
try std.testing.expectEqual(@as(?Entry, null), lookup("/"));
|
|
try std.testing.expectEqual(@as(?Entry, null), lookup(""));
|
|
}
|