use crate::content_type::ContentType;
use crate::error::StreamError;
use crate::format::{DefaultFormat, ItemEncoder, StreamFormat, StreamFormatEncode};
use bytes::BytesMut;
const TEXT_CONTENT_TYPE: &str = "text/plain; charset=utf-8";
#[derive(Debug, Clone, Copy, Default)]
pub struct TextStreamFormat;
impl TextStreamFormat {
pub fn new() -> Self {
Self
}
}
impl DefaultFormat for TextStreamFormat {
fn default_format() -> Self {
Self
}
}
impl StreamFormat for TextStreamFormat {
fn format_name(&self) -> &'static str {
"text"
}
fn default_content_type(&self) -> &'static str {
TEXT_CONTENT_TYPE
}
fn accepts_content_type(&self, ct: &ContentType<'_>) -> bool {
ct.matches("text/plain")
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct TextEncoder;
impl ItemEncoder<String> for TextEncoder {
fn encode(&mut self, item: &String, _index: u64, buf: &mut BytesMut) -> Result<(), StreamError> {
buf.extend_from_slice(item.as_bytes());
Ok(())
}
}
impl StreamFormatEncode<String> for TextStreamFormat {
type Encoder = TextEncoder;
fn encoder(&self) -> Self::Encoder {
TextEncoder
}
}