use std::ffi::OsString;
#[derive(Debug)]
pub struct ServerBootstrapError {
reason: String,
}
impl ServerBootstrapError {
fn from_dispatch(error: &anyhow::Error) -> Self {
Self {
reason: format!("{error:#}"),
}
}
}
impl std::fmt::Display for ServerBootstrapError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.reason)
}
}
impl std::error::Error for ServerBootstrapError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerExit {
RefusedToStart,
Success,
}
pub fn run(args: Vec<OsString>) -> Result<ServerExit, ServerBootstrapError> {
match crate::cli::entry::run_main(args) {
Ok(crate::cli::entry::MainExit::Success) => Ok(ServerExit::Success),
Ok(crate::cli::entry::MainExit::ServeRefusedToStart) => Ok(ServerExit::RefusedToStart),
Err(error) => Err(ServerBootstrapError::from_dispatch(&error)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wired_run_dispatches_and_the_contract_shapes_hold() {
let exit = run(vec![
OsString::from("symforge"),
OsString::from("--version"),
])
.expect("the --version fast path succeeds");
assert_eq!(exit, ServerExit::Success);
let exits = [ServerExit::RefusedToStart, ServerExit::Success];
for exit in exits {
match exit {
ServerExit::RefusedToStart | ServerExit::Success => {}
}
}
fn all_five_auto<
T: Send + Sync + Unpin + std::panic::RefUnwindSafe + std::panic::UnwindSafe,
>() {
}
all_five_auto::<ServerBootstrapError>();
all_five_auto::<ServerExit>();
let refusal = ServerBootstrapError::from_dispatch(&anyhow::anyhow!("boom"));
assert!(format!("{refusal}").contains("boom"));
let _as_error: &dyn std::error::Error = &refusal;
}
#[test]
fn bootstrap_error_is_an_opaque_struct_not_an_enum() {
let source = include_str!("server_api.rs");
let struct_needle = format!("\n{} ServerBootstrapError", "pub struct");
let enum_needle = format!("\n{} ServerBootstrapError", "pub enum");
assert!(
source.contains(&struct_needle),
"the contract pins kind: struct"
);
assert!(
!source.contains(&enum_needle),
"an enum here is the invented-surface defect the contract's \
opaque struct exists to prevent"
);
let declaration = source
.split(struct_needle.as_str())
.nth(1)
.expect("declaration present");
let after_name = declaration.trim_start();
assert!(
after_name.starts_with('{'),
"the declaration must open a brace body immediately — a tuple \
struct's parenthesized fields would evade the field scan"
);
let body_end = after_name.find('}').expect("struct body closes");
let body = &after_name[1..body_end];
assert!(
!body.contains("pub "),
"has_nonpublic_fields: every field stays private, got: {body}"
);
}
}