ironrdp_core/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
/// Asserts that the traits support dynamic dispatch.
///
/// From <https://docs.rs/static_assertions/1.1.0/src/static_assertions/assert_obj_safe.rs.html#72-76>
#[macro_export]
macro_rules! assert_obj_safe {
($($xs:path),+ $(,)?) => {
$(const _: Option<&dyn $xs> = None;)+
};
}
/// Asserts that the type implements _all_ of the given traits.
///
/// From <https://docs.rs/static_assertions/1.1.0/src/static_assertions/assert_impl.rs.html#113-121>
#[macro_export]
macro_rules! assert_impl {
($type:ty: $($trait:path),+ $(,)?) => {
const _: fn() = || {
// Only callable when `$type` implements all traits in `$($trait)+`.
fn assert_impl_all<T: ?Sized $(+ $trait)+>() {}
assert_impl_all::<$type>();
};
};
}
/// Finds the name of the function in which this macro is expanded
#[macro_export]
macro_rules! function {
// Taken from https://stackoverflow.com/a/40234666
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
core::any::type_name::<T>()
}
let name = type_name_of(f);
name.strip_suffix("::f").unwrap()
}};
}
/// Creates a "not enough bytes" error with context information.
///
/// This macro generates an error indicating that there weren't enough bytes
/// in a buffer for a particular operation.
///
/// # Arguments
///
/// * `context` - The context in which the error occurred (optional)
/// * `received` - The number of bytes actually received
/// * `expected` - The number of bytes expected
///
/// # Examples
///
/// ```
/// use ironrdp_core::not_enough_bytes_err;
///
/// let err = not_enough_bytes_err!("parsing header", 5, 10);
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! not_enough_bytes_err {
( $context:expr, $received:expr , $expected:expr $(,)? ) => {{
$crate::not_enough_bytes_err($context, $received, $expected)
}};
( $received:expr , $expected:expr $(,)? ) => {{
$crate::not_enough_bytes_err!($crate::function!(), $received, $expected)
}};
}
/// Creates an "invalid field" error with context information.
///
/// This macro generates an error indicating that a field in a data structure
/// or input is invalid for some reason.
///
/// # Arguments
///
/// * `context` - The context in which the error occurred (optional)
/// * `field` - The name of the invalid field
/// * `reason` - The reason why the field is invalid
///
/// # Examples
///
/// ```
/// use ironrdp_core::invalid_field_err;
///
/// let err = invalid_field_err!("user input", "Age", "must be positive");
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! invalid_field_err {
( $context:expr, $field:expr , $reason:expr $(,)? ) => {{
$crate::invalid_field_err($context, $field, $reason)
}};
( $field:expr , $reason:expr $(,)? ) => {{
$crate::invalid_field_err!($crate::function!(), $field, $reason)
}};
}
/// Creates an "unexpected message type" error with context information.
///
/// This macro generates an error indicating that an unexpected message type
/// was received in a particular context.
///
/// # Arguments
///
/// * `context` - The context in which the error occurred (optional)
/// * `got` - The unexpected message type that was received
///
/// # Examples
///
/// ```
/// use ironrdp_core::unexpected_message_type_err;
///
/// let err = unexpected_message_type_err!("Erase");
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! unexpected_message_type_err {
( $context:expr, $got:expr $(,)? ) => {{
$crate::unexpected_message_type_err($context, $got)
}};
( $got:expr $(,)? ) => {{
$crate::unexpected_message_type_err!($crate::function!(), $got)
}};
}
/// Creates an "unsupported version" error with context information.
///
/// This macro generates an error indicating that an unsupported version
/// was encountered in a particular context.
///
/// # Arguments
///
/// * `context` - The context in which the error occurred (optional)
/// * `got` - The unsupported version that was encountered
///
/// # Examples
///
/// ```
/// use ironrdp_core::unsupported_version_err;
///
/// let err = unsupported_version_err!("protocol version", 12);
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! unsupported_version_err {
( $context:expr, $got:expr $(,)? ) => {{
$crate::unsupported_version_err($context, $got)
}};
( $got:expr $(,)? ) => {{
$crate::unsupported_version_err!($crate::function!(), $got)
}};
}
/// Creates an "unsupported value" error with context information.
///
/// This macro generates an error indicating that an unsupported value
/// was encountered for a specific named parameter or field.
///
/// # Arguments
///
/// * `context` - The context in which the error occurred (optional)
/// * `name` - The name of the parameter or field with the unsupported value
/// * `value` - The unsupported value that was encountered
///
/// # Examples
///
/// ```
/// use ironrdp_core::unsupported_value_err;
///
/// let err = unsupported_value_err!("configuration", "log_level", "EXTREME");
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! unsupported_value_err {
( $context:expr, $name:expr, $value:expr $(,)? ) => {{
$crate::unsupported_value_err($context, $name, $value)
}};
( $name:expr, $value:expr $(,)? ) => {{
$crate::unsupported_value_err!($crate::function!(), $name, $value)
}};
}
/// Creates a generic "other" error with optional context and source information.
///
/// This macro generates a generic error that can include a description, context,
/// and an optional source error. It's useful for creating custom errors or
/// wrapping other errors with additional context.
///
/// # Arguments
///
/// * `description` - A description of the error (optional)
/// * `context` - The context in which the error occurred (optional)
/// * `source` - The source error, if this error is wrapping another (optional)
///
/// # Examples
///
/// ```
/// use ironrdp_core::other_err;
///
/// // With description and source
/// let source_err = std::io::Error::new(std::io::ErrorKind::Other, "Source error");
/// let err = other_err!("Something went wrong", source: source_err);
///
/// // With context and description
/// let err = other_err!("parsing input", "Unexpected end of file");
///
/// // With only description
/// let err = other_err!("Operation failed");
///
/// // With only source
/// let err = other_err!(source: std::io::Error::new(std::io::ErrorKind::Other, "IO error"));
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! other_err {
( $context:expr, source: $source:expr $(,)? ) => {{
$crate::other_err_with_source($context, "", $source)
}};
( $context:expr, $description:expr $(,)? ) => {{
$crate::other_err($context, $description)
}};
( source: $source:expr $(,)? ) => {{
$crate::other_err!($crate::function!(), source: $source)
}};
( $description:expr $(,)? ) => {{
$crate::other_err!($crate::function!(), $description)
}};
}
/// Ensures that a buffer has at least the expected size.
///
/// This macro checks if the buffer length is greater than or equal to the expected size.
/// If not, it returns a "not enough bytes" error.
///
/// # Arguments
///
/// * `ctx` - The context for the error message (optional)
/// * `buf` - The buffer to check
/// * `expected` - The expected minimum size of the buffer
///
/// # Examples
///
/// ```
/// use ironrdp_core::ensure_size;
///
/// fn parse_data(buf: &[u8]) -> Result<(), Error> {
/// ensure_size!(in: buf, size: 10);
/// // ... rest of the parsing logic
/// Ok(())
/// }
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! ensure_size {
(ctx: $ctx:expr, in: $buf:ident, size: $expected:expr) => {{
let received = $buf.len();
let expected = $expected;
if !(received >= expected) {
return Err($crate::not_enough_bytes_err($ctx, received, expected));
}
}};
(in: $buf:ident, size: $expected:expr) => {{
$crate::ensure_size!(ctx: $crate::function!(), in: $buf, size: $expected)
}};
}
/// Ensures that a buffer has at least the fixed part size of a struct.
///
/// This macro is a specialized version of `ensure_size` that uses the
/// `FIXED_PART_SIZE` constant of the current struct.
///
/// # Examples
///
/// ```
/// use ironrdp_core::ensure_fixed_part_size;
///
/// struct MyStruct {
/// // ... fields
/// }
///
/// impl MyStruct {
/// const FIXED_PART_SIZE: usize = 20;
///
/// fn parse(buf: &[u8]) -> Result<Self, Error> {
/// ensure_fixed_part_size!(in: buf);
/// // ... parsing logic
/// }
/// }
/// ```
///
/// # Note
///
/// This macro assumes that the current struct has a `FIXED_PART_SIZE` constant defined.
#[macro_export]
macro_rules! ensure_fixed_part_size {
(in: $buf:ident) => {{
$crate::ensure_size!(ctx: $crate::function!(), in: $buf, size: Self::FIXED_PART_SIZE)
}};
}
/// Safely casts a length to a different integer type.
///
/// This macro attempts to convert a length value to a different integer type,
/// returning an error if the conversion fails due to overflow.
///
/// # Arguments
///
/// * `ctx` - The context for the error message (optional)
/// * `field` - The name of the field being cast
/// * `len` - The length value to cast
///
/// # Examples
///
/// ```
/// use ironrdp_core::cast_length;
///
/// fn process_data(data: &[u8]) -> Result<(), Error> {
/// let len: u16 = cast_length!("data length", data.len())?;
/// // ... rest of the processing logic
/// Ok(())
/// }
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! cast_length {
($ctx:expr, $field:expr, $len:expr) => {{
$len.try_into()
.map_err(|e| $crate::invalid_field_err_with_source($ctx, $field, "too many elements", e))
}};
($field:expr, $len:expr) => {{
$crate::cast_length!($crate::function!(), $field, $len)
}};
}
/// Safely casts an integer to a different integer type.
///
/// This macro attempts to convert an integer value to a different integer type,
/// returning an error if the conversion fails due to out-of-range issues.
///
/// # Arguments
///
/// * `ctx` - The context for the error message (optional)
/// * `field` - The name of the field being cast
/// * `len` - The integer value to cast
///
/// # Examples
///
/// ```
/// use ironrdp_core::cast_int;
///
/// fn process_value(value: u64) -> Result<i32, Error> {
/// let casted_value: i32 = cast_int!("input value", value)?;
/// Ok(casted_value)
/// }
/// ```
///
/// # Note
///
/// If the context is not provided, it will use the current function name.
#[macro_export]
macro_rules! cast_int {
($ctx:expr, $field:expr, $len:expr) => {{
$len.try_into().map_err(|e| {
$crate::invalid_field_err_with_source($ctx, $field, "out of range integral type conversion", e)
})
}};
($field:expr, $len:expr) => {{
$crate::cast_int!($crate::function!(), $field, $len)
}};
}