use crate::encoding::Encoder;
#[cfg(not(feature = "indexes"))]
use crate::template::Section;
#[cfg(feature = "indexes")]
use crate::template::{Indexed, Section};
use crate::Content;
pub trait Combine {
type I: Content + Copy + Sized;
type J: Content + Copy + Sized;
type K: Content + Copy + Sized;
type Previous: ContentSequence;
fn combine<X: Content + ?Sized>(self, other: &X) -> (Self::I, Self::J, Self::K, &X);
fn crawl_back(self) -> Self::Previous;
}
pub trait ContentSequence: Combine + Sized + Copy {
#[inline]
fn render_field_escaped<E: Encoder>(
&self,
_hash: u64,
_name: &str,
_encoder: &mut E,
) -> Result<(), E::Error> {
Ok(())
}
#[inline]
fn render_field_unescaped<E: Encoder>(
&self,
_hash: u64,
_name: &str,
_encoder: &mut E,
) -> Result<(), E::Error> {
Ok(())
}
#[inline]
fn render_field_section<P, E>(
&self,
_hash: u64,
_name: &str,
_section: Section<'_, P>,
_encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
Ok(())
}
#[inline]
fn render_field_inverse<P, E>(
&self,
_hash: u64,
_name: &str,
_section: Section<'_, P>,
_encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
Ok(())
}
#[cfg(feature = "indexes")]
#[inline]
fn render_index_section<'section, P, E>(
&self,
_indexed: &Indexed,
_section: Section<'section, P>,
_encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
Ok(())
}
}
impl Combine for () {
type I = ();
type J = ();
type K = ();
type Previous = ();
#[inline]
fn combine<X: Content + ?Sized>(self, other: &X) -> ((), (), (), &X) {
((), (), (), other)
}
#[inline]
fn crawl_back(self) -> Self::Previous {}
}
impl ContentSequence for () {}
impl<A, B, C, D> Combine for (A, B, C, D)
where
A: Content + Copy,
B: Content + Copy,
C: Content + Copy,
D: Content + Copy,
{
type I = B;
type J = C;
type K = D;
type Previous = ((), A, B, C);
#[inline]
fn combine<X: Content + ?Sized>(self, other: &X) -> (B, C, D, &X) {
(self.1, self.2, self.3, other)
}
#[inline]
fn crawl_back(self) -> ((), A, B, C) {
((), self.0, self.1, self.2)
}
}
impl<A, B, C, D> ContentSequence for (A, B, C, D)
where
A: Content + Copy,
B: Content + Copy,
C: Content + Copy,
D: Content + Copy,
{
#[inline]
fn render_field_escaped<E: Encoder>(
&self,
hash: u64,
name: &str,
encoder: &mut E,
) -> Result<(), E::Error> {
if !self.3.render_field_escaped(hash, name, encoder)?
&& !self.2.render_field_escaped(hash, name, encoder)?
&& !self.1.render_field_escaped(hash, name, encoder)?
{
self.0.render_field_escaped(hash, name, encoder)?;
}
Ok(())
}
#[inline]
fn render_field_unescaped<E: Encoder>(
&self,
hash: u64,
name: &str,
encoder: &mut E,
) -> Result<(), E::Error> {
if !self.3.render_field_unescaped(hash, name, encoder)?
&& !self.2.render_field_unescaped(hash, name, encoder)?
&& !self.1.render_field_unescaped(hash, name, encoder)?
{
self.0.render_field_unescaped(hash, name, encoder)?;
}
Ok(())
}
#[inline]
fn render_field_section<P, E>(
&self,
hash: u64,
name: &str,
section: Section<P>,
encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
if !self.3.render_field_section(hash, name, section, encoder)? {
let section = section.without_last();
if !self.2.render_field_section(hash, name, section, encoder)? {
let section = section.without_last();
if !self.1.render_field_section(hash, name, section, encoder)? {
let section = section.without_last();
self.0.render_field_section(hash, name, section, encoder)?;
}
}
}
Ok(())
}
#[inline]
fn render_field_inverse<P, E>(
&self,
hash: u64,
name: &str,
section: Section<P>,
encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
if !self.3.render_field_inverse(hash, name, section, encoder)?
&& !self.2.render_field_inverse(hash, name, section, encoder)?
&& !self.1.render_field_inverse(hash, name, section, encoder)?
&& !self.0.render_field_inverse(hash, name, section, encoder)?
{
section.render(encoder)?;
}
Ok(())
}
#[cfg(feature = "indexes")]
#[inline]
fn render_index_section<'section, P, E>(
&self,
indexed: &Indexed,
section: Section<'section, P>,
encoder: &mut E,
) -> Result<(), E::Error>
where
P: ContentSequence,
E: Encoder,
{
if !self.3.render_index_section(indexed, section, encoder)? {
let section = section.without_last();
if !self.2.render_index_section(indexed, section, encoder)? {
let section = section.without_last();
if !self.1.render_index_section(indexed, section, encoder)? {
let section = section.without_last();
self.0.render_index_section(indexed, section, encoder)?;
}
}
}
Ok(())
}
}