use super::super::FieldOrExt;
use super::super::{ANNOTATIONS, CBL_START};
use super::annotations::{push_tag_modifiers, AnnWriter};
use super::output::{wfl_prefix, wfl_prefix_n, write_nan_hex};
use crate::serialize::common::escape_bytes_into;
pub(in super::super) struct ScalarCtx<'a> {
pub(in super::super) field_number: u64,
pub(in super::super) field_schema: Option<&'a FieldOrExt>,
pub(in super::super) tag_ohb: Option<u64>,
pub(in super::super) tag_oor: bool,
pub(in super::super) len_ohb: Option<u64>,
pub(in super::super) wire_type_name: &'a str,
pub(in super::super) nan_bits: Option<u64>,
}
pub(in super::super) fn render_scalar(
ctx: &ScalarCtx<'_>,
value_str: &str,
is_wire: bool, out: &mut Vec<u8>,
) {
let ScalarCtx {
field_number,
field_schema,
tag_ohb,
tag_oor,
len_ohb,
wire_type_name,
nan_bits,
} = *ctx;
let annotations = ANNOTATIONS.with(|c| c.get());
let unknown = field_schema.is_none();
if !annotations && (unknown || is_wire) {
return;
}
let use_numeric_key = unknown || is_wire;
wfl_prefix_n(field_number, field_schema, use_numeric_key, out);
out.extend_from_slice(value_str.as_bytes());
if annotations {
let mut aw = AnnWriter::new();
if unknown || is_wire {
aw.push_wire(out, wire_type_name);
push_tag_modifiers(&mut aw, out, tag_ohb, tag_oor, len_ohb);
} else {
aw.push_field_decl(out, field_number, field_schema, None, None);
push_tag_modifiers(&mut aw, out, tag_ohb, tag_oor, len_ohb);
if let Some(bits) = nan_bits {
aw.sep(out);
out.extend_from_slice(b"nan_bits: 0x");
write_nan_hex(bits, out);
}
}
}
out.push(b'\n');
CBL_START.with(|c| c.set(out.len())); }
pub(in super::super) fn render_invalid(
field_number: u64,
_field_schema: Option<&FieldOrExt>,
tag_ohb: Option<u64>,
tag_oor: bool,
inv_name: &str,
raw: &[u8],
out: &mut Vec<u8>,
) {
let annotations = ANNOTATIONS.with(|c| c.get());
wfl_prefix_n(field_number, None, true, out);
out.push(b'"');
escape_bytes_into(raw, out);
out.push(b'"');
if annotations {
let mut aw = AnnWriter::new();
aw.push_wire(out, inv_name);
push_tag_modifiers(&mut aw, out, tag_ohb, tag_oor, None);
}
out.push(b'\n');
CBL_START.with(|c| c.set(out.len())); }
pub(in super::super) fn render_invalid_tag_type(raw: &[u8], out: &mut Vec<u8>) {
let annotations = ANNOTATIONS.with(|c| c.get());
wfl_prefix("0", out);
out.push(b'"');
escape_bytes_into(raw, out);
out.push(b'"');
if annotations {
let mut aw = AnnWriter::new();
aw.push(out, b"INVALID_TAG_TYPE"); }
out.push(b'\n');
CBL_START.with(|c| c.set(out.len())); }
pub(in super::super) fn render_truncated_bytes(
field_number: u64,
tag_ohb: Option<u64>,
tag_oor: bool,
len_ohb: Option<u64>,
missing: u64,
raw: &[u8],
out: &mut Vec<u8>,
) {
let annotations = ANNOTATIONS.with(|c| c.get());
wfl_prefix_n(field_number, None, true, out);
out.push(b'"');
escape_bytes_into(raw, out);
out.push(b'"');
if annotations {
let mut aw = AnnWriter::new();
aw.push(out, b"TRUNCATED_BYTES"); push_tag_modifiers(&mut aw, out, tag_ohb, tag_oor, len_ohb);
aw.push_u64_mod(out, b"MISSING: ", missing); }
out.push(b'\n');
CBL_START.with(|c| c.set(out.len())); }