#[cfg(feature = "alloc")]
use alloc::string::String;
use ironrdp_error::{Error, Source};
pub trait WithSource {
#[must_use]
fn with_source<E: Source>(self, source: E) -> Self;
}
impl<T> WithSource for Error<T> {
fn with_source<E: Source>(self, source: E) -> Self {
self.with_source(source)
}
}
pub trait NotEnoughBytesErr {
fn not_enough_bytes(context: &'static str, received: usize, expected: usize) -> Self;
}
pub fn not_enough_bytes_err<T: NotEnoughBytesErr>(context: &'static str, received: usize, expected: usize) -> T {
T::not_enough_bytes(context, received, expected)
}
pub trait InvalidFieldErr {
fn invalid_field(context: &'static str, field: &'static str, reason: &'static str) -> Self;
}
pub fn invalid_field_err_with_source<T: InvalidFieldErr + WithSource, E: Source>(
context: &'static str,
field: &'static str,
reason: &'static str,
source: E,
) -> T {
T::invalid_field(context, field, reason).with_source(source)
}
pub fn invalid_field_err<T: InvalidFieldErr>(context: &'static str, field: &'static str, reason: &'static str) -> T {
T::invalid_field(context, field, reason)
}
pub trait UnexpectedMessageTypeErr {
fn unexpected_message_type(context: &'static str, got: u8) -> Self;
}
pub fn unexpected_message_type_err<T: UnexpectedMessageTypeErr>(context: &'static str, got: u8) -> T {
T::unexpected_message_type(context, got)
}
pub trait UnsupportedVersionErr {
fn unsupported_version(context: &'static str, got: u8) -> Self;
}
pub fn unsupported_version_err<T: UnsupportedVersionErr>(context: &'static str, got: u8) -> T {
T::unsupported_version(context, got)
}
pub trait UnsupportedValueErr {
#[cfg(feature = "alloc")]
fn unsupported_value(context: &'static str, name: &'static str, value: String) -> Self;
#[cfg(not(feature = "alloc"))]
fn unsupported_value(context: &'static str, name: &'static str) -> Self;
}
#[cfg(feature = "alloc")]
pub fn unsupported_value_err<T: UnsupportedValueErr>(context: &'static str, name: &'static str, value: String) -> T {
T::unsupported_value(context, name, value)
}
#[cfg(not(feature = "alloc"))]
pub fn unsupported_value_err<T: UnsupportedValueErr>(context: &'static str, name: &'static str) -> T {
T::unsupported_value(context, name)
}
pub trait OtherErr {
fn other(context: &'static str, description: &'static str) -> Self;
}
pub fn other_err<T: OtherErr>(context: &'static str, description: &'static str) -> T {
T::other(context, description)
}
pub fn other_err_with_source<T: OtherErr + WithSource, E: Source>(
context: &'static str,
description: &'static str,
source: E,
) -> T {
T::other(context, description).with_source(source)
}