rpn(1): made EvaluationError struct contain exit code
This commit is contained in:
parent
8e0eaeab38
commit
d768a257a3
20
src/rpn.rs
20
src/rpn.rs
|
@ -112,19 +112,15 @@ impl Display for CalcType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct EvaluationError { pub message: String }
|
struct EvaluationError {
|
||||||
|
message: String,
|
||||||
impl Display for EvaluationError {
|
code: i32,
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// I’m no math nerd but I want the highest possible approximation of 0.9
|
// I’m no math nerd but I want the highest possible approximation of 0.9
|
||||||
// repeating and it seems this can give it to me
|
// repeating and it seems this can give it to me
|
||||||
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
|
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
|
||||||
|
|
||||||
|
|
||||||
fn eval(
|
fn eval(
|
||||||
input: &str,
|
input: &str,
|
||||||
initial_stack: VecDeque<f64>,
|
initial_stack: VecDeque<f64>,
|
||||||
|
@ -150,7 +146,8 @@ fn eval(
|
||||||
Val(v) => stack.push_back(v),
|
Val(v) => stack.push_back(v),
|
||||||
Invalid(i) => {
|
Invalid(i) => {
|
||||||
return Err(EvaluationError {
|
return Err(EvaluationError {
|
||||||
message: format!("{}: Invalid token", i)
|
message: format!("{}: Invalid token", i),
|
||||||
|
code: EX_DATAERR,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
op => {
|
op => {
|
||||||
|
@ -175,7 +172,8 @@ fn eval(
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return Err(EvaluationError {
|
return Err(EvaluationError {
|
||||||
message: format!("{}: Unexpected operation.", op)
|
message: format!("{}: Unexpected operation.", op),
|
||||||
|
code: EX_DATAERR,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -217,7 +215,7 @@ fn main() -> ExitCode {
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}: {}", argv[0], err.message);
|
eprintln!("{}: {}", argv[0], err.message);
|
||||||
return ExitCode::from(EX_DATAERR as u8);
|
return ExitCode::from(err.code as u8);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -242,7 +240,7 @@ fn main() -> ExitCode {
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}: {}", argv[0], err.message);
|
eprintln!("{}: {}", argv[0], err.message);
|
||||||
return ExitCode::from(EX_DATAERR as u8);
|
return ExitCode::from(err.code as u8);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user