liao_generator/generator/
mod.rs

1use self::{schema::generate_schema, term::generate_term};
2
3use crate::constants::{TermOptions, GenerateOptions, SchemaOptions, TermSchema};
4
5mod factor;
6mod schema;
7mod term;
8
9pub fn generate(options: &GenerateOptions) -> Vec<i64> {
10    // check simple rules
11    if options.min >= options.max {
12        // TODO: handle error here
13        panic!("options.min ({}) can't be greater then equal options.max ({})", options.min, options.max);
14    }
15
16    let mut terms: Vec<i64> = vec![];
17    let mut terms_sum: i64 = 0;
18
19    let formula = &options.formula;
20
21    let schema: Vec<TermSchema> = generate_schema(&SchemaOptions {
22        terms_len: options.len,
23    });
24
25    let mut cur_num: Vec<i64> = "0"
26        .repeat(options.max.to_string().len())
27        .split("")
28        .map(|x| x.to_string())
29        .filter(|x| x.len() > 0)
30        .map(|x| x.parse::<i64>().unwrap())
31        .collect();
32
33    for i in 0..options.len {
34        let term_options = TermOptions {
35            cur_num: cur_num.clone(),
36            formula: *formula,
37            terms_len: terms.len() as i64,
38            schema: schema.clone()[i],
39            max: options.max,
40            min: options.min,
41            previos_term: if i > 0 {
42                Some(terms[i - 1].abs())
43            } else {
44                None
45            },
46        };
47
48        let term: Option<i64> = generate_term(term_options);
49        if let Some(t) = term {
50            terms.push(t);
51            terms_sum += t;
52
53            cur_num = terms_sum
54                .to_string()
55                .replace("-", "")
56                .split("")
57                .map(|x| x.to_string())
58                .filter(|x| x.len() > 0)
59                .map(|x| x.parse::<i64>().unwrap())
60                .collect();
61
62            if options.max.to_string().clone().len() as isize - cur_num.clone().len() as isize > 0 {
63                cur_num = "0"
64                    .repeat(options.max.to_string().len() - cur_num.len())
65                    .split("")
66                    .map(|x| x.to_string())
67                    .filter(|x| x.len() > 0)
68                    .map(|x| x.parse::<i64>().unwrap())
69                    .chain(cur_num.into_iter())
70                    .collect();
71            }
72        }
73    }
74
75    terms
76}