use alloc::string::String;
use alloc::vec::Vec;
use mf2_catalog::{Catalog, StrRef};
use crate::error::FormatError;
pub trait Sink {
fn push_str(&mut self, s: &str);
#[doc(hidden)]
fn push_catalog_text(&mut self, catalog: &Catalog, r: StrRef) -> bool {
match catalog.text(r) {
Some(s) => {
self.push_str(s);
true
}
None => false,
}
}
}
impl Sink for String {
fn push_str(&mut self, s: &str) {
let room = |t: &String| t.capacity().wrapping_sub(t.len()) >= s.len();
if (room(self) || self.try_reserve(s.len()).is_ok()) && room(self) {
String::push_str(self, s);
}
}
}
pub trait ErrorSink {
fn error(&mut self, e: FormatError);
}
impl ErrorSink for Vec<FormatError> {
fn error(&mut self, e: FormatError) {
crate::scratch::try_push(self, e);
}
}
#[derive(Clone, Copy, Default, Debug)]
pub struct NoErrors;
impl ErrorSink for NoErrors {
#[inline]
fn error(&mut self, _: FormatError) {}
}
pub trait SubPartSink {
fn sub_part(&mut self, kind: &str, text: &str);
}