dstar_gateway_core/error/
state.rs1use crate::session::client::ClientStateKind;
4use crate::types::{ProtocolKind, StreamId};
5
6#[derive(Debug, Clone, thiserror::Error)]
8#[non_exhaustive]
9pub enum StateError {
10 #[error("stream {already_active} is still streaming; cannot start {requested}")]
12 StreamAlreadyActive {
13 already_active: StreamId,
15 requested: StreamId,
17 },
18
19 #[error("voice sequence {got} out of range; D-STAR seq must be 0..21")]
21 VoiceSeqOutOfRange {
22 got: u8,
24 },
25
26 #[error("received frame for unknown stream id {stream_id}")]
28 UnknownStreamId {
29 stream_id: StreamId,
31 },
32
33 #[error("{operation} is not valid in {state:?} for protocol {protocol:?}")]
45 WrongState {
46 operation: &'static str,
48 state: ClientStateKind,
50 protocol: ProtocolKind,
52 },
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 const SID_1111: StreamId = StreamId::new(0x1111).unwrap();
60 const SID_2222: StreamId = StreamId::new(0x2222).unwrap();
61
62 #[test]
63 fn state_error_stream_already_active_display() {
64 let err = StateError::StreamAlreadyActive {
65 already_active: SID_1111,
66 requested: SID_2222,
67 };
68 let s = err.to_string();
69 assert!(s.contains("0x1111"));
70 assert!(s.contains("0x2222"));
71 }
72
73 #[test]
74 fn state_error_voice_seq_out_of_range_display() {
75 let err = StateError::VoiceSeqOutOfRange { got: 25 };
76 assert!(err.to_string().contains("25"));
77 }
78
79 #[test]
80 fn state_error_wrong_state_display() {
81 let err = StateError::WrongState {
82 operation: "enqueue_connect",
83 state: ClientStateKind::Connected,
84 protocol: ProtocolKind::DPlus,
85 };
86 let s = err.to_string();
87 assert!(s.contains("enqueue_connect"));
88 assert!(s.contains("Connected"));
89 assert!(s.contains("DPlus"));
90 }
91}