rpn(1): made EvaluationError struct contain exit code

This commit is contained in:
Emma Tebibyte 2024-02-06 23:29:43 -07:00
parent 8e0eaeab38
commit d768a257a3
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -112,19 +112,15 @@ impl Display for CalcType {
}
#[derive(Debug, Clone)]
struct EvaluationError { pub message: String }
impl Display for EvaluationError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
struct EvaluationError {
message: String,
code: i32,
}
// Im no math nerd but I want the highest possible approximation of 0.9
// repeating and it seems this can give it to me
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
fn eval(
input: &str,
initial_stack: VecDeque<f64>,
@ -150,7 +146,8 @@ fn eval(
Val(v) => stack.push_back(v),
Invalid(i) => {
return Err(EvaluationError {
message: format!("{}: Invalid token", i)
message: format!("{}: Invalid token", i),
code: EX_DATAERR,
})
},
op => {
@ -175,7 +172,8 @@ fn eval(
};
} else {
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) => {
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) => {
eprintln!("{}: {}", argv[0], err.message);
return ExitCode::from(EX_DATAERR as u8);
return ExitCode::from(err.code as u8);
},
};
}