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, line_comment, star, ampersand, hash, dollar, at, 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 = "f", .symbol = .line_comment, .label = "//", .protocol_command = "command insert //" }, .{ .key = "x", .symbol = .star, .label = "*", .protocol_command = "command insert *" }, .{ .key = "a", .symbol = .ampersand, .label = "&", .protocol_command = "command insert &" }, .{ .key = "h", .symbol = .hash, .label = "#", .protocol_command = "command insert #" }, .{ .key = "d", .symbol = .dollar, .label = "$", .protocol_command = "command insert $" }, .{ .key = "r", .symbol = .at, .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 / f // x * a & h # d $ r @ 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.line_comment, lookup("f").?.symbol); try std.testing.expectEqual(Symbol.star, lookup("x").?.symbol); try std.testing.expectEqual(Symbol.ampersand, lookup("a").?.symbol); try std.testing.expectEqual(Symbol.hash, lookup("h").?.symbol); try std.testing.expectEqual(Symbol.dollar, lookup("d").?.symbol); try std.testing.expectEqual(Symbol.at, lookup("r").?.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(.line_comment)); try std.testing.expectEqualStrings("command insert *", protocolCommand(.star)); try std.testing.expectEqualStrings("command insert &", protocolCommand(.ampersand)); try std.testing.expectEqualStrings("command insert #", protocolCommand(.hash)); try std.testing.expectEqualStrings("command insert $", protocolCommand(.dollar)); try std.testing.expectEqualStrings("command insert @", protocolCommand(.at)); 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("z")); try std.testing.expectEqual(@as(?Entry, null), lookup("/")); try std.testing.expectEqual(@as(?Entry, null), lookup("")); }