浏览代码

don't prefix every error value with "Error:"

do it in the error handling, when printing things.
Lucas Stadler 10 年之前
父节点
当前提交
0a2b2faf84
共有 1 个文件被更改,包括 8 次插入8 次删除
  1. 8 8
      rust/dpll/cnf.rs

+ 8 - 8
rust/dpll/cnf.rs

@ -22,20 +22,20 @@ fn parse_dimac(dimac: &str) -> Result<CNF, String> {
22 22
    let mut num_clauses;
23 23
    
24 24
    match lines.next() {
25
        None => { return Err("Error: expected cnf description".to_string()) }
25
        None => { return Err("expected cnf description".to_string()) }
26 26
        Some(line) => {
27 27
            let desc: Vec<&str> = line.split(" ").collect();
28 28
            if desc.len() != 4 || desc[0] != "p" || desc[1] != "cnf" {
29
                return Err("Error: cnf description must be of the form 'p cnf <num vars> <num clauses>'".to_string())
29
                return Err("cnf description must be of the form 'p cnf <num vars> <num clauses>'".to_string())
30 30
            }
31 31
            match desc[2].parse::<u32>() {
32 32
                Ok(n) => { num_vars = n }
33
                Err(e) => { return Err(format!("Error: <num vars> must be a positive integer: {}", e)) }
33
                Err(e) => { return Err(format!("<num vars> must be a positive integer: {}", e)) }
34 34
            }
35 35
            
36 36
            match desc[3].parse::<u32>() {
37 37
                Ok(n) => { num_clauses = n }
38
                Err(e) => { return Err(format!("Error: <num clauses> must be a positive integer: {}", e)) }
38
                Err(e) => { return Err(format!("<num clauses> must be a positive integer: {}", e)) }
39 39
            }
40 40
            println!("cnf has {} variables and {} clauses", num_vars, num_clauses)
41 41
        }
@ -43,17 +43,17 @@ fn parse_dimac(dimac: &str) -> Result<CNF, String> {
43 43
44 44
    let clause_lines: Vec<&str> = lines.collect();
45 45
    if clause_lines.len() as u32 != num_clauses {
46
        return Err(format!("Error: Wrong number of clauses: Expected {}, but got {}", num_clauses, clause_lines.len()))
46
        return Err(format!("Wrong number of clauses: Expected {}, but got {}", num_clauses, clause_lines.len()))
47 47
    }
48 48
49 49
    let mut clauses: Vec<Vec<i32>> = Vec::with_capacity(num_clauses as usize);
50 50
    for clause_line in clause_lines {
51 51
        let mut vars: Vec<i32> = clause_line.split(" ").map(|x| x.parse::<i32>().unwrap()).collect();
52 52
        if vars.is_empty() {
53
            return Err("Error: empty clause".to_string())
53
            return Err("empty clause".to_string())
54 54
        }
55 55
        if vars[vars.len()-1] != 0 {
56
            return Err("Clause must be terminated with 0".to_string())
56
            return Err("clause must be terminated with 0".to_string())
57 57
        }
58 58
        let l = vars.len();
59 59
        vars.truncate(l - 1);
@ -70,7 +70,7 @@ fn main() {
70 70
    match io::stdin().read_to_string(input) {
71 71
        Ok(_) => match parse_dimac(input) {
72 72
            Ok(cnf) => { println!("ok!") }
73
            Err(e) => { println!("{}", e) }
73
            Err(e) => { println!("Error: {}", e) }
74 74
        },
75 75
        Err(e) => { println!("Error: {}", e) }
76 76
    }