use crate::alloc_prelude::*;
use core::fmt::Write;
#[derive(Debug, Default)]
pub struct XmlDestination {
buffer: String,
}
impl XmlDestination {
pub fn new() -> Self {
Self {
buffer: String::new(),
}
}
pub fn write_str(&mut self, s: &str) {
let _ = self.buffer.write_str(s);
}
pub fn write_char(&mut self, c: char) {
self.buffer.push(c);
}
pub fn into_string(self) -> String {
self.buffer
}
pub fn as_str(&self) -> &str {
&self.buffer
}
pub fn clear(&mut self) {
self.buffer.clear();
}
}
impl babbel_core::io::traits::IDestination for XmlDestination {
fn add_byte(&mut self, byte: u8) {
self.buffer.push(byte as char);
}
fn add_bytes(&mut self, bytes: &str) {
self.buffer.push_str(bytes);
}
fn clear(&mut self) {
self.buffer.clear();
}
fn last(&self) -> Option<u8> {
self.buffer.as_bytes().last().copied()
}
}
impl babbel_core::io::traits::IClearable for XmlDestination {
fn clear(&mut self) {
self.buffer.clear();
}
}
impl babbel_core::io::traits::ITailInspectable for XmlDestination {
fn last_byte(&self) -> Option<u8> {
self.buffer.as_bytes().last().copied()
}
}
pub trait DestinationExt {
fn write_str(&mut self, s: &str);
fn write_char(&mut self, c: char);
}
impl<D: babbel_core::io::traits::IDestination + ?Sized> DestinationExt for D {
#[inline]
fn write_str(&mut self, s: &str) {
self.add_bytes(s);
}
#[inline]
fn write_char(&mut self, c: char) {
if c.is_ascii() {
self.add_byte(c as u8);
} else {
let mut buf = [0u8; 4];
self.add_bytes(c.encode_utf8(&mut buf));
}
}
}