diff --git a/src/root.zig b/src/root.zig index 44c7df4..8712ab3 100644 --- a/src/root.zig +++ b/src/root.zig @@ -226,7 +226,7 @@ pub fn Ymlz(comptime Destination: type) type { }, .Pointer => { if (actual_type_info.Pointer.size == .Slice and actual_type_info.Pointer.child == u8) { - @field(destination, field.name) = try self.parseStringExpression(raw_line, depth); + @field(destination, field.name) = try self.parseStringExpression(raw_line, depth, false); } else if (actual_type_info.Pointer.size == .Slice and (actual_type_info.Pointer.child == []const u8 or actual_type_info.Pointer.child == []u8)) { @field(destination, field.name) = try self.parseStringArrayExpression(actual_type_info.Pointer.child, depth + 1); } else if (actual_type_info.Pointer.size == .Slice and @typeInfo(actual_type_info.Pointer.child) != .Pointer) { @@ -293,12 +293,8 @@ pub fn Ymlz(comptime Destination: type) type { if (raw_line) |line| { try self.allocations.append(line); - if (line.len == 0) { - return "\n"; - } - // TODO: Need to fix this comments can start not only from index 0. - if (line[0] == '#') { + if (line.len == 0 or line[0] == '#') { // Skipping comments return self.readLine(); } @@ -333,7 +329,7 @@ pub fn Ymlz(comptime Destination: type) type { break; } - const result = try self.parseStringExpression(raw_value_line, depth); + const result = try self.parseStringExpression(raw_value_line, depth, false); try list.append(result); } @@ -362,8 +358,8 @@ pub fn Ymlz(comptime Destination: type) type { return try list.toOwnedSlice(); } - fn parseStringExpression(self: *Self, raw_line: []const u8, depth: usize) ![]const u8 { - const expression = try self.parseSimpleExpression(raw_line, depth); + fn parseStringExpression(self: *Self, raw_line: []const u8, depth: usize, is_multiline: bool) ![]const u8 { + const expression = try self.parseSimpleExpression(raw_line, depth, is_multiline); const value = self.getExpressionValue(expression); if (value.len == 0) return value; @@ -393,7 +389,7 @@ pub fn Ymlz(comptime Destination: type) type { break; } - const expression = try self.parseSimpleExpression(raw_value_line, depth); + const expression = try self.parseSimpleExpression(raw_value_line, depth, true); const value = self.getExpressionValue(expression); try list.appendSlice(value); @@ -420,7 +416,7 @@ pub fn Ymlz(comptime Destination: type) type { } fn parseBooleanExpression(self: *Self, raw_line: []const u8, depth: usize) !bool { - const expression = try self.parseSimpleExpression(raw_line, depth); + const expression = try self.parseSimpleExpression(raw_line, depth, false); const value = self.getExpressionValue(expression); const isBooleanTrue = std.mem.eql(u8, value, "True") or std.mem.eql(u8, value, "true") or std.mem.eql(u8, value, "On") or std.mem.eql(u8, value, "on"); @@ -439,7 +435,7 @@ pub fn Ymlz(comptime Destination: type) type { } fn parseNumericExpression(self: *Self, comptime T: type, raw_line: []const u8, depth: usize) !T { - const expression = try self.parseSimpleExpression(raw_line, depth); + const expression = try self.parseSimpleExpression(raw_line, depth, false); const value = self.getExpressionValue(expression); switch (@typeInfo(T)) { @@ -458,14 +454,14 @@ pub fn Ymlz(comptime Destination: type) type { fn withoutQuotes(self: *Self, line: []const u8) []const u8 { _ = self; - if (line[0] == '\'' or line[0] == '"' and line[line.len - 1] == '\'' or line[line.len - 1] == '"') { + if ((line[0] == '\'' or line[0] == '"') and (line[line.len - 1] == '\'' or line[line.len - 1] == '"')) { return line[1 .. line.len - 1]; } return line; } - fn parseSimpleExpression(self: *Self, raw_line: []const u8, depth: usize) !Expression { + fn parseSimpleExpression(self: *Self, raw_line: []const u8, depth: usize, is_multiline: bool) !Expression { const indent_depth = self.getIndentDepth(depth); if (raw_line.len < indent_depth) { @@ -475,6 +471,14 @@ pub fn Ymlz(comptime Destination: type) type { }; } + // NOTE: Need to think about this a bit more, maybe there is a cleaner solution for this. + if (is_multiline) { + return .{ + .value = .{ .Simple = raw_line[indent_depth..] }, + .raw = raw_line, + }; + } + const line = raw_line[indent_depth..]; if (line[0] == '-') { diff --git a/src/tests.zig b/src/tests.zig index b8dfa45..58b2e82 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -29,7 +29,11 @@ test "98YD" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 5.5. Comment Indicator")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "Spec Example 5.5. Comment Indicator")); + // dump: "" + try expect(element.dump.len == 0); } test "CC74" { @@ -57,7 +61,11 @@ test "CC74" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 6.20. Tag Handles")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "Spec Example 6.20. Tag Handles")); + try expect(std.mem.eql(u8, element.tree, "+STR\n +DOC ---\n =VAL \"bar\n -DOC\n-STR")); + try expect(std.mem.eql(u8, element.dump, "--- ! \"bar\"\n")); } test "F6MC" { @@ -85,5 +93,8 @@ test "F6MC" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "More indented lines at the beginning of folded block scalars")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "More indented lines at the beginning of folded block scalars")); + try expect(std.mem.eql(u8, element.json, "{\n \"a\": \" more indented\\nregular\\n\",\n \"b\": \"\\n\\n more indented\\nregular\\n\"\n}")); }