1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::convert_into_calculator_float;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::prelude::*;
use qoqo_calculator::Calculator;
#[pyclass(name = "Calculator", module = "qoqo_calculator_pyo3")]
pub struct CalculatorWrapper {
pub r_calculator: Calculator,
}
#[pymethods]
impl CalculatorWrapper {
#[new]
fn new() -> Self {
let r_calculator = Calculator::new();
CalculatorWrapper { r_calculator }
}
fn set(&mut self, variable_string: &str, val: f64) {
self.r_calculator.set_variable(variable_string, val);
}
pub fn parse_str(&mut self, input: &str) -> PyResult<f64> {
match self.r_calculator.parse_str(input) {
Ok(x) => Ok(x),
Err(x) => Err(PyValueError::new_err(format!(
"{:?}; expression: {}",
x, input
))),
}
}
pub fn parse_get(&mut self, input: &PyAny) -> PyResult<f64> {
let converted = convert_into_calculator_float(input)
.map_err(|_| PyTypeError::new_err("Input can not be converted to Calculator Float"))?;
let out = self.r_calculator.parse_get(converted);
match out {
Ok(x) => Ok(x),
Err(x) => Err(PyValueError::new_err(format!("{:?}", x))),
}
}
}
pub fn parse_str(expression: &str) -> PyResult<f64> {
let mut calculator = Calculator::new();
match calculator.parse_str(expression) {
Ok(x) => Ok(x),
Err(x) => Err(PyValueError::new_err(format!(
"{:?}; expression {}",
x, expression
))),
}
}