use super::{FormattingContext, FormattingError};
use crate::core::Expression;
mod expressions;
mod functions;
const MAX_RECURSION_DEPTH: usize = 1000;
const MAX_TERMS_PER_OPERATION: usize = 10000;
#[derive(Debug, Default, Clone)]
pub struct LaTeXContext {
pub needs_parentheses: bool,
}
impl FormattingContext for LaTeXContext {}
pub trait LaTeXFormatter {
fn to_latex<C>(&self, context: C) -> Result<String, FormattingError>
where
C: Into<Option<LaTeXContext>>,
{
let context = context.into().unwrap_or_default();
self.to_latex_with_depth(&context, 0)
}
fn to_latex_with_depth(
&self,
context: &LaTeXContext,
depth: usize,
) -> Result<String, FormattingError>;
fn function_to_latex_with_depth(
&self,
name: &str,
args: &[Expression],
context: &LaTeXContext,
depth: usize,
) -> Result<String, FormattingError>;
fn function_to_latex(
&self,
name: &str,
args: &[Expression],
context: &LaTeXContext,
) -> Result<String, FormattingError> {
match self.function_to_latex_with_depth(name, args, context, 0) {
Ok(result) => Ok(result),
Err(error) => Err(FormattingError::InvalidMathConstruct {
reason: error.to_string(),
}),
}
}
}
impl LaTeXFormatter for Expression {
fn to_latex_with_depth(
&self,
context: &LaTeXContext,
depth: usize,
) -> Result<String, FormattingError> {
expressions::to_latex_with_depth_impl(self, context, depth)
}
fn function_to_latex_with_depth(
&self,
name: &str,
args: &[Expression],
context: &LaTeXContext,
depth: usize,
) -> Result<String, FormattingError> {
functions::function_to_latex_with_depth_impl(self, name, args, context, depth)
}
}