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    DockerImageNotConfigured,
17    UnsupportedIsolationPolicy,
18    SpawnConflict,
19    ProtocolMismatch,
20}
21
22impl ErrorCode {
23    pub fn as_str(self) -> &'static str {
24        match self {
25            Self::RuntimeUnavailable => "runtime_unavailable",
26            Self::SessionNotFound => "session_not_found",
27            Self::TmuxPaneDead => "tmux_pane_dead",
28            Self::HeadlessNudgeUnsupported => "headless_nudge_unsupported",
29            Self::LaunchFailed => "launch_failed",
30            Self::InvalidTarget => "invalid_target",
31            Self::DockerImageNotConfigured => "docker_image_not_configured",
32            Self::UnsupportedIsolationPolicy => "unsupported_isolation_policy",
33            Self::SpawnConflict => "spawn_conflict",
34            Self::ProtocolMismatch => "protocol_mismatch",
35        }
36    }
37}
38
39impl Display for ErrorCode {
40    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
41        formatter.write_str(self.as_str())
42    }
43}
44
45#[derive(Debug, Error)]
46pub enum ProtocolError {
47    #[error("connection closed before a message arrived")]
48    Eof,
49    #[error("io error: {0}")]
50    Io(#[from] std::io::Error),
51    #[error("json error: {0}")]
52    Json(#[from] serde_json::Error),
53    #[error("unsupported runtime protocol version: expected {expected}, got {got}")]
54    UnsupportedVersion { expected: &'static str, got: String },
55}
56
57#[derive(Debug, Error)]
58#[error("unsupported runtime kind: {0}")]
59pub struct RuntimeKindParseError(pub String);