Skip to main content

lilo_rm_core/
error.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
7#[non_exhaustive]
8#[serde(rename_all = "snake_case")]
9pub enum ErrorCode {
10    RuntimeUnavailable,
11    SessionNotFound,
12    TmuxPaneDead,
13    HeadlessNudgeUnsupported,
14    LaunchFailed,
15    InvalidTarget,
16    SpawnConflict,
17    ProtocolMismatch,
18}
19
20impl ErrorCode {
21    pub fn as_str(self) -> &'static str {
22        match self {
23            Self::RuntimeUnavailable => "runtime_unavailable",
24            Self::SessionNotFound => "session_not_found",
25            Self::TmuxPaneDead => "tmux_pane_dead",
26            Self::HeadlessNudgeUnsupported => "headless_nudge_unsupported",
27            Self::LaunchFailed => "launch_failed",
28            Self::InvalidTarget => "invalid_target",
29            Self::SpawnConflict => "spawn_conflict",
30            Self::ProtocolMismatch => "protocol_mismatch",
31        }
32    }
33}
34
35impl Display for ErrorCode {
36    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
37        formatter.write_str(self.as_str())
38    }
39}
40
41#[derive(Debug, Error)]
42pub enum ProtocolError {
43    #[error("connection closed before a message arrived")]
44    Eof,
45    #[error("io error: {0}")]
46    Io(#[from] std::io::Error),
47    #[error("json error: {0}")]
48    Json(#[from] serde_json::Error),
49    #[error("unsupported runtime protocol version: expected {expected}, got {got}")]
50    UnsupportedVersion { expected: &'static str, got: String },
51}
52
53#[derive(Debug, Error)]
54#[error("unsupported runtime kind: {0}")]
55pub struct RuntimeKindParseError(pub String);