use core::fmt::{Error, Write};
use crate::latex_modes::{LatexModeKindExt, LatexModeKind, ControlSeqDelimited, InlineMathMode};
pub struct Counters {
pub equation: usize,
others: core::marker::PhantomData<()>,
}
pub struct CountersLabel(String);
pub struct LabelGenerationError;
pub trait Label {
fn eqref<IM,W>(&self, w: &mut W) -> Result<(), Error>
where
IM: LatexModeKindExt,
W: Write;
unsafe fn tag_n_label<W>(&self, w: &mut W) -> Result<(), Error>
where
W: Write;
}
pub trait LabelGenerator {
type Label: Label;
fn next_label(&mut self) -> Result<Self::Label, LabelGenerationError>;
}
impl Counters {
pub fn new() -> Self {
Self {
equation: 1,
others: core::marker::PhantomData,
}
}
}
impl Label for CountersLabel {
fn eqref<IM,W>(&self, w: &mut W) -> Result<(), Error>
where
IM: LatexModeKindExt,
W: Write,
{
let is_delimiting_required = match IM::KIND {
LatexModeKind::InlineMathMode | LatexModeKind::DisplayMathMode => false,
_ => true
};
if is_delimiting_required {
InlineMathMode::write_opening_control_seq(w)?;
}
w.write_str(r"\eqref{")?;
w.write_str(self.0.as_str())?;
w.write_str("}")?;
if is_delimiting_required {
InlineMathMode::write_closing_control_seq(w)?;
}
Ok(())
}
unsafe fn tag_n_label<W>(&self, w: &mut W) -> Result<(), Error>
where
W: Write
{
w.write_str(r"\tag{")?;
w.write_str(self.0.as_str())?;
w.write_char('}')?;
w.write_str(r"\label{")?;
w.write_str(self.0.as_str())?;
w.write_char('}')
}
}
impl LabelGenerator for Counters {
type Label = CountersLabel;
fn next_label(&mut self) -> Result<Self::Label, LabelGenerationError> {
let label = format!("{}", self.equation);
self.equation += 1;
Ok(CountersLabel(label))
}
}