Struct Source

Source
pub struct Source { /* private fields */ }
Expand description

源代码表示结构。

统一的源代码抽象,支持从文件、字符串等多种来源加载代码, 并提供完整的源码位置追踪和错误报告能力。

§设计特点

§内存共享

使用 Arc<Vec<char>> 来共享源码内容,避免重复拷贝, 多个 Token 和 AST 节点可以安全地引用同一份源码。

§路径追踪

可选的文件路径信息,用于错误报告和调试时显示文件来源。

§Unicode 友好

内部使用 Vec<char> 存储,完整支持 Unicode 字符, 便于处理多语言源码和字符边界计算。

§序列化支持

实现了 Serde 序列化,支持 AST 的持久化存储和网络传输。

§使用示例

// 从字符串创建
let source = Source::from_string("let x = 42".to_string());

// 从文件创建
let source = Source::from_file("example.onion")?;

// 带路径信息创建
let source = Source::from_string_with_file_path(
    "let y = x + 1".to_string(),
    "inline.onion"
);

Implementations§

Source§

impl Source

Source

pub fn from_string(source: String) -> Self

从字符串创建 Source。

创建不包含文件路径信息的源码实例,适用于内存中的代码片段、 动态生成的代码或测试用例。

§参数
  • source:源代码字符串
§返回

新的 Source 实例

§示例
let source = Source::from_string("let x = 42".to_string());
Source

pub fn from_string_with_file_path<P: AsRef<Path>>( source: String, path: P, ) -> Self

从字符串和文件路径创建 Source。

创建包含文件路径信息的源码实例,适用于需要保留来源信息的场景, 如编辑器插件、调试器或错误报告。

§参数
  • source:源代码字符串
  • path:文件路径,可以是任何可转换为 Path 的类型
§返回

新的 Source 实例,包含路径信息

§示例
let source = Source::from_string_with_file_path(
    "let y = x + 1".to_string(),
    "math.onion"
);
Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>

从文件创建 Source。

读取文件内容并创建 Source 实例,自动设置文件路径信息。 这是从磁盘文件加载源码的标准方法。

§参数
  • path:文件路径,可以是任何可转换为 Path 的类型
§返回
  • Ok(Source):成功时返回包含文件内容和路径的 Source
  • Err(std::io::Error):文件读取失败时返回 IO 错误
§错误

当文件不存在、权限不足或读取过程中发生 IO 错误时返回错误。

§示例
let source = Source::from_file("example.onion")?;
Source

pub fn file_path(&self) -> Option<&Path>

获取文件路径。

返回源码的文件路径,如果源码不是来自文件则返回 None。 主要用于错误报告和调试信息显示。

§返回
  • Some(&Path):如果源码来自文件
  • None:如果源码来自内存字符串
Source

pub fn content(&self) -> &Arc<Vec<char>>

获取源码内容的共享引用。

返回内部字符向量的 Arc 引用,允许多个地方共享同一份源码内容。 主要供内部模块使用,如词法分析器和 AST 节点。

§返回

源码字符向量的共享引用

Source

pub fn content_str(&self) -> String

获取源码内容的字符串表示。

将内部的字符向量转换为字符串,主要用于显示、打印或与 需要字符串的 API 交互。

§返回

源码的字符串表示

Trait Implementations§

Source§

impl Clone for Source

Source§

fn clone(&self) -> Source

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Debug 格式化输出。

提供源码的调试信息,包括文件路径(或标记为内存中)和 内容的前 40 个字符预览。有助于调试和日志输出。

§输出格式

Source(file: "path/to/file.onion", content: "let x = 42...")

对于内存中的源码: Source(file: "<in-memory>", content: "let x = 42...")

Source§

impl Deref for Source

Source§

fn deref(&self) -> &Self::Target

解引用到内部的字符向量。

允许 Source 像 Arc<Vec<char>> 一样使用,提供对字符向量的直接访问。 这使得可以直接在 Source 上使用索引、切片等操作。

§示例
let source = Source::from_string("hello".to_string());
let first_char = source[0];  // 等价于 source.content()[0]
let slice = &source[1..3];   // 等价于 &source.content()[1..3]
Source§

type Target = Arc<Vec<char>>

The resulting type after dereferencing.
Source§

impl<'de> Deserialize<'de> for Source

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

从字符串反序列化 Source。

将序列化的字符串转换回 Source 结构,文件路径信息设置为 None, 因为序列化时未包含路径信息。

Source§

impl Display for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Display 格式化输出。

提供简洁的源码标识,主要用于用户友好的错误消息和日志输出。 只显示文件路径或匿名标记,不包含源码内容。

§输出格式
  • 有文件路径:/path/to/file.onion
  • 无文件路径:<anonymous>
Source§

impl From<&str> for Source

Source§

fn from(source: &str) -> Self

从字符串切片转换为 Source。

提供便捷的转换方法,会将字符串切片复制为 String 再创建 Source。

Source§

impl From<String> for Source

Source§

fn from(source: String) -> Self

从 String 转换为 Source。

提供便捷的转换方法,等价于 Source::from_string()

Source§

impl Serialize for Source

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

序列化 Source 为字符串。

只序列化源码内容,忽略文件路径信息。这样可以避免路径信息在不同环境间的兼容性问题,同时减少序列化数据的大小。

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl Send for Source

§

impl Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,