Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/ironrdp-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ test = false

[features]
default = []
std = ["alloc", "ironrdp-error/std"]
std = ["alloc", "ironrdp-error/std", "thiserror/std"]
alloc = ["ironrdp-error/alloc"]

[dependencies]
ironrdp-error = { path = "../ironrdp-error", version = "0.1" } # public
thiserror = { version = "2", default-features = false }
44 changes: 8 additions & 36 deletions crates/ironrdp-core/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(feature = "alloc")]
use alloc::string::String;
use core::fmt;

use crate::{
InvalidFieldErr, NotEnoughBytesErr, OtherErr, ReadCursor, UnexpectedMessageTypeErr, UnsupportedValueErr,
Expand All @@ -16,34 +15,39 @@ pub type DecodeError = ironrdp_error::Error<DecodeErrorKind>;

/// Enum representing different kinds of decode errors.
#[non_exhaustive]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, thiserror::Error)]
pub enum DecodeErrorKind {
/// Error when there are not enough bytes to decode.
#[error("not enough bytes provided to decode: received {received} bytes, expected {expected} bytes")]
NotEnoughBytes {
/// Number of bytes received.
received: usize,
/// Number of bytes expected.
expected: usize,
},
/// Error when a field is invalid.
#[error("invalid `{field}`: {reason}")]
InvalidField {
/// Name of the invalid field.
field: &'static str,
/// Reason for invalidity.
reason: &'static str,
},
/// Error when an unexpected message type is encountered.
#[error("invalid message type ({got})")]
UnexpectedMessageType {
/// The unexpected message type received.
got: u8,
},
/// Error when an unsupported version is encountered.
#[error("unsupported version ({got})")]
UnsupportedVersion {
/// The unsupported version received.
got: u8,
},
/// Error when an unsupported value is encountered (with allocation feature).
#[cfg(feature = "alloc")]
#[error("unsupported {name} ({value})")]
UnsupportedValue {
/// Name of the unsupported value.
name: &'static str,
Expand All @@ -52,51 +56,19 @@ pub enum DecodeErrorKind {
},
/// Error when an unsupported value is encountered (without allocation feature).
#[cfg(not(feature = "alloc"))]
#[error("unsupported {name}")]
UnsupportedValue {
/// Name of the unsupported value.
name: &'static str,
},
/// Generic error for other cases.
#[error("other ({description})")]
Other {
/// Description of the error.
description: &'static str,
},
}

#[cfg(feature = "std")]
impl core::error::Error for DecodeErrorKind {}

impl fmt::Display for DecodeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotEnoughBytes { received, expected } => write!(
f,
"not enough bytes provided to decode: received {received} bytes, expected {expected} bytes"
),
Self::InvalidField { field, reason } => {
write!(f, "invalid `{field}`: {reason}")
}
Self::UnexpectedMessageType { got } => {
write!(f, "invalid message type ({got})")
}
Self::UnsupportedVersion { got } => {
write!(f, "unsupported version ({got})")
}
#[cfg(feature = "alloc")]
Self::UnsupportedValue { name, value } => {
write!(f, "unsupported {name} ({value})")
}
#[cfg(not(feature = "alloc"))]
Self::UnsupportedValue { name } => {
write!(f, "unsupported {name}")
}
Self::Other { description } => {
write!(f, "other ({description})")
}
}
}
}

impl NotEnoughBytesErr for DecodeError {
fn not_enough_bytes(context: &'static str, received: usize, expected: usize) -> Self {
Self::new(context, DecodeErrorKind::NotEnoughBytes { received, expected })
Expand Down
44 changes: 8 additions & 36 deletions crates/ironrdp-core/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::{vec, vec::Vec};
use core::fmt;

#[cfg(feature = "alloc")]
use crate::WriteBuf;
Expand All @@ -20,34 +19,39 @@ pub type EncodeError = ironrdp_error::Error<EncodeErrorKind>;

/// Represents the different kinds of errors that can occur during encoding operations.
#[non_exhaustive]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, thiserror::Error)]
pub enum EncodeErrorKind {
/// Indicates that there were not enough bytes to complete the encoding operation.
#[error("not enough bytes provided to decode: received {received} bytes, expected {expected} bytes")]
NotEnoughBytes {
/// The number of bytes actually received.
received: usize,
/// The number of bytes expected or required.
expected: usize,
},
/// Indicates that a field in the data being encoded is invalid.
#[error("invalid `{field}`: {reason}")]
InvalidField {
/// The name of the invalid field.
field: &'static str,
/// The reason why the field is considered invalid.
reason: &'static str,
},
/// Indicates that an unexpected message type was encountered during encoding.
#[error("invalid message type ({got})")]
UnexpectedMessageType {
/// The unexpected message type that was received.
got: u8,
},
/// Indicates that an unsupported version was encountered during encoding.
#[error("unsupported version ({got})")]
UnsupportedVersion {
/// The unsupported version that was received.
got: u8,
},
/// Indicates that an unsupported value was encountered during encoding.
#[cfg(feature = "alloc")]
#[error("unsupported {name} ({value})")]
UnsupportedValue {
/// The name of the field or parameter with the unsupported value.
name: &'static str,
Expand All @@ -56,51 +60,19 @@ pub enum EncodeErrorKind {
},
/// Indicates that an unsupported value was encountered during encoding (no-alloc version).
#[cfg(not(feature = "alloc"))]
#[error("unsupported {name}")]
UnsupportedValue {
/// The name of the field or parameter with the unsupported value.
name: &'static str,
},
/// Represents any other error that doesn't fit into the above categories.
#[error("other ({description})")]
Other {
/// A description of the error.
description: &'static str,
},
}

#[cfg(feature = "std")]
impl core::error::Error for EncodeErrorKind {}

impl fmt::Display for EncodeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotEnoughBytes { received, expected } => write!(
f,
"not enough bytes provided to decode: received {received} bytes, expected {expected} bytes"
),
Self::InvalidField { field, reason } => {
write!(f, "invalid `{field}`: {reason}")
}
Self::UnexpectedMessageType { got } => {
write!(f, "invalid message type ({got})")
}
Self::UnsupportedVersion { got } => {
write!(f, "unsupported version ({got})")
}
#[cfg(feature = "alloc")]
Self::UnsupportedValue { name, value } => {
write!(f, "unsupported {name} ({value})")
}
#[cfg(not(feature = "alloc"))]
Self::UnsupportedValue { name } => {
write!(f, "unsupported {name}")
}
Self::Other { description } => {
write!(f, "other ({description})")
}
}
}
}

impl NotEnoughBytesErr for EncodeError {
fn not_enough_bytes(context: &'static str, received: usize, expected: usize) -> Self {
Self::new(context, EncodeErrorKind::NotEnoughBytes { received, expected })
Expand Down
1 change: 1 addition & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading