Skip to main content

audiorouter_core/
error.rs

1//! Error categories for distinguishing exit codes.
2//!
3//! - [`ErrorKind::Config`] -> exit code 1 (config/validation failure)
4//! - [`ErrorKind::Runtime`] -> exit code 2 (runtime / device / I-O error)
5
6/// Error category carried alongside messages to select exit codes.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum ErrorKind {
9    /// Config validation or parse failure — exit code 1.
10    Config,
11    /// Runtime, device, or I-O error — exit code 2.
12    Runtime,
13}
14
15/// A message tagged with an [`ErrorKind`] so the caller can pick the right exit code.
16#[derive(Debug)]
17pub struct AppError {
18    pub kind: ErrorKind,
19    pub message: String,
20}
21
22impl AppError {
23    pub fn config(msg: impl Into<String>) -> Self {
24        Self {
25            kind: ErrorKind::Config,
26            message: msg.into(),
27        }
28    }
29
30    pub fn runtime(msg: impl Into<String>) -> Self {
31        Self {
32            kind: ErrorKind::Runtime,
33            message: msg.into(),
34        }
35    }
36}
37
38impl std::fmt::Display for AppError {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.write_str(&self.message)
41    }
42}
43
44impl std::error::Error for AppError {}
45
46/// The exit code associated with an [`ErrorKind`].
47pub fn exit_code_for(kind: ErrorKind) -> i32 {
48    match kind {
49        ErrorKind::Config => 1,
50        ErrorKind::Runtime => 2,
51    }
52}