Skip to main content

atm_error/
error_codes.rs

1//! Stable ATM-owned error-code registry.
2
3use std::fmt;
4use std::str::FromStr;
5
6use serde::{Deserialize, Deserializer, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct UnknownAtmErrorCode(pub String);
10
11impl fmt::Display for UnknownAtmErrorCode {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(f, "unknown ATM error code: {}", self.0)
14    }
15}
16
17impl std::error::Error for UnknownAtmErrorCode {}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub enum AtmErrorCode {
21    ConfigHomeUnavailable,
22    AtmHomeUnresolved,
23    ConfigParseFailed,
24    ConfigRetiredHookMembersKey,
25    ConfigRetiredLegacyHookKeys,
26    ConfigTeamParseFailed,
27    ConfigTeamMissing,
28    IdentityUnavailable,
29    IdentityInvalid,
30    IdentityConflict,
31    MemberAlreadyExists,
32    MemberNotFound,
33    DaemonUnavailable,
34    RuntimeRootInvalid,
35    RuntimeBootstrapRefused,
36    SocketOverrideForbidden,
37    DaemonMayHaveExecuted,
38    DaemonLifecycleWedge,
39    DaemonLaunchGateRejected,
40    DaemonServingStateRejected,
41    DaemonStaleOwnerRecoveryFailed,
42    DaemonAutoStartFailed,
43    DaemonConnectionSaturated,
44    RemoteDeliveryUnconfirmed,
45    /// The daemon launch argument did not name a supported peer-wire mode.
46    PeerWireModeInvalid,
47    /// A non-launch source attempted to select the daemon peer-wire mode.
48    PeerWireModeSourceForbidden,
49    /// Plaintext diagnostic mode was asked to satisfy authenticated peer work.
50    PeerWirePlaintextAuthenticationRequired,
51    PeerConfigValidationFailed,
52    CertificateOperationFailed,
53    /// A configured peer did not satisfy exact mTLS hostname or pin authentication.
54    PeerAuthenticationFailed,
55    /// A bounded peer transport operation did not finish before its deadline.
56    TransportTimeout,
57    /// A peer transport handshake failed outside configuration or pin authentication.
58    TransportProtocolFailed,
59    BindPreflightFailed,
60    ClientDaemonVersionIncompatible,
61    DaemonAdvisorySessionAlreadyRegistered,
62    DaemonAdvisorySessionNotRegistered,
63    DaemonAdvisorySessionCleanupFailed,
64    AddressParseFailed,
65    TeamUnavailable,
66    TeamInvalid,
67    TeamNotFound,
68    AgentNotFound,
69    MailboxReadFailed,
70    MailboxWriteFailed,
71    MailboxLockFailed,
72    MailboxLockReadOnlyFilesystem,
73    MailboxLockTimeout,
74    InternalError,
75    MessageValidationFailed,
76    /// Search is intentionally local-only until a separately authorized peer
77    /// query protocol exists.
78    SearchLocalOnly,
79    LocalHttpCapabilityInvalid,
80    LocalHttpEndpointSchemaUnsupported,
81    LocalHttpEndpointMissing,
82    LocalHttpEndpointNonLoopback,
83    LocalHttpRuntimeDirectoryMissing,
84    LocalHttpCapabilityRevoked,
85    MessageIdConflict,
86    SelfAddressedSendInvalid,
87    EmptyNudgeTemplateBody,
88    CallerContextRequestInvalid,
89    /// Template catalog content must be strict UTF-8 before any row is admitted.
90    TemplateContentNotUtf8,
91    /// A template passed to the no-include render operation references another template.
92    DecomposedTemplateIncludeForbidden,
93    TemplateLoadFailed,
94    TemplateHashApiFailed,
95    /// The upstream template inspection parser rejected frontmatter or body/directive syntax.
96    TemplateInspectionParseFailed,
97    TemplateRequiredVariableMissing,
98    TemplateRenderVerificationFailed,
99    TemplateIncludeUnresolved,
100    TemplateClassificationInvalid,
101    TemplateWorkflowInvalid,
102    TemplateWorkflowValueInvalid,
103    TemplateTagReserved,
104    /// A bounded local workflow query or lifecycle projection is invalid.
105    WorkflowQueryInvalid,
106    /// Runtime telemetry configuration failed validation and was disabled.
107    WorkflowTelemetryConfigInvalid,
108    /// A best-effort workflow telemetry record was dropped.
109    WorkflowTelemetryDropped,
110    SerializationFailed,
111    FilePolicyRejected,
112    FileReferenceRewriteFailed,
113    WaitTimeout,
114    AckInvalidState,
115    ClearInvalidState,
116    ObservabilityEmitFailed,
117    ObservabilityQueryFailed,
118    ObservabilityFollowFailed,
119    ObservabilityHealthFailed,
120    ObservabilityBootstrapFailed,
121    ObservabilityHealthOk,
122    WarningInvalidTeamMemberSkipped,
123    WarningMailboxRecordSkipped,
124    WarningMalformedAtmFieldIgnored,
125    WarningObservabilityHealthDegraded,
126    WarningSqliteHealthDegraded,
127    WarningOriginInboxEntrySkipped,
128    WarningMissingTeamConfigFallback,
129    WarningSendAlertStateDegraded,
130    WarningIdentityDrift,
131    WarningRosterDrift,
132    WarningBaselineMemberMissing,
133    WarningRestoreInProgress,
134    WarningStaleMailboxLock,
135    WarningHookSkipped,
136    WarningHookExecutionFailed,
137    PostSendPaneMissing,
138    PostSendTmuxSendFailed,
139    PostSendGraftUnavailable,
140    GraftReceiverAlreadyActive,
141    PostSendAdvisoryDeliveryFailed,
142    TestFakeTransportInjectionFailed,
143    HelpTopicNotFound,
144}
145
146impl AtmErrorCode {
147    pub fn as_str(self) -> &'static str {
148        if let Some(value) = self.config_or_identity_str() {
149            return value;
150        }
151        if let Some(value) = self.daemon_or_address_str() {
152            return value;
153        }
154        if let Some(value) = self.mailbox_or_validation_str() {
155            return value;
156        }
157        if let Some(value) = self.observability_or_warning_str() {
158            return value;
159        }
160        self.post_send_or_misc_str()
161    }
162
163    fn config_or_identity_str(self) -> Option<&'static str> {
164        Some(match self {
165            Self::ConfigHomeUnavailable => "ATM_CONFIG_HOME_UNAVAILABLE",
166            Self::AtmHomeUnresolved => "ATM_HOME_UNRESOLVED",
167            Self::ConfigParseFailed => "ATM_CONFIG_PARSE_FAILED",
168            Self::ConfigRetiredHookMembersKey => "ATM_CONFIG_RETIRED_HOOK_MEMBERS_KEY",
169            Self::ConfigRetiredLegacyHookKeys => "ATM_CONFIG_RETIRED_LEGACY_HOOK_KEYS",
170            Self::ConfigTeamParseFailed => "ATM_CONFIG_TEAM_PARSE_FAILED",
171            Self::ConfigTeamMissing => "ATM_CONFIG_TEAM_MISSING",
172            Self::IdentityUnavailable => "ATM_IDENTITY_UNAVAILABLE",
173            Self::IdentityInvalid => "ATM_IDENTITY_INVALID",
174            Self::IdentityConflict => "ATM_IDENTITY_CONFLICT",
175            Self::MemberAlreadyExists => "ATM_MEMBER_ALREADY_EXISTS",
176            Self::MemberNotFound => "ATM_MEMBER_NOT_FOUND",
177            _ => return None,
178        })
179    }
180
181    fn daemon_or_address_str(self) -> Option<&'static str> {
182        Some(match self {
183            Self::DaemonUnavailable => "ATM_DAEMON_UNAVAILABLE",
184            Self::RuntimeRootInvalid => "ATM_RUNTIME_ROOT_INVALID",
185            Self::RuntimeBootstrapRefused => "ATM_RUNTIME_BOOTSTRAP_REFUSED",
186            Self::SocketOverrideForbidden => "ATM_SOCKET_OVERRIDE_FORBIDDEN",
187            Self::DaemonMayHaveExecuted => "ATM_DAEMON_MAY_HAVE_EXECUTED",
188            Self::DaemonLifecycleWedge => "ATM_DAEMON_LIFECYCLE_WEDGE",
189            Self::DaemonLaunchGateRejected => "ATM_DAEMON_LAUNCH_GATE_REJECTED",
190            Self::DaemonServingStateRejected => "ATM_DAEMON_SERVING_STATE_REJECTED",
191            Self::DaemonStaleOwnerRecoveryFailed => "ATM_DAEMON_STALE_OWNER_RECOVERY_FAILED",
192            Self::DaemonAutoStartFailed => "ATM_DAEMON_AUTO_START_FAILED",
193            Self::DaemonConnectionSaturated => "ATM_DAEMON_CONNECTION_SATURATED",
194            Self::RemoteDeliveryUnconfirmed => "REMOTE_DELIVERY_UNCONFIRMED",
195            Self::PeerWireModeInvalid => "ATM_PEER_WIRE_MODE_INVALID",
196            Self::PeerWireModeSourceForbidden => "ATM_PEER_WIRE_MODE_SOURCE_FORBIDDEN",
197            Self::PeerWirePlaintextAuthenticationRequired => {
198                "ATM_PEER_WIRE_PLAINTEXT_AUTHENTICATION_REQUIRED"
199            }
200            Self::PeerConfigValidationFailed => "ATM_PEER_CONFIG_VALIDATION_FAILED",
201            Self::CertificateOperationFailed => "ATM_CERTIFICATE_OPERATION_FAILED",
202            Self::PeerAuthenticationFailed => "ATM_PEER_AUTHENTICATION_FAILED",
203            Self::TransportTimeout => "ATM_TRANSPORT_TIMEOUT",
204            Self::TransportProtocolFailed => "ATM_TRANSPORT_PROTOCOL_FAILED",
205            Self::BindPreflightFailed => "ATM_BIND_PREFLIGHT_FAILED",
206            Self::ClientDaemonVersionIncompatible => "ATM_CLIENT_DAEMON_VERSION_INCOMPATIBLE",
207            Self::DaemonAdvisorySessionAlreadyRegistered => {
208                "ATM_DAEMON_ADVISORY_SESSION_ALREADY_REGISTERED"
209            }
210            Self::DaemonAdvisorySessionNotRegistered => {
211                "ATM_DAEMON_ADVISORY_SESSION_NOT_REGISTERED"
212            }
213            Self::DaemonAdvisorySessionCleanupFailed => {
214                "ATM_DAEMON_ADVISORY_SESSION_CLEANUP_FAILED"
215            }
216            Self::AddressParseFailed => "ATM_ADDRESS_PARSE_FAILED",
217            Self::TeamUnavailable => "ATM_TEAM_UNAVAILABLE",
218            Self::TeamInvalid => "ATM_TEAM_INVALID",
219            Self::TeamNotFound => "ATM_TEAM_NOT_FOUND",
220            Self::AgentNotFound => "ATM_AGENT_NOT_FOUND",
221            _ => return None,
222        })
223    }
224
225    fn mailbox_or_validation_str(self) -> Option<&'static str> {
226        Some(match self {
227            Self::MailboxReadFailed => "ATM_MAILBOX_READ_FAILED",
228            Self::MailboxWriteFailed => "ATM_MAILBOX_WRITE_FAILED",
229            Self::MailboxLockFailed => "ATM_MAILBOX_LOCK_FAILED",
230            Self::MailboxLockReadOnlyFilesystem => "ATM_MAILBOX_LOCK_READ_ONLY_FILESYSTEM",
231            Self::MailboxLockTimeout => "ATM_MAILBOX_LOCK_TIMEOUT",
232            Self::InternalError => "ATM_INTERNAL_ERROR",
233            Self::MessageValidationFailed => "ATM_MESSAGE_VALIDATION_FAILED",
234            Self::SearchLocalOnly => "ATM_SEARCH_LOCAL_ONLY",
235            Self::LocalHttpCapabilityInvalid => "ATM_LOCAL_HTTP_CAPABILITY_INVALID",
236            Self::LocalHttpEndpointSchemaUnsupported => {
237                "ATM_LOCAL_HTTP_ENDPOINT_SCHEMA_UNSUPPORTED"
238            }
239            Self::LocalHttpEndpointMissing => "ATM_LOCAL_HTTP_ENDPOINT_MISSING",
240            Self::LocalHttpEndpointNonLoopback => "ATM_LOCAL_HTTP_ENDPOINT_NON_LOOPBACK",
241            Self::LocalHttpRuntimeDirectoryMissing => "ATM_LOCAL_HTTP_RUNTIME_DIRECTORY_MISSING",
242            Self::LocalHttpCapabilityRevoked => "ATM_LOCAL_HTTP_CAPABILITY_REVOKED",
243            Self::MessageIdConflict => "ATM_MESSAGE_ID_CONFLICT",
244            Self::SelfAddressedSendInvalid => "ATM_SELF_ADDRESSED_SEND_INVALID",
245            Self::EmptyNudgeTemplateBody => "ATM_NUDGE_TEMPLATE_BODY_EMPTY",
246            Self::CallerContextRequestInvalid => "ATM_CALLER_CONTEXT_REQUEST_INVALID",
247            Self::TemplateContentNotUtf8 => "TEMPLATE_CONTENT_NOT_UTF8",
248            Self::DecomposedTemplateIncludeForbidden => "DECOMPOSED_TEMPLATE_INCLUDE_FORBIDDEN",
249            Self::TemplateLoadFailed => "TEMPLATE_LOAD_FAILED",
250            Self::TemplateHashApiFailed => "TEMPLATE_HASH_API_FAILED",
251            Self::TemplateInspectionParseFailed => "TEMPLATE_INSPECTION_PARSE_FAILED",
252            Self::TemplateRequiredVariableMissing => "TEMPLATE_REQUIRED_VARIABLE_MISSING",
253            Self::TemplateRenderVerificationFailed => "TEMPLATE_RENDER_VERIFICATION_FAILED",
254            Self::TemplateIncludeUnresolved => "TEMPLATE_INCLUDE_UNRESOLVED",
255            Self::TemplateClassificationInvalid => "TEMPLATE_CLASSIFICATION_INVALID",
256            Self::TemplateWorkflowInvalid => "TEMPLATE_WORKFLOW_INVALID",
257            Self::TemplateWorkflowValueInvalid => "TEMPLATE_WORKFLOW_VALUE_INVALID",
258            Self::TemplateTagReserved => "TEMPLATE_TAG_RESERVED",
259            Self::WorkflowQueryInvalid => "ATM_WORKFLOW_QUERY_INVALID",
260            Self::WorkflowTelemetryConfigInvalid => "ATM_WORKFLOW_TELEMETRY_CONFIG_INVALID",
261            Self::WorkflowTelemetryDropped => "ATM_WORKFLOW_TELEMETRY_DROPPED",
262            Self::SerializationFailed => "ATM_SERIALIZATION_FAILED",
263            Self::FilePolicyRejected => "ATM_FILE_POLICY_REJECTED",
264            Self::FileReferenceRewriteFailed => "ATM_FILE_REFERENCE_REWRITE_FAILED",
265            Self::WaitTimeout => "ATM_WAIT_TIMEOUT",
266            Self::AckInvalidState => "ATM_ACK_INVALID_STATE",
267            Self::ClearInvalidState => "ATM_CLEAR_INVALID_STATE",
268            _ => return None,
269        })
270    }
271
272    fn observability_or_warning_str(self) -> Option<&'static str> {
273        Some(match self {
274            Self::ObservabilityEmitFailed => "ATM_OBSERVABILITY_EMIT_FAILED",
275            Self::ObservabilityQueryFailed => "ATM_OBSERVABILITY_QUERY_FAILED",
276            Self::ObservabilityFollowFailed => "ATM_OBSERVABILITY_FOLLOW_FAILED",
277            Self::ObservabilityHealthFailed => "ATM_OBSERVABILITY_HEALTH_FAILED",
278            Self::ObservabilityBootstrapFailed => "ATM_OBSERVABILITY_BOOTSTRAP_FAILED",
279            Self::ObservabilityHealthOk => "ATM_OBSERVABILITY_HEALTH_OK",
280            Self::WarningInvalidTeamMemberSkipped => "ATM_WARNING_INVALID_TEAM_MEMBER_SKIPPED",
281            Self::WarningMailboxRecordSkipped => "ATM_WARNING_MAILBOX_RECORD_SKIPPED",
282            Self::WarningMalformedAtmFieldIgnored => "ATM_WARNING_MALFORMED_ATM_FIELD_IGNORED",
283            Self::WarningObservabilityHealthDegraded => "ATM_WARNING_OBSERVABILITY_HEALTH_DEGRADED",
284            Self::WarningSqliteHealthDegraded => "ATM_WARNING_SQLITE_HEALTH_DEGRADED",
285            Self::WarningOriginInboxEntrySkipped => "ATM_WARNING_ORIGIN_INBOX_ENTRY_SKIPPED",
286            Self::WarningMissingTeamConfigFallback => "ATM_WARNING_MISSING_TEAM_CONFIG_FALLBACK",
287            Self::WarningSendAlertStateDegraded => "ATM_WARNING_SEND_ALERT_STATE_DEGRADED",
288            Self::WarningIdentityDrift => "ATM_WARNING_IDENTITY_DRIFT",
289            Self::WarningRosterDrift => "ATM_WARNING_ROSTER_DRIFT",
290            Self::WarningBaselineMemberMissing => "ATM_WARNING_BASELINE_MEMBER_MISSING",
291            Self::WarningRestoreInProgress => "ATM_WARNING_RESTORE_IN_PROGRESS",
292            Self::WarningStaleMailboxLock => "ATM_WARNING_STALE_MAILBOX_LOCK",
293            Self::WarningHookSkipped => "ATM_WARNING_HOOK_SKIPPED",
294            Self::WarningHookExecutionFailed => "ATM_WARNING_HOOK_EXECUTION_FAILED",
295            _ => return None,
296        })
297    }
298
299    fn post_send_or_misc_str(self) -> &'static str {
300        match self {
301            Self::PostSendPaneMissing => "ATM_POST_SEND_PANE_MISSING",
302            Self::PostSendTmuxSendFailed => "ATM_POST_SEND_TMUX_SEND_FAILED",
303            Self::PostSendGraftUnavailable => "ATM_POST_SEND_GRAFT_UNAVAILABLE",
304            Self::GraftReceiverAlreadyActive => "ATM_GRAFT_RECEIVER_ALREADY_ACTIVE",
305            Self::PostSendAdvisoryDeliveryFailed => "ATM_POST_SEND_ADVISORY_DELIVERY_FAILED",
306            Self::TestFakeTransportInjectionFailed => "ATM_TEST_FAKE_TRANSPORT_INJECTION_FAILED",
307            Self::HelpTopicNotFound => "ATM_HELP_TOPIC_NOT_FOUND",
308            _ => unreachable!("all AtmErrorCode variants must be covered by as_str helpers"),
309        }
310    }
311}
312
313impl FromStr for AtmErrorCode {
314    type Err = UnknownAtmErrorCode;
315
316    fn from_str(value: &str) -> Result<Self, Self::Err> {
317        parse_known_error_code(value).ok_or_else(|| UnknownAtmErrorCode(value.to_owned()))
318    }
319}
320
321fn parse_known_error_code(value: &str) -> Option<AtmErrorCode> {
322    parse_config_or_identity_code(value)
323        .or_else(|| parse_daemon_or_address_code(value))
324        .or_else(|| parse_mailbox_or_validation_code(value))
325        .or_else(|| parse_observability_or_warning_code(value))
326        .or_else(|| parse_post_send_or_misc_code(value))
327}
328
329fn parse_config_or_identity_code(value: &str) -> Option<AtmErrorCode> {
330    Some(match value {
331        "ATM_CONFIG_HOME_UNAVAILABLE" => AtmErrorCode::ConfigHomeUnavailable,
332        "ATM_HOME_UNRESOLVED" => AtmErrorCode::AtmHomeUnresolved,
333        "ATM_CONFIG_PARSE_FAILED" => AtmErrorCode::ConfigParseFailed,
334        "ATM_CONFIG_RETIRED_HOOK_MEMBERS_KEY" => AtmErrorCode::ConfigRetiredHookMembersKey,
335        "ATM_CONFIG_RETIRED_LEGACY_HOOK_KEYS" => AtmErrorCode::ConfigRetiredLegacyHookKeys,
336        "ATM_CONFIG_TEAM_PARSE_FAILED" => AtmErrorCode::ConfigTeamParseFailed,
337        "ATM_CONFIG_TEAM_MISSING" => AtmErrorCode::ConfigTeamMissing,
338        "ATM_IDENTITY_UNAVAILABLE" => AtmErrorCode::IdentityUnavailable,
339        "ATM_IDENTITY_INVALID" => AtmErrorCode::IdentityInvalid,
340        "ATM_IDENTITY_CONFLICT" => AtmErrorCode::IdentityConflict,
341        "ATM_MEMBER_ALREADY_EXISTS" => AtmErrorCode::MemberAlreadyExists,
342        "ATM_MEMBER_NOT_FOUND" => AtmErrorCode::MemberNotFound,
343        _ => return None,
344    })
345}
346
347fn parse_daemon_or_address_code(value: &str) -> Option<AtmErrorCode> {
348    Some(match value {
349        "ATM_DAEMON_UNAVAILABLE" => AtmErrorCode::DaemonUnavailable,
350        "ATM_RUNTIME_ROOT_INVALID" => AtmErrorCode::RuntimeRootInvalid,
351        "ATM_RUNTIME_BOOTSTRAP_REFUSED" => AtmErrorCode::RuntimeBootstrapRefused,
352        "ATM_SOCKET_OVERRIDE_FORBIDDEN" => AtmErrorCode::SocketOverrideForbidden,
353        "ATM_DAEMON_MAY_HAVE_EXECUTED" => AtmErrorCode::DaemonMayHaveExecuted,
354        "ATM_DAEMON_LIFECYCLE_WEDGE" => AtmErrorCode::DaemonLifecycleWedge,
355        "ATM_DAEMON_LAUNCH_GATE_REJECTED" => AtmErrorCode::DaemonLaunchGateRejected,
356        "ATM_DAEMON_SERVING_STATE_REJECTED" => AtmErrorCode::DaemonServingStateRejected,
357        "ATM_DAEMON_STALE_OWNER_RECOVERY_FAILED" => AtmErrorCode::DaemonStaleOwnerRecoveryFailed,
358        "ATM_DAEMON_AUTO_START_FAILED" => AtmErrorCode::DaemonAutoStartFailed,
359        "ATM_DAEMON_CONNECTION_SATURATED" => AtmErrorCode::DaemonConnectionSaturated,
360        "REMOTE_DELIVERY_UNCONFIRMED" => AtmErrorCode::RemoteDeliveryUnconfirmed,
361        "ATM_PEER_WIRE_MODE_INVALID" => AtmErrorCode::PeerWireModeInvalid,
362        "ATM_PEER_WIRE_MODE_SOURCE_FORBIDDEN" => AtmErrorCode::PeerWireModeSourceForbidden,
363        "ATM_PEER_WIRE_PLAINTEXT_AUTHENTICATION_REQUIRED" => {
364            AtmErrorCode::PeerWirePlaintextAuthenticationRequired
365        }
366        "ATM_PEER_CONFIG_VALIDATION_FAILED" => AtmErrorCode::PeerConfigValidationFailed,
367        "ATM_CERTIFICATE_OPERATION_FAILED" => AtmErrorCode::CertificateOperationFailed,
368        "ATM_PEER_AUTHENTICATION_FAILED" => AtmErrorCode::PeerAuthenticationFailed,
369        "ATM_TRANSPORT_TIMEOUT" => AtmErrorCode::TransportTimeout,
370        "ATM_TRANSPORT_PROTOCOL_FAILED" => AtmErrorCode::TransportProtocolFailed,
371        "ATM_BIND_PREFLIGHT_FAILED" => AtmErrorCode::BindPreflightFailed,
372        "ATM_CLIENT_DAEMON_VERSION_INCOMPATIBLE" => AtmErrorCode::ClientDaemonVersionIncompatible,
373        "ATM_DAEMON_ADVISORY_SESSION_ALREADY_REGISTERED" => {
374            AtmErrorCode::DaemonAdvisorySessionAlreadyRegistered
375        }
376        "ATM_DAEMON_ADVISORY_SESSION_NOT_REGISTERED" => {
377            AtmErrorCode::DaemonAdvisorySessionNotRegistered
378        }
379        "ATM_DAEMON_ADVISORY_SESSION_CLEANUP_FAILED" => {
380            AtmErrorCode::DaemonAdvisorySessionCleanupFailed
381        }
382        "ATM_ADDRESS_PARSE_FAILED" => AtmErrorCode::AddressParseFailed,
383        "ATM_TEAM_UNAVAILABLE" => AtmErrorCode::TeamUnavailable,
384        "ATM_TEAM_INVALID" => AtmErrorCode::TeamInvalid,
385        "ATM_TEAM_NOT_FOUND" => AtmErrorCode::TeamNotFound,
386        "ATM_AGENT_NOT_FOUND" => AtmErrorCode::AgentNotFound,
387        _ => return None,
388    })
389}
390
391fn parse_mailbox_or_validation_code(value: &str) -> Option<AtmErrorCode> {
392    Some(match value {
393        "ATM_MAILBOX_READ_FAILED" => AtmErrorCode::MailboxReadFailed,
394        "ATM_MAILBOX_WRITE_FAILED" => AtmErrorCode::MailboxWriteFailed,
395        "ATM_MAILBOX_LOCK_FAILED" => AtmErrorCode::MailboxLockFailed,
396        "ATM_MAILBOX_LOCK_READ_ONLY_FILESYSTEM" => AtmErrorCode::MailboxLockReadOnlyFilesystem,
397        "ATM_MAILBOX_LOCK_TIMEOUT" => AtmErrorCode::MailboxLockTimeout,
398        "ATM_INTERNAL_ERROR" => AtmErrorCode::InternalError,
399        "ATM_MESSAGE_VALIDATION_FAILED" => AtmErrorCode::MessageValidationFailed,
400        "ATM_SEARCH_LOCAL_ONLY" => AtmErrorCode::SearchLocalOnly,
401        "ATM_LOCAL_HTTP_CAPABILITY_INVALID" => AtmErrorCode::LocalHttpCapabilityInvalid,
402        "ATM_LOCAL_HTTP_ENDPOINT_SCHEMA_UNSUPPORTED" => {
403            AtmErrorCode::LocalHttpEndpointSchemaUnsupported
404        }
405        "ATM_LOCAL_HTTP_ENDPOINT_MISSING" => AtmErrorCode::LocalHttpEndpointMissing,
406        "ATM_LOCAL_HTTP_ENDPOINT_NON_LOOPBACK" => AtmErrorCode::LocalHttpEndpointNonLoopback,
407        "ATM_LOCAL_HTTP_RUNTIME_DIRECTORY_MISSING" => {
408            AtmErrorCode::LocalHttpRuntimeDirectoryMissing
409        }
410        "ATM_LOCAL_HTTP_CAPABILITY_REVOKED" => AtmErrorCode::LocalHttpCapabilityRevoked,
411        "ATM_MESSAGE_ID_CONFLICT" => AtmErrorCode::MessageIdConflict,
412        "ATM_SELF_ADDRESSED_SEND_INVALID" => AtmErrorCode::SelfAddressedSendInvalid,
413        "ATM_NUDGE_TEMPLATE_BODY_EMPTY" => AtmErrorCode::EmptyNudgeTemplateBody,
414        "ATM_CALLER_CONTEXT_REQUEST_INVALID" => AtmErrorCode::CallerContextRequestInvalid,
415        "TEMPLATE_CONTENT_NOT_UTF8" => AtmErrorCode::TemplateContentNotUtf8,
416        "DECOMPOSED_TEMPLATE_INCLUDE_FORBIDDEN" => AtmErrorCode::DecomposedTemplateIncludeForbidden,
417        "TEMPLATE_LOAD_FAILED" => AtmErrorCode::TemplateLoadFailed,
418        "TEMPLATE_HASH_API_FAILED" => AtmErrorCode::TemplateHashApiFailed,
419        "TEMPLATE_INSPECTION_PARSE_FAILED" => AtmErrorCode::TemplateInspectionParseFailed,
420        "TEMPLATE_REQUIRED_VARIABLE_MISSING" => AtmErrorCode::TemplateRequiredVariableMissing,
421        "TEMPLATE_RENDER_VERIFICATION_FAILED" => AtmErrorCode::TemplateRenderVerificationFailed,
422        "TEMPLATE_INCLUDE_UNRESOLVED" => AtmErrorCode::TemplateIncludeUnresolved,
423        "TEMPLATE_CLASSIFICATION_INVALID" => AtmErrorCode::TemplateClassificationInvalid,
424        "TEMPLATE_WORKFLOW_INVALID" => AtmErrorCode::TemplateWorkflowInvalid,
425        "TEMPLATE_WORKFLOW_VALUE_INVALID" => AtmErrorCode::TemplateWorkflowValueInvalid,
426        "TEMPLATE_TAG_RESERVED" => AtmErrorCode::TemplateTagReserved,
427        "ATM_WORKFLOW_QUERY_INVALID" => AtmErrorCode::WorkflowQueryInvalid,
428        "ATM_WORKFLOW_TELEMETRY_CONFIG_INVALID" => AtmErrorCode::WorkflowTelemetryConfigInvalid,
429        "ATM_WORKFLOW_TELEMETRY_DROPPED" => AtmErrorCode::WorkflowTelemetryDropped,
430        "ATM_SERIALIZATION_FAILED" => AtmErrorCode::SerializationFailed,
431        "ATM_FILE_POLICY_REJECTED" => AtmErrorCode::FilePolicyRejected,
432        "ATM_FILE_REFERENCE_REWRITE_FAILED" => AtmErrorCode::FileReferenceRewriteFailed,
433        "ATM_WAIT_TIMEOUT" => AtmErrorCode::WaitTimeout,
434        "ATM_ACK_INVALID_STATE" => AtmErrorCode::AckInvalidState,
435        "ATM_CLEAR_INVALID_STATE" => AtmErrorCode::ClearInvalidState,
436        _ => return None,
437    })
438}
439
440fn parse_observability_or_warning_code(value: &str) -> Option<AtmErrorCode> {
441    Some(match value {
442        "ATM_OBSERVABILITY_EMIT_FAILED" => AtmErrorCode::ObservabilityEmitFailed,
443        "ATM_OBSERVABILITY_QUERY_FAILED" => AtmErrorCode::ObservabilityQueryFailed,
444        "ATM_OBSERVABILITY_FOLLOW_FAILED" => AtmErrorCode::ObservabilityFollowFailed,
445        "ATM_OBSERVABILITY_HEALTH_FAILED" => AtmErrorCode::ObservabilityHealthFailed,
446        "ATM_OBSERVABILITY_BOOTSTRAP_FAILED" => AtmErrorCode::ObservabilityBootstrapFailed,
447        "ATM_OBSERVABILITY_HEALTH_OK" => AtmErrorCode::ObservabilityHealthOk,
448        "ATM_WARNING_INVALID_TEAM_MEMBER_SKIPPED" => AtmErrorCode::WarningInvalidTeamMemberSkipped,
449        "ATM_WARNING_MAILBOX_RECORD_SKIPPED" => AtmErrorCode::WarningMailboxRecordSkipped,
450        "ATM_WARNING_MALFORMED_ATM_FIELD_IGNORED" => AtmErrorCode::WarningMalformedAtmFieldIgnored,
451        "ATM_WARNING_OBSERVABILITY_HEALTH_DEGRADED" => {
452            AtmErrorCode::WarningObservabilityHealthDegraded
453        }
454        "ATM_WARNING_SQLITE_HEALTH_DEGRADED" => AtmErrorCode::WarningSqliteHealthDegraded,
455        "ATM_WARNING_ORIGIN_INBOX_ENTRY_SKIPPED" => AtmErrorCode::WarningOriginInboxEntrySkipped,
456        "ATM_WARNING_MISSING_TEAM_CONFIG_FALLBACK" => {
457            AtmErrorCode::WarningMissingTeamConfigFallback
458        }
459        "ATM_WARNING_SEND_ALERT_STATE_DEGRADED" => AtmErrorCode::WarningSendAlertStateDegraded,
460        "ATM_WARNING_IDENTITY_DRIFT" => AtmErrorCode::WarningIdentityDrift,
461        "ATM_WARNING_ROSTER_DRIFT" => AtmErrorCode::WarningRosterDrift,
462        "ATM_WARNING_BASELINE_MEMBER_MISSING" => AtmErrorCode::WarningBaselineMemberMissing,
463        "ATM_WARNING_RESTORE_IN_PROGRESS" => AtmErrorCode::WarningRestoreInProgress,
464        "ATM_WARNING_STALE_MAILBOX_LOCK" => AtmErrorCode::WarningStaleMailboxLock,
465        "ATM_WARNING_HOOK_SKIPPED" => AtmErrorCode::WarningHookSkipped,
466        "ATM_WARNING_HOOK_EXECUTION_FAILED" => AtmErrorCode::WarningHookExecutionFailed,
467        _ => return None,
468    })
469}
470
471fn parse_post_send_or_misc_code(value: &str) -> Option<AtmErrorCode> {
472    Some(match value {
473        "ATM_POST_SEND_PANE_MISSING" => AtmErrorCode::PostSendPaneMissing,
474        "ATM_POST_SEND_TMUX_SEND_FAILED" => AtmErrorCode::PostSendTmuxSendFailed,
475        "ATM_POST_SEND_GRAFT_UNAVAILABLE" => AtmErrorCode::PostSendGraftUnavailable,
476        "ATM_GRAFT_RECEIVER_ALREADY_ACTIVE" => AtmErrorCode::GraftReceiverAlreadyActive,
477        "ATM_POST_SEND_ADVISORY_DELIVERY_FAILED" => AtmErrorCode::PostSendAdvisoryDeliveryFailed,
478        "ATM_TEST_FAKE_TRANSPORT_INJECTION_FAILED" => {
479            AtmErrorCode::TestFakeTransportInjectionFailed
480        }
481        "ATM_HELP_TOPIC_NOT_FOUND" => AtmErrorCode::HelpTopicNotFound,
482        _ => return None,
483    })
484}
485
486impl fmt::Display for AtmErrorCode {
487    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
488        f.write_str(self.as_str())
489    }
490}
491
492impl Serialize for AtmErrorCode {
493    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
494    where
495        S: serde::Serializer,
496    {
497        serializer.serialize_str(self.as_str())
498    }
499}
500
501impl<'de> Deserialize<'de> for AtmErrorCode {
502    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
503    where
504        D: Deserializer<'de>,
505    {
506        let value = String::deserialize(deserializer)?;
507        value.parse().map_err(serde::de::Error::custom)
508    }
509}