muse_code_sdk/
host_death.rs1use crate::msp::{InitializeResult, SessionDurability};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum SessionDurabilityProfile {
9 Durable,
11 Ephemeral,
13 Unrecognized(String),
15}
16
17#[must_use]
19pub fn read_session_durability(result: &InitializeResult) -> SessionDurabilityProfile {
20 durability_profile(result.session_durability.as_ref())
21}
22
23pub(crate) fn durability_profile(declared: Option<&SessionDurability>) -> SessionDurabilityProfile {
24 match declared {
25 None | Some(SessionDurability::Durable) => SessionDurabilityProfile::Durable,
26 Some(SessionDurability::Ephemeral) => SessionDurabilityProfile::Ephemeral,
27 Some(SessionDurability::Unknown(value)) => {
28 SessionDurabilityProfile::Unrecognized(value.clone())
29 }
30 }
31}
32
33#[must_use]
35pub const fn survives_host_death(profile: &SessionDurabilityProfile) -> bool {
36 matches!(profile, SessionDurabilityProfile::Durable)
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
41#[non_exhaustive]
42pub enum HostDeathNotification {
43 CleanShutdown,
45 TransportEof,
50 AbnormalExit,
52}
53
54#[must_use]
56pub fn is_abnormal_host_death(notification: &HostDeathNotification) -> bool {
57 !matches!(notification, HostDeathNotification::CleanShutdown)
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62#[non_exhaustive]
63pub enum HostCloseAttribution {
64 Ours,
66 CleanHostExit,
68 DurableHostDeath,
70 DiscardedHostDeath,
72}
73
74impl HostCloseAttribution {
75 #[must_use]
78 pub const fn may_reattach(self) -> bool {
79 matches!(self, Self::DurableHostDeath)
80 }
81
82 #[must_use]
84 pub const fn is_host_death(self) -> bool {
85 matches!(self, Self::DurableHostDeath | Self::DiscardedHostDeath)
86 }
87}
88
89#[must_use]
91pub fn attribute_transport_eof(
92 profile: &SessionDurabilityProfile,
93 client_closing: bool,
94) -> HostCloseAttribution {
95 if client_closing {
96 HostCloseAttribution::Ours
97 } else {
98 death_for(profile)
99 }
100}
101
102#[must_use]
108pub fn attribute_process_exit(
109 profile: &SessionDurabilityProfile,
110 clean_shutdown: bool,
111) -> HostCloseAttribution {
112 if clean_shutdown {
113 HostCloseAttribution::CleanHostExit
114 } else {
115 death_for(profile)
116 }
117}
118
119const fn death_for(profile: &SessionDurabilityProfile) -> HostCloseAttribution {
120 if survives_host_death(profile) {
121 HostCloseAttribution::DurableHostDeath
122 } else {
123 HostCloseAttribution::DiscardedHostDeath
124 }
125}