use core::fmt::{Error, Write};
pub struct Counters {
pub equation: usize,
others: core::marker::PhantomData<()>,
}
pub struct CountersLabel(String);
pub struct LabelGenerationError;
pub trait Label {
unsafe fn eqref<W>(&self, w: &mut W) -> Result<(), Error>
where
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 {
unsafe fn eqref<W>(&self, w: &mut W) -> Result<(), Error>
where
W: Write,
{
w.write_str(r"$\eqref{")?;
w.write_str(self.0.as_str())?;
w.write_str("}$")
}
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))
}
}