Skip to content

Commit

Permalink
Fixing bugs (#17)
Browse files Browse the repository at this point in the history
* fix small bugs

* fix

* more fixes
  • Loading branch information
pwbh authored Sep 7, 2024
1 parent c2ba7a7 commit 665111b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
32 changes: 18 additions & 14 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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] == '-') {
Expand Down
17 changes: 14 additions & 3 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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 <tag:example.com,2000:app/foo> \"bar\n -DOC\n-STR"));
try expect(std.mem.eql(u8, element.dump, "--- !<tag:example.com,2000:app/foo> \"bar\"\n"));
}

test "F6MC" {
Expand Down Expand Up @@ -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}"));
}

0 comments on commit 665111b

Please sign in to comment.