Skip to main content

zerodds_idl_rust/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3//! Error family of the Rust codegen.
4
5use core::fmt;
6
7/// Error while generating Rust code from the IDL AST.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum RustGenError {
10    /// The IDL construct is currently not supported by the Rust codegen.
11    ///
12    /// Not a spec bug — some IDL features (Fixed, Map, Any, Bitset,
13    /// Bitmask, Valuetype, Interface, Component, Home) are outside
14    /// the DDS-DataType path. Whoever generates DDS DataTypes does not
15    /// need them.
16    Unsupported {
17        /// Construct name (e.g. `"fixed"`).
18        what: &'static str,
19        /// Position in the IDL source (byte offset from the AST span).
20        at: usize,
21    },
22    /// The annotation value could not be evaluated.
23    InvalidAnnotation {
24        /// Annotation name (e.g. `"@id"`).
25        name: String,
26        /// Reason.
27        reason: &'static str,
28    },
29    /// The type reference could not be resolved.
30    UnresolvedType {
31        /// Fully qualified name.
32        scoped: String,
33    },
34}
35
36impl fmt::Display for RustGenError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::Unsupported { what, at } => {
40                write!(
41                    f,
42                    "rust-codegen: unsupported IDL construct `{what}` at offset {at}"
43                )
44            }
45            Self::InvalidAnnotation { name, reason } => {
46                write!(f, "rust-codegen: invalid annotation `{name}`: {reason}")
47            }
48            Self::UnresolvedType { scoped } => {
49                write!(f, "rust-codegen: unresolved type `{scoped}`")
50            }
51        }
52    }
53}
54
55impl std::error::Error for RustGenError {}
56
57/// Result-Alias.
58pub type Result<T> = core::result::Result<T, RustGenError>;