File size: 3,196 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::LodaCppError;
use loda_rust_core::util::BigIntVec;
use num_bigint::BigInt;
use std::error::Error;

pub struct LodaCppEvalTerms {
    terms: BigIntVec,
}

impl LodaCppEvalTerms {
    fn new(terms: BigIntVec) -> Self {
        Self {
            terms: terms,
        }
    }

    pub fn parse(raw_output_from_lodacpp: &String, term_count: usize) -> Result<LodaCppEvalTerms, Box<dyn Error>> {
        let trimmed_output: String = raw_output_from_lodacpp.trim().to_string();
        if trimmed_output.is_empty() {
            error!("No output to parse.");
            return Err(Box::new(LodaCppError::NoOutput));
        }
        let term_strings = trimmed_output.split(",");
        let mut terms_bigintvec = BigIntVec::with_capacity(term_count);
        for term_string in term_strings {
            if term_string.starts_with("+") {
                error!("Positive number should not start with plus symbol. '{}'", term_string);
                return Err(Box::new(LodaCppError::ParseTerms));
            }
            let bytes: &[u8] = term_string.as_bytes();
            let bigint: BigInt = match BigInt::parse_bytes(bytes, 10) {
                Some(value) => value,
                None => {
                    error!("Unable to parse a number as BigInt. '{}'", term_string);
                    return Err(Box::new(LodaCppError::ParseTerms));
                }
            };
            terms_bigintvec.push(bigint);
        };
        Ok(LodaCppEvalTerms::new(terms_bigintvec))
    }

    #[allow(dead_code)]
    pub fn terms(&self) -> &BigIntVec {
        &self.terms
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use loda_rust_core::util::BigIntVecToString;

    fn parse(input: &str) -> String {
        let s = input.to_string();
        let evalterms: LodaCppEvalTerms = match LodaCppEvalTerms::parse(&s, 0) {
            Ok(value) => value,
            Err(error) => {
                if let Some(lodacpp_error) = error.downcast_ref::<LodaCppError>() {
                    if LodaCppError::NoOutput == *lodacpp_error {
                        return "ERROR NoOutput".to_string();
                    }
                    if LodaCppError::ParseTerms == *lodacpp_error {
                        return "ERROR ParseTerms".to_string();
                    }
                    return format!("ERROR LODACPP: {:?}", lodacpp_error);
                }
                return format!("ERROR OTHER: {:?}", error);
            }
        };
        evalterms.terms().to_compact_comma_string()
    }

    #[test]
    fn test_10000_parse_ok() {
        assert_eq!(parse("-1,2,-3,4"), "-1,2,-3,4");
        assert_eq!(parse("\n  42\t  "), "42");
        assert_eq!(parse("0"), "0");
        assert_eq!(parse("0,0"), "0,0");
        assert_eq!(parse("0000555"), "555");
    }
    
    #[test]
    fn test_20000_parse_error() {
        assert_eq!(parse(""), "ERROR NoOutput");
        assert_eq!(parse(" "), "ERROR NoOutput");
        assert_eq!(parse(" \n "), "ERROR NoOutput");
        assert_eq!(parse("1,2,overflow"), "ERROR ParseTerms");
        assert_eq!(parse("c++ exception"), "ERROR ParseTerms");
        assert_eq!(parse("+123"), "ERROR ParseTerms");
    }
}