1
0
Fork 0

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
1 changed files with 9 additions and 11 deletions

View File

@ -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)
}
} }
// Im no math nerd but I want the highest possible approximation of 0.9 // Im 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);
}, },
}; };
} }