Skip to main content

nargo_ir/
types.rs

1#![warn(missing_docs)]
2
3//! 基础类型模块
4//! 
5//! 提供 HXO IR 系统的基础类型定义,包括错误类型、常量和基础数据结构。
6
7use nargo_types::Error;
8use serde::{Deserialize, Serialize};
9use std::fmt;
10
11/// 代码位置信息
12pub use nargo_types::Span;
13
14/// IR 错误类型
15///
16/// 表示在处理 IR 时可能遇到的各种错误情况
17#[derive(Debug, Clone, PartialEq)]
18pub enum IRError {
19    /// 无效的输入
20    ///
21    /// 当输入数据不符合预期格式时返回此错误
22    InvalidInput(String),
23    /// 结构不一致
24    ///
25    /// 当 IR 结构内部不一致时返回此错误
26    InconsistentStructure(String),
27    /// 超出大小限制
28    ///
29    /// 当 IR 元素大小超出预设限制时返回此错误
30    SizeLimitExceeded(String),
31    /// 循环引用
32    ///
33    /// 当 IR 中存在循环引用时返回此错误
34    CircularReference(String),
35    /// 无效的表达式
36    ///
37    /// 当表达式语法或语义无效时返回此错误
38    InvalidExpression(String),
39    /// 其他错误
40    ///
41    /// 其他未分类的错误
42    Other(String),
43}
44
45impl fmt::Display for IRError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            IRError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
49            IRError::InconsistentStructure(msg) => write!(f, "Inconsistent structure: {}", msg),
50            IRError::SizeLimitExceeded(msg) => write!(f, "Size limit exceeded: {}", msg),
51            IRError::CircularReference(msg) => write!(f, "Circular reference: {}", msg),
52            IRError::InvalidExpression(msg) => write!(f, "Invalid expression: {}", msg),
53            IRError::Other(msg) => write!(f, "{}", msg),
54        }
55    }
56}
57
58impl From<IRError> for Error {
59    fn from(err: IRError) -> Self {
60        Error::external_error("nargo-ir".to_string(), err.to_string(), Span::unknown())
61    }
62}
63
64/// 大小限制常量
65pub const MAX_STRING_LENGTH: usize = 1024 * 1024; // 1MB
66pub const MAX_ARRAY_LENGTH: usize = 10000;
67pub const MAX_OBJECT_SIZE: usize = 1000;
68pub const MAX_RECURSION_DEPTH: usize = 100;
69
70/// 代码中的 trivia 信息,包括空白和注释
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
72pub struct Trivia {
73    /// 前导空白
74    pub leading_whitespace: String,
75    /// 前导注释
76    pub leading_comments: Vec<Comment>,
77    /// 尾随注释
78    pub trailing_comments: Vec<Comment>,
79}
80
81impl Trivia {
82    /// 创建一个空的 Trivia
83    pub fn new() -> Self {
84        Self::default()
85    }
86
87    /// 检查 Trivia 是否为空
88    pub fn is_empty(&self) -> bool {
89        self.leading_whitespace.is_empty() && self.leading_comments.is_empty() && self.trailing_comments.is_empty()
90    }
91}
92
93/// 代码注释
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub struct Comment {
96    /// 注释内容
97    pub content: String,
98    /// 是否为块注释
99    pub is_block: bool,
100    /// 注释的位置信息
101    pub span: Span,
102}
103
104impl Comment {
105    /// 创建一个新的注释
106    pub fn new(content: String, is_block: bool, span: Span) -> Self {
107        Self { content, is_block, span }
108    }
109
110    /// 检查注释是否为空
111    pub fn is_empty(&self) -> bool {
112        self.content.trim().is_empty()
113    }
114}