Files
texrra-engine/crates/texrra-core/src/environment.rs
T
gloamxun bb1d9e0561 Initial texrra engine
Rename the crate, binary, core crate, font families, env vars, and internal commands from texcraft/tex-engine to texrra. Serve the separately versioned texrra-web frontend from TEXRRA_WEB_DIR, ../web, or ./web instead of embedding index.html. Add starred section heading support and an arXiv template conformance fixture.
2026-10-19 11:41:46 +08:00

64 lines
1.7 KiB
Rust

use std::fmt;
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct EnvironmentStack {
names: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EndEnvironmentError {
Unmatched { requested: String },
Mismatched { active: String, requested: String },
}
impl fmt::Display for EndEnvironmentError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unmatched { requested } => {
write!(
formatter,
"`\\end{{{requested}}}` has no matching `\\begin`"
)
}
Self::Mismatched { active, requested } => write!(
formatter,
"environment `{active}` was ended by `\\end{{{requested}}}`"
),
}
}
}
impl EnvironmentStack {
pub fn begin(&mut self, name: String) {
self.names.push(name);
}
pub fn opens_tex_group(name: &str) -> bool {
name != "document"
}
pub fn current(&self) -> Option<&str> {
self.names.last().map(String::as_str)
}
pub fn end(&mut self, requested: &str) -> Result<(), EndEnvironmentError> {
match self.current() {
Some(active) if active == requested => {
self.names.pop();
Ok(())
}
Some(active) => Err(EndEnvironmentError::Mismatched {
active: active.to_string(),
requested: requested.to_string(),
}),
None => Err(EndEnvironmentError::Unmatched {
requested: requested.to_string(),
}),
}
}
pub fn as_slice(&self) -> &[String] {
&self.names
}
}