Skip to content

Commit ee84102

Browse files
committed
v0.2.3 linted, public API changed.
1 parent 0304a32 commit ee84102

10 files changed

+70
-74
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lib_xch"
3-
version = "0.2.1"
3+
version = "0.2.3"
44
authors = ["LEXUGE <LEXUGEyky@outlook.com>"]
55
description = "Crate xch-ceb's official lib"
66
license = "GPL-3.0"

src/balancer_mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn xch_try(
2424
f: i32,
2525
searching_range: i32,
2626
traversal: &mut Vec<i32>,
27-
list: &Vec<Vec<i32>>,
27+
list: &[Vec<i32>],
2828
chmcl_f_sut: &ChemicalEquation,
2929
len: usize,
3030
) -> Result<bool, String> {
@@ -50,18 +50,18 @@ pub fn xch_try(
5050

5151
fn check(
5252
traversal: &mut Vec<i32>,
53-
list: &Vec<Vec<i32>>,
53+
list: &[Vec<i32>],
5454
chmcl_f_sut: &ChemicalEquation,
5555
len: usize,
5656
) -> Result<bool, String> {
5757
let mut tmp1: i32;
5858
let mut tmp2: i32;
59-
for i in 1..len + 1 {
59+
for item in list.iter().take(len + 1).skip(1) {
6060
tmp1 = 0;
6161
tmp2 = 0;
6262
for j in 1..chmcl_f_sut.left_num as usize + 1 {
6363
let tmp: i32;
64-
tmp = match list[i][j].checked_mul(traversal[j - 1]) {
64+
tmp = match item[j].checked_mul(traversal[j - 1]) {
6565
Some(s) => s,
6666
None => return Err("[ERROR] i32 overflow".to_string()),
6767
};
@@ -72,7 +72,7 @@ fn check(
7272
}
7373
for j in chmcl_f_sut.left_num as usize + 1..chmcl_f_sut.sum as usize + 1 {
7474
let tmp: i32;
75-
tmp = match list[i][j].checked_mul(traversal[j - 1]) {
75+
tmp = match item[j].checked_mul(traversal[j - 1]) {
7676
Some(s) => s,
7777
None => return Err("[ERROR] i32 overflow".to_string()),
7878
};

src/handler.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use balancer_mod::xch_try;
2727
/// In following cases, API will **panic**:
2828
/// 1.Stack Overflow may cause panic too. Because it is using recusive balancer and regex-based parser.
2929
/// And in the other failed situation, it'll return a `error_message` and contain `parser_result`(maybe it is empty).
30-
pub fn handler_api(equation: String, searching_range: i32) -> Result<Vec<i32>, ErrorHandler> {
30+
pub fn handler_api(equation: &str, searching_range: i32) -> Result<Vec<i32>, ErrorHandler> {
3131
// T is successful traversal vector, E is list vector which parser returned.
3232
let mut traversal: Vec<i32> = Vec::new();
3333
let (chemical_equation_struct, elements_table_len, list) = match xch_parser(equation) {
@@ -62,8 +62,8 @@ pub fn handler_api(equation: String, searching_range: i32) -> Result<Vec<i32>, E
6262
}
6363
}
6464

65-
/// ErrorHandler returns when `handler::handler_api` failed somehow.
66-
/// **CAUTION: parser_result might empty if parser is failed.**
65+
/// `ErrorHandler` returns when `handler::handler_api` failed somehow.
66+
/// **CAUTION: `parser_result` might empty if parser is failed.**
6767
pub struct ErrorHandler {
6868
pub error_message: String,
6969
pub parser_result: Vec<Vec<i32>>,

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

1616
// extern crate(s)
17-
#[macro_use] extern crate lazy_static;
17+
#[macro_use]
18+
extern crate lazy_static;
1819
extern crate regex;
1920

2021
// mods

src/parser_mod/legal_check_util.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
// Overall: This is the source code of the Delta-3 Parser.
1717

18-
pub fn legal_check(equation: &String) -> Result<bool, String> {
18+
pub fn legal_check(equation: &str) -> Result<bool, String> {
1919
let equation = equation.chars().into_iter().collect::<Vec<_>>();
2020
let mut tmp = 0;
2121
for i in equation {
2222
if check_char(i) == 0 {
2323
return Err("[ERROR] Illegal Equation!".to_string());
2424
}
2525
if i == '=' {
26-
tmp = tmp + 1;
26+
tmp += 1;
2727
}
2828
}
2929
if tmp != 1 {
@@ -32,7 +32,7 @@ pub fn legal_check(equation: &String) -> Result<bool, String> {
3232
Ok(true)
3333
}
3434

35-
pub fn legal_check_brackets(formula: &String) -> Result<bool, String> {
35+
pub fn legal_check_brackets(formula: &str) -> Result<bool, String> {
3636
let formula = formula.chars().into_iter().collect::<Vec<_>>();
3737
for i in 0..formula.len() {
3838
if formula[i] == '(' {
@@ -45,32 +45,32 @@ pub fn legal_check_brackets(formula: &String) -> Result<bool, String> {
4545
Ok(true)
4646
}
4747

48-
fn brackets_matcher(formula: &Vec<char>, pos: usize, mode: bool) -> Result<usize, String> {
48+
fn brackets_matcher(formula: &[char], pos: usize, mode: bool) -> Result<usize, String> {
4949
let mut fake_stack = 0;
5050

51-
if mode == true {
52-
for i in pos + 1..formula.len() {
53-
if formula[i] == '(' {
54-
fake_stack = fake_stack + 1;
51+
if mode {
52+
for (i, item) in formula.iter().enumerate().skip(pos + 1) {
53+
if *item == '(' {
54+
fake_stack += 1;
5555
}
56-
if formula[i] == ')' {
56+
if *item == ')' {
5757
if fake_stack == 0 {
5858
return Ok(i);
5959
} else {
60-
fake_stack = fake_stack - 1;
60+
fake_stack -= 1;
6161
}
6262
}
6363
}
6464
} else {
6565
for i in (0..pos).rev() {
6666
if formula[i] == ')' {
67-
fake_stack = fake_stack + 1;
67+
fake_stack += 1;
6868
}
6969
if formula[i] == '(' {
7070
if fake_stack == 0 {
7171
return Ok(i);
7272
} else {
73-
fake_stack = fake_stack - 1;
73+
fake_stack -= 1;
7474
}
7575
}
7676
}
@@ -80,16 +80,16 @@ fn brackets_matcher(formula: &Vec<char>, pos: usize, mode: bool) -> Result<usize
8080

8181
fn check_char(test: char) -> i32 {
8282
if (test >= 'a') && (test <= 'z') {
83-
return 1; // 'a'~'z'
83+
1 // 'a'~'z'
8484
} else if (test >= 'A') && (test <= 'Z') {
85-
return 2; // 'A'~'Z'
85+
2 // 'A'~'Z'
8686
} else if (test >= '0') && (test <= '9') {
87-
return 3; // '0'~'9'
87+
3 // '0'~'9'
8888
} else if (test == '(') || (test <= ')') {
89-
return 4; // ( or )
89+
4 // ( or )
9090
} else if (test == '+') || (test == '=') {
91-
return 5; // + or =
91+
5 // + or =
9292
} else {
93-
return 0; // nothing!
93+
0 // nothing!
9494
}
9595
}

src/parser_mod/mod.rs

+25-31
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ use std::vec::Vec;
2525
use std::i32;
2626
// inside uses
2727
use structs::ChemicalEquation;
28-
use self::parser_struct::{TableDesc,FormulaDesc,TokenDesc};
28+
use self::parser_struct::{FormulaDesc, TableDesc, TokenDesc};
2929
use self::legal_check_util::{legal_check, legal_check_brackets};
3030

31-
pub fn xch_parser(equation: String) -> Result<(ChemicalEquation, usize, Vec<Vec<i32>>),String> {
32-
legal_check(&equation)?;
31+
pub fn xch_parser(equation: &str) -> Result<(ChemicalEquation, usize, Vec<Vec<i32>>), String> {
32+
legal_check(equation)?;
3333
let mut chemical_equation_struct = ChemicalEquation {
3434
left_num: 0,
3535
right_num: 0,
3636
sum: 0,
3737
};
38-
{ // block to get chemical_equation_struct.sum
38+
{
39+
// block to get chemical_equation_struct.sum
3940
let v: Vec<&str> = equation.split('=').collect();
4041
let equation_left: String = String::from(v[0]);
4142
let equation_right: String = String::from(v[1]);
@@ -45,16 +46,13 @@ pub fn xch_parser(equation: String) -> Result<(ChemicalEquation, usize, Vec<Vec<
4546
let mut table = TableDesc::new(chemical_equation_struct.sum);
4647
table.update_list_vec(); // first access will be like list[1][1]
4748

48-
{ // block to call parsers
49+
{
50+
// block to call parsers
4951
let v: Vec<&str> = equation.split('=').collect();
5052
let equation_left: String = String::from(v[0]);
5153
let equation_right: String = String::from(v[1]);
5254

53-
chemical_equation_struct.left_num = part_parser(
54-
&equation_left,
55-
&mut table,
56-
0,
57-
)?;
55+
chemical_equation_struct.left_num = part_parser(&equation_left, &mut table, 0)?;
5856
chemical_equation_struct.right_num = part_parser(
5957
&equation_right,
6058
&mut table,
@@ -63,10 +61,14 @@ pub fn xch_parser(equation: String) -> Result<(ChemicalEquation, usize, Vec<Vec<
6361
}
6462

6563
// return
66-
Ok((chemical_equation_struct, table.get_elements_table_len(), table.get_list()))
64+
Ok((
65+
chemical_equation_struct,
66+
table.get_elements_table_len(),
67+
table.get_list(),
68+
))
6769
}
6870

69-
fn parser_get_sum(equation: &String) -> Result<i32,String> {
71+
fn parser_get_sum(equation: &str) -> Result<i32, String> {
7072
let mut sum: i32 = 0;
7173
for _ in equation.split('+') {
7274
sum = match sum.checked_add(1) {
@@ -77,20 +79,12 @@ fn parser_get_sum(equation: &String) -> Result<i32,String> {
7779
Ok(sum)
7880
}
7981

80-
fn part_parser(
81-
equation: &String,
82-
table: &mut TableDesc,
83-
begin: i32,
84-
) -> Result<i32,String> {
82+
fn part_parser(equation: &str, table: &mut TableDesc, begin: i32) -> Result<i32, String> {
8583
let mut sum = begin;
8684
for formula in equation.split('+') {
87-
sum = sum + 1;
85+
sum += 1;
8886
legal_check_brackets(&formula.to_string())?;
89-
parser_formula(
90-
&formula.to_string(),
91-
table,
92-
sum as usize,
93-
)?;
87+
parser_formula(&formula.to_string(), table, sum as usize)?;
9488
}
9589
Ok(sum - begin)
9690
}
@@ -101,7 +95,7 @@ fn formula_spliter(target: &str) -> Result<Vec<FormulaDesc>, String> {
10195
static ref RE: Regex = Regex::new(r"\((([A-Z][a-z]*(\d+)*)+)\)(\d+)*").unwrap();
10296
}
10397

104-
if RE.is_match(target) == false {
98+
if !RE.is_match(target) {
10599
return Err("[ERROR] No more to split!".to_string());
106100
}
107101
for cap in RE.captures_iter(target) {
@@ -124,12 +118,12 @@ fn formula_spliter(target: &str) -> Result<Vec<FormulaDesc>, String> {
124118
Ok(v)
125119
}
126120

127-
fn get_token(target: &str) -> Result<Vec<TokenDesc>,String> {
121+
fn get_token(target: &str) -> Result<Vec<TokenDesc>, String> {
128122
let mut v: Vec<TokenDesc> = Vec::new();
129123
lazy_static! {
130124
static ref RE: Regex = Regex::new(r"([A-Z][a-z]*)(\d+)*").unwrap();
131125
}
132-
if RE.is_match(target) == false {
126+
if !RE.is_match(target) {
133127
return Err("[ERROR] No tokens!".to_string());
134128
}
135129
for cap in RE.captures_iter(target) {
@@ -151,7 +145,7 @@ fn get_token(target: &str) -> Result<Vec<TokenDesc>,String> {
151145
Ok(v)
152146
}
153147

154-
fn mul_phrase(phrase: &FormulaDesc) -> Result<String,String> {
148+
fn mul_phrase(phrase: &FormulaDesc) -> Result<String, String> {
155149
let mut v = get_token(&phrase.formula_self)?;
156150
for token in &mut v {
157151
token.times = match token.times.checked_mul(phrase.times) {
@@ -173,18 +167,18 @@ fn replace_phrase(target: &str, src: &str, des: &str) -> String {
173167

174168
fn parser_formula(
175169
// parse the chemical formula
176-
formula: &String,
170+
formula: &str,
177171
table: &mut TableDesc,
178172
location: usize,
179-
) -> Result<bool, String>{
173+
) -> Result<bool, String> {
180174
let formula_backup = formula;
181175
let mut formula = format!("({})", formula_backup);
182176

183-
while formula_spliter(&formula).is_ok() == true {
177+
while formula_spliter(&formula).is_ok() {
184178
for p in formula_spliter(&formula)? {
185179
formula = replace_phrase(&formula, &p.all, &(mul_phrase(&p)?));
186180
}
187181
}
188-
table.store_in_table(&formula,location)?;
182+
table.store_in_table(&formula, location)?;
189183
Ok(true)
190184
}

src/parser_mod/parser_struct.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ pub struct TableDesc {
3838
}
3939

4040
impl TableDesc {
41-
pub fn store_in_table(&mut self, formula: &String, location: usize) -> Result<bool, String> {
41+
pub fn store_in_table(&mut self, formula: &str, location: usize) -> Result<bool, String> {
4242
for t in get_token(formula)? {
43-
if self.find_element_in_table(&t.token_name).is_ok() == false {
43+
if !self.find_element_in_table(&t.token_name).is_ok() {
4444
let len = self.elements_table.len();
4545
self.elements_table.push(ElementStruct {
4646
name: t.token_name.clone(),
@@ -69,7 +69,8 @@ impl TableDesc {
6969
(self.list).to_vec()
7070
}
7171

72-
pub fn new(sum: i32) -> Self { // PLEASE call update_list_vec after new!
72+
pub fn new(sum: i32) -> Self {
73+
// PLEASE call update_list_vec after new!
7374
Self {
7475
elements_table: Vec::new(),
7576
list: Vec::new(),
@@ -90,7 +91,7 @@ impl TableDesc {
9091
v
9192
}
9293

93-
fn find_element_in_table(&self, target: &String) -> Result<usize, String> {
94+
fn find_element_in_table(&self, target: &str) -> Result<usize, String> {
9495
for i in &(self.elements_table) {
9596
if i.name == *target {
9697
return Ok(i.num);

tests/integration_test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ extern crate lib_xch;
1717

1818
mod testers;
1919

20-
use testers::{tester,tester_error};
20+
use testers::{tester, tester_error};
2121

2222
#[test]
2323
fn simples() {
24-
tester("H2O=H2+O2", 10, vec![2, 2, 1]);
24+
tester("H2O=H2+O2", 10, &[2, 2, 1]);
2525
}
2626

2727
#[test]
2828
fn high_performance() {
29-
tester("As2O3+Zn+HCl=AsH3+ZnCl2+H2O", 50, vec![1, 6, 12, 2, 6, 3]);
29+
tester("As2O3+Zn+HCl=AsH3+ZnCl2+H2O", 50, &[1, 6, 12, 2, 6, 3]);
3030
}
3131

3232
#[test]
3333
fn brackets() {
34-
tester("O2(O3(O)4O5(O))=O", 50, vec![1, 15]);
34+
tester("O2(O3(O)4O5(O))=O", 50, &[1, 15]);
3535
}
3636

3737
#[test]

0 commit comments

Comments
 (0)