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