ironrdp_session/
lib.rs

1#![cfg_attr(doc, doc = include_str!("../README.md"))]
2#![doc(html_logo_url = "https://cdnweb.devolutions.net/images/projects/devolutions/logos/devolutions-icon-shadow.svg")]
3#![allow(clippy::arithmetic_side_effects)] // FIXME: remove
4
5mod macros;
6
7pub mod fast_path;
8pub mod image;
9pub mod legacy;
10pub mod pointer;
11pub mod rfx; // FIXME: maybe this module should not be in this crate
12pub mod x224;
13
14mod active_stage;
15
16use core::fmt;
17
18pub use active_stage::{ActiveStage, ActiveStageOutput, GracefulDisconnectReason};
19
20pub type SessionResult<T> = Result<T, SessionError>;
21
22#[non_exhaustive]
23#[derive(Debug)]
24pub enum SessionErrorKind {
25    Pdu(ironrdp_pdu::PduError),
26    Encode(ironrdp_core::EncodeError),
27    Decode(ironrdp_core::DecodeError),
28    Reason(String),
29    General,
30    Custom,
31}
32
33impl fmt::Display for SessionErrorKind {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match &self {
36            SessionErrorKind::Pdu(_) => write!(f, "PDU error"),
37            SessionErrorKind::Encode(_) => write!(f, "encode error"),
38            SessionErrorKind::Decode(_) => write!(f, "decode error"),
39            SessionErrorKind::Reason(description) => write!(f, "reason: {description}"),
40            SessionErrorKind::General => write!(f, "general error"),
41            SessionErrorKind::Custom => write!(f, "custom error"),
42        }
43    }
44}
45
46impl core::error::Error for SessionErrorKind {
47    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
48        match &self {
49            SessionErrorKind::Pdu(e) => Some(e),
50            SessionErrorKind::Encode(e) => Some(e),
51            SessionErrorKind::Decode(e) => Some(e),
52            SessionErrorKind::Reason(_) => None,
53            SessionErrorKind::General => None,
54            SessionErrorKind::Custom => None,
55        }
56    }
57}
58
59pub type SessionError = ironrdp_error::Error<SessionErrorKind>;
60
61pub trait SessionErrorExt {
62    fn pdu(error: ironrdp_pdu::PduError) -> Self;
63    fn encode(error: ironrdp_core::EncodeError) -> Self;
64    fn decode(error: ironrdp_core::DecodeError) -> Self;
65    fn general(context: &'static str) -> Self;
66    fn reason(context: &'static str, reason: impl Into<String>) -> Self;
67    fn custom<E>(context: &'static str, e: E) -> Self
68    where
69        E: core::error::Error + Sync + Send + 'static;
70}
71
72impl SessionErrorExt for SessionError {
73    fn pdu(error: ironrdp_pdu::PduError) -> Self {
74        Self::new("payload error", SessionErrorKind::Pdu(error))
75    }
76
77    fn encode(error: ironrdp_core::EncodeError) -> Self {
78        Self::new("encode error", SessionErrorKind::Encode(error))
79    }
80
81    fn decode(error: ironrdp_core::DecodeError) -> Self {
82        Self::new("decode error", SessionErrorKind::Decode(error))
83    }
84
85    fn general(context: &'static str) -> Self {
86        Self::new(context, SessionErrorKind::General)
87    }
88
89    fn reason(context: &'static str, reason: impl Into<String>) -> Self {
90        Self::new(context, SessionErrorKind::Reason(reason.into()))
91    }
92
93    fn custom<E>(context: &'static str, e: E) -> Self
94    where
95        E: core::error::Error + Sync + Send + 'static,
96    {
97        Self::new(context, SessionErrorKind::Custom).with_source(e)
98    }
99}
100
101pub trait SessionResultExt {
102    #[must_use]
103    fn with_context(self, context: &'static str) -> Self;
104    #[must_use]
105    fn with_source<E>(self, source: E) -> Self
106    where
107        E: core::error::Error + Sync + Send + 'static;
108}
109
110impl<T> SessionResultExt for SessionResult<T> {
111    fn with_context(self, context: &'static str) -> Self {
112        self.map_err(|mut e| {
113            e.context = context;
114            e
115        })
116    }
117
118    fn with_source<E>(self, source: E) -> Self
119    where
120        E: core::error::Error + Sync + Send + 'static,
121    {
122        self.map_err(|e| e.with_source(source))
123    }
124}