Skip to content

Commit f558ebb

Browse files
committed
♻️ refactor: move functions to utils
1 parent 224aeef commit f558ebb

3 files changed

Lines changed: 53 additions & 47 deletions

File tree

src-tauri/src/core/parser.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod utils;
55
use super::functions::operators;
66
use super::functions::{Expression, Function};
77

8-
use self::utils::is_valid_number;
8+
use self::utils::{get_function_name, is_const_or_variable};
99

1010
pub fn parse_expression<'a>(expression: &'a str) -> Result<Expression, String> {
1111
let mut string = String::from(expression);
@@ -133,49 +133,3 @@ fn get_function_parameters(content: &Vec<String>) -> Result<Vec<Expression>, Str
133133

134134
Ok(out_vec)
135135
}
136-
137-
fn is_const_or_variable(expression: &String) -> Result<Option<Expression>, String> {
138-
if expression.contains('(') {
139-
return Ok(None);
140-
}
141-
142-
// const
143-
if is_valid_number(&expression, 10) {
144-
return Ok(Some(Expression::Const {
145-
value: expression.parse::<f64>().unwrap(),
146-
}));
147-
// var
148-
} else if expression.len() == 1 {
149-
return Ok(Some(Expression::Variable {
150-
identifier: expression.chars().nth(0).unwrap(),
151-
}));
152-
} else {
153-
return Err(format!(
154-
"Expression: '{}' is not a function, but neither a number or a variable!",
155-
expression
156-
));
157-
}
158-
}
159-
160-
fn get_function_name(expression: &String) -> Result<String, String> {
161-
let mut index = 0;
162-
let mut name: String = String::from("");
163-
while index < expression.len() {
164-
let char = expression.chars().nth(index).unwrap();
165-
if char.is_alphabetic() {
166-
name.push(char);
167-
} else if char == '(' {
168-
return Ok(name);
169-
} else {
170-
return Err(format!(
171-
"Error while parsing expression: invalid character '{}' !",
172-
char
173-
));
174-
}
175-
index += 1;
176-
}
177-
178-
Err(String::from(
179-
"Error while parsing expression: could not find a function!",
180-
))
181-
}

src-tauri/src/core/parser/utils.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use super::Expression;
2+
13
#[derive(PartialEq, Eq)]
24
pub enum Direction {
35
Left,
@@ -108,3 +110,49 @@ pub fn is_valid_number(string: &String, radix: u32) -> bool {
108110
}
109111
true
110112
}
113+
114+
pub fn is_const_or_variable(expression: &String) -> Result<Option<Expression>, String> {
115+
if expression.contains('(') {
116+
return Ok(None);
117+
}
118+
119+
// const
120+
if is_valid_number(&expression, 10) {
121+
return Ok(Some(Expression::Const {
122+
value: expression.parse::<f64>().unwrap(),
123+
}));
124+
// var
125+
} else if expression.len() == 1 {
126+
return Ok(Some(Expression::Variable {
127+
identifier: expression.chars().nth(0).unwrap(),
128+
}));
129+
} else {
130+
return Err(format!(
131+
"Expression: '{}' is not a function, but neither a number or a variable!",
132+
expression
133+
));
134+
}
135+
}
136+
137+
pub fn get_function_name(expression: &String) -> Result<String, String> {
138+
let mut index = 0;
139+
let mut name: String = String::from("");
140+
while index < expression.len() {
141+
let char = expression.chars().nth(index).unwrap();
142+
if char.is_alphabetic() {
143+
name.push(char);
144+
} else if char == '(' {
145+
return Ok(name);
146+
} else {
147+
return Err(format!(
148+
"Error while parsing expression: invalid character '{}' !",
149+
char
150+
));
151+
}
152+
index += 1;
153+
}
154+
155+
Err(String::from(
156+
"Error while parsing expression: could not find a function!",
157+
))
158+
}

src/app.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
@tailwind components;
33
@tailwind utilities;
44

5+
body {
6+
@apply overflow-hidden;
7+
}
8+
59
@font-face {
610
font-family: 'JetBrainsMono';
711
src: url('./assets/fonts/JetBrainsMono-Regular.ttf');

0 commit comments

Comments
 (0)