Skip to main content

geometry_dag/
error.rs

1//! 错误类型:全链路 fail-closed,解析外部输入的每一条失败路径都携带定位信息。
2//!
3//! 与 TS 侧 `MeshletError`(`packages/deep-engine/src/geometry/types.ts`)的 error code
4//! 一一对应,便于双端行为对拍与诊断信息互认。
5
6use std::path::PathBuf;
7
8/// 编译管线错误。`Display` 输出即面向用户的诊断信息,不依赖上下文补充。
9#[derive(Debug, thiserror::Error)]
10pub enum DagError {
11    /// 输入几何非法(TS: `invalid-input`)。
12    #[error("invalid input: {0}")]
13    InvalidInput(String),
14
15    /// 超出构建预算(TS: `budget-exceeded`)。
16    #[error("budget exceeded: {label} (limit {limit})")]
17    BudgetExceeded {
18        /// 超限项的人类可读标签(如 "source triangles")。
19        label: String,
20        /// 允许的上限。
21        limit: u64,
22    },
23
24    /// 内部结果非法或不可表示(TS: `invalid-result` / `overflow`)。
25    #[error("invalid result: {0}")]
26    InvalidResult(String),
27
28    /// 数值溢出:输出尺寸无法在目标宽度内表示。
29    #[error("overflow: {0}")]
30    Overflow(String),
31
32    /// I/O 失败(文件读写)。
33    #[error("io error on {}: {source}", path.display())]
34    Io {
35        /// 涉及的文件路径。
36        path: PathBuf,
37        /// 底层 I/O 错误。
38        #[source]
39        source: std::io::Error,
40    },
41
42    /// `.dgc` 文件结构非法(魔数/版本/截断/校验和不符)。
43    #[error("dgc format error: {0}")]
44    DgcFormat(String),
45
46    /// OBJ 解析失败(行号 + 原因)。
47    #[error("obj parse error at line {line}: {reason}")]
48    ObjParse {
49        /// 出错的 1-based 行号。
50        line: usize,
51        /// 失败原因。
52        reason: String,
53    },
54}
55
56impl DagError {
57    /// 构造 [`DagError::InvalidInput`] 的便捷入口。
58    pub(crate) fn invalid_input(message: impl Into<String>) -> Self {
59        Self::InvalidInput(message.into())
60    }
61
62    /// 构造 [`DagError::BudgetExceeded`] 的便捷入口。
63    pub(crate) fn budget_exceeded(label: impl Into<String>, limit: u64) -> Self {
64        Self::BudgetExceeded { label: label.into(), limit }
65    }
66
67    /// 构造 [`DagError::Overflow`] 的便捷入口。
68    pub(crate) fn overflow(message: impl Into<String>) -> Self {
69        Self::Overflow(message.into())
70    }
71
72    /// 构造 [`DagError::DgcFormat`] 的便捷入口。
73    pub(crate) fn dgc_format(message: impl Into<String>) -> Self {
74        Self::DgcFormat(message.into())
75    }
76}
77
78/// 便捷 [`Result`] 别名。
79pub type DagResult<T> = Result<T, DagError>;