Skip to content

Commit

Permalink
force_by_string
Browse files Browse the repository at this point in the history
  • Loading branch information
r4gus committed Oct 14, 2023
1 parent 8710363 commit 7b94ea1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ pub const StringifyOptions = struct {
skip_null_fields: bool = true,
/// Convert an u8 slice into a CBOR text string.
slice_as_text: bool = false,
force_byte_string: bool = false,
/// Use the field name instead of the numerical value to represent a enum.
enum_as_text: bool = true,
/// Pass an optional allocator. This might be useful when implementing
Expand All @@ -390,6 +391,7 @@ pub const FieldSettings = struct {
skip_if_null: bool = true,
/// Convert the field into a CBOR text string (only if u8 slice)
slice_as_text: bool = false,
force_byte_string: bool = false,
/// Use the field name instead of its numerical value (only if enum)
enum_as_text: bool = true,
} = .{},
Expand Down Expand Up @@ -537,6 +539,7 @@ pub fn stringify(
// We found settings for the given field
child_options.skip_null_fields = fs.options.skip_if_null;
child_options.slice_as_text = fs.options.slice_as_text;
child_options.force_byte_string = fs.options.force_byte_string;
child_options.enum_as_text = fs.options.enum_as_text;

if (fs.alias) |alias| {
Expand Down Expand Up @@ -571,7 +574,11 @@ pub fn stringify(
if (ptr_info.child == u8) {
const is_sentinel = ptr_info.sentinel != null;
const valid_utf8 = std.unicode.utf8ValidateSlice(value);
head = if ((is_sentinel or options.slice_as_text) and valid_utf8) 0x60 else 0x40;
if (options.force_byte_string) {
head = 0x40;
} else {
head = if ((is_sentinel or options.slice_as_text) and valid_utf8) 0x60 else 0x40;
}
} else {
head = 0x80;
}
Expand Down

0 comments on commit 7b94ea1

Please sign in to comment.