Skip to main content

optimization_job_api/
lib.rs

1#![doc = include_str!("../README.md")]
2//!
3//! The [`legacy`] module keeps compatibility with the earlier `fcmaes.job/0.1`
4//! format; its numerical semantics are unchanged.
5pub mod artifact;
6pub mod legacy;
7pub mod process;
8pub mod result;
9pub mod spec;
10pub mod storage;
11pub mod worker;
12pub use spec::*;
13pub type Error = Box<dyn std::error::Error + Send + Sync>;
14pub type Result<T> = std::result::Result<T, Error>;
15
16/// An error with a stable, machine-readable code. The runner prints the code
17/// next to the message; the [guide](https://github.com/dietmarwo/optimization-jobs/blob/main/docs/job-api.md#errors) lists every code.
18#[derive(Debug)]
19pub struct Coded {
20    pub code: &'static str,
21    pub message: String,
22}
23impl std::fmt::Display for Coded {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        f.write_str(&self.message)
26    }
27}
28impl std::error::Error for Coded {}
29/// An [`Error`] carrying `code`.
30pub fn coded(code: &'static str, message: impl Into<String>) -> Error {
31    Box::new(Coded {
32        code,
33        message: message.into(),
34    })
35}
36/// The code of `error`, `runner_error` if it has none.
37pub fn error_code(error: &Error) -> &'static str {
38    error
39        .downcast_ref::<Coded>()
40        .map_or("runner_error", |c| c.code)
41}
42/// True for error codes after which the identical command can succeed later
43/// without any change: only `attempt_active` (wait for the processes of the
44/// attempt to exit). Every other error needs a different request, a
45/// re-registered worker or operator action.
46pub fn retryable(code: &str) -> bool {
47    code == "attempt_active"
48}