#[cfg(feature = "ffi")]
#[allow(clippy::unnecessary_cast)]
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
type Expression;
#[swift_bridge(swift_name = "parseText")]
fn parse_text(input: &str) -> Result<Expression, String>;
#[swift_bridge(swift_name = "parseLatex")]
fn parse_latex_ffi(input: &str) -> Result<Expression, String>;
#[swift_bridge(swift_name = "toString")]
fn expression_to_string(expr: &Expression) -> String;
#[swift_bridge(swift_name = "toLatex")]
fn expression_to_latex(expr: &Expression) -> String;
#[swift_bridge(swift_name = "findVariables")]
fn expression_find_variables(expr: &Expression) -> Vec<String>;
#[swift_bridge(swift_name = "findFunctions")]
fn expression_find_functions(expr: &Expression) -> Vec<String>;
#[swift_bridge(swift_name = "findConstants")]
fn expression_find_constants(expr: &Expression) -> Vec<String>;
#[swift_bridge(swift_name = "depth")]
fn expression_depth(expr: &Expression) -> usize;
#[swift_bridge(swift_name = "nodeCount")]
fn expression_node_count(expr: &Expression) -> usize;
#[swift_bridge(swift_name = "toJSON")]
fn expression_to_json(expr: &Expression) -> Result<String, String>;
#[swift_bridge(swift_name = "toJSONPretty")]
fn expression_to_json_pretty(expr: &Expression) -> Result<String, String>;
}
}
#[cfg(feature = "ffi")]
use crate::{parse, parser::parse_latex as parse_latex_internal, Expression};
#[cfg(feature = "ffi")]
use crate::parser::{
latex::parse_latex_equation_system, text::parse_equation_system as parse_text_equation_system,
};
#[cfg(feature = "ffi")]
pub fn parse_text(input: &str) -> Result<Expression, String> {
parse(input).map_err(|e| e.to_string())
}
#[cfg(feature = "ffi")]
pub fn parse_latex_ffi(input: &str) -> Result<Expression, String> {
parse_latex_internal(input).map_err(|e| e.to_string())
}
#[cfg(feature = "ffi")]
pub fn expression_to_string(expr: &Expression) -> String {
format!("{}", expr)
}
#[cfg(feature = "ffi")]
pub fn expression_to_latex(expr: &Expression) -> String {
use crate::latex::ToLatex;
expr.to_latex()
}
#[cfg(feature = "ffi")]
pub fn expression_find_variables(expr: &Expression) -> Vec<String> {
expr.find_variables().into_iter().collect()
}
#[cfg(feature = "ffi")]
pub fn expression_find_functions(expr: &Expression) -> Vec<String> {
expr.find_functions().into_iter().collect()
}
#[cfg(feature = "ffi")]
pub fn expression_find_constants(expr: &Expression) -> Vec<String> {
expr.find_constants()
.into_iter()
.map(|c| format!("{}", c))
.collect()
}
#[cfg(feature = "ffi")]
pub fn expression_depth(expr: &Expression) -> usize {
expr.depth()
}
#[cfg(feature = "ffi")]
pub fn expression_node_count(expr: &Expression) -> usize {
expr.node_count()
}
#[cfg(feature = "ffi")]
pub fn expression_to_json(expr: &Expression) -> Result<String, String> {
#[cfg(feature = "serde")]
{
serde_json::to_string(expr).map_err(|e| e.to_string())
}
#[cfg(not(feature = "serde"))]
{
let _ = expr;
Err("serde feature is not enabled".to_string())
}
}
#[cfg(feature = "ffi")]
pub fn expression_to_json_pretty(expr: &Expression) -> Result<String, String> {
#[cfg(feature = "serde")]
{
serde_json::to_string_pretty(expr).map_err(|e| e.to_string())
}
#[cfg(not(feature = "serde"))]
{
let _ = expr;
Err("serde feature is not enabled".to_string())
}
}
#[cfg(feature = "ffi")]
pub fn parse_equation_system_ffi(input: &str) -> Result<Vec<Expression>, String> {
parse_text_equation_system(input).map_err(|e| e.to_string())
}
#[cfg(feature = "ffi")]
pub fn parse_latex_equation_system_ffi(input: &str) -> Result<Vec<Expression>, String> {
parse_latex_equation_system(input).map_err(|e| e.to_string())
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_json;