SimpleFormatter

Trait SimpleFormatter 

Source
pub trait SimpleFormatter {
    // Required method
    fn to_simple_with_depth(
        &self,
        context: &SimpleContext,
        depth: usize,
    ) -> Result<String, FormattingError>;

    // Provided method
    fn to_simple(
        &self,
        context: &SimpleContext,
    ) -> Result<String, FormattingError> { ... }
}
Expand description

Format the expression to Simple

Required Methods§

Source

fn to_simple_with_depth( &self, context: &SimpleContext, depth: usize, ) -> Result<String, FormattingError>

Format with explicit recursion depth tracking

Internal method that provides stack overflow protection by tracking recursion depth. This method returns a Result to allow proper error propagation during recursive formatting.

§Arguments
  • context - Formatting configuration
  • depth - Current recursion depth (starts at 0)
§Returns
  • Ok(String) - Successfully formatted expression
  • Err(String) - Error message if limits exceeded
§Safety Limits
  • Maximum recursion depth: 1000 levels
  • Maximum terms per operation: 10000 terms/factors/arguments
§Examples
use mathhook_core::core::Expression;
use mathhook_core::formatter::simple::{SimpleFormatter, SimpleContext};

let expr = Expression::from("x + y");
let context = SimpleContext::default();
let result = expr.to_simple_with_depth(&context, 0);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "x + y");

Provided Methods§

Source

fn to_simple(&self, context: &SimpleContext) -> Result<String, FormattingError>

Format an Expression as simple mathematical notation

Converts mathematical expressions into clean, readable text format without LaTeX commands or complex markup. The output can be customized using the provided context.

§Arguments
  • context - Formatting configuration controlling output style
§Context Options
  • float_precision - Number of decimal places for floating point numbers
  • use_unicode - Whether to use Unicode symbols (× instead of *)
  • parenthesize_negatives - Whether to wrap negative numbers in parentheses
  • implicit_multiplication - Whether to use implicit multiplication (2x vs 2*x)
§Examples
use mathhook_core::{Expression, expr};
use mathhook_core::formatter::simple::{SimpleFormatter, SimpleContext};

let expression = expr!(2 * x);
let context = SimpleContext::default();
let result = expression.to_simple(&context).unwrap();
assert!(result.contains("2"));
assert!(result.contains("x"));

// With Unicode symbols
let context = SimpleContext { use_unicode: true, ..Default::default() };
let result = expression.to_simple(&context).unwrap();
assert!(result.contains("×"));
§Error Handling

Returns error messages for expressions that exceed safety limits:

  • Maximum recursion depth (1000 levels)
  • Maximum terms per operation (10000 terms)

Implementors§