use super::sys::text::text::{text_encode_options, upb_DebugString};
use super::{AssociatedMiniTable, MessagePtr};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TextEncodeOptions {
pub single_line: bool,
pub skip_unknown: bool,
}
impl TextEncodeOptions {
pub fn to_bits(self) -> i32 {
let mut bits = 0;
if self.single_line {
bits |= text_encode_options::SINGLE_LINE;
}
if self.skip_unknown {
bits |= text_encode_options::SKIP_UNKNOWN;
}
bits
}
}
pub unsafe fn debug_string<T: AssociatedMiniTable>(msg: MessagePtr<T>) -> String {
let mt = T::mini_table();
let msg = msg.raw();
let len = unsafe { upb_DebugString(msg, mt, 0, core::ptr::null_mut(), 0) };
assert!(len < isize::MAX as usize);
let mut buf = vec![0u8; len + 1];
let written_len = unsafe { upb_DebugString(msg, mt, 0, buf.as_mut_ptr(), buf.len()) };
assert_eq!(len, written_len);
String::from_utf8_lossy(&buf[..len]).into_owned()
}