Common structures: WIP.

master
Karchnu 2020-12-15 17:54:21 +01:00
parent 1ee88ce284
commit bbd2e67cb8
1 changed files with 51 additions and 0 deletions

51
src/common-structures.zig Normal file
View File

@ -0,0 +1,51 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const hashString = std.hash_map.hashString;
const eqlString = std.hash_map.eqlString;
const HashMap = std.HashMap;
const AutoHashMap = std.AutoHashMap;
const ArrayList = std.ArrayList;
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(Node);
const Definitions = AutoHashMap([] const u8, Node);
pub const Node = struct {
id: ?[] const u8,
type_name: [] const u8,
properties: PropertyHashMap,
children: NodeList,
};
pub const PropertyValue = union {
string: [] const u8, // String.
integer: u64, // Num (integer).
float: f64, // Num (float).
reference: *u8, // Reference to another property (property binding).
};
pub const Root = struct {
definitions: Definitions,
children: NodeList,
};
test "simple test about structures" {
const allocator = std.heap.page_allocator;
var value = PropertyValue { .integer = 10 };
var properties = PropertyHashMap.init(allocator);
defer properties.deinit();
try properties.put("hello", value);
std.debug.print("\n", .{});
var it = properties.iterator();
while(it.next()) |kv| {
std.debug.print("key: {} => value: {}\n", .{kv.key, properties.get(kv.key)});
}
}