pub trait AstNode {
// Required methods
fn append_source(&self, sink: &mut String, source: Option<&str>);
fn byte_span(&self) -> ByteSpan;
// Provided methods
fn source_span(&self, source_map: &SourceMap<'_>) -> Option<SourceSpan> { ... }
fn to_source(&self, source: Option<&str>) -> String { ... }
}Expand description
Trait implemented by all AST node types. Provides source reconstruction and span access methods.
All AST node types implement this trait via
#[inherent] impl AstNode, giving each node both inherent
methods (no trait import needed) and a trait bound for generic
utilities (error formatters, linters, etc.).
§Source Reconstruction Modes
-
Source-slice mode (fast, lossless): When
sourceisSome(s),append_sourceslices&s[span.start.byte_offset..span.end.byte_offset]. This is the common path for string-based token sources. Zero allocation. -
Synthetic-formatting mode (slower, lossy): When
sourceisNone,append_sourcewalks the AST and emits keywords, names, values, and punctuation with standard spacing. The output is semantically equivalent but not formatting-identical.
§Span Access
Every AST node carries a ByteSpan recording its
byte-offset range in the source text.
byte_span() exposes this uniformly
across all node types, and
source_span() resolves it to
line/column coordinates on demand via a SourceMap.
Required Methods§
Sourcefn append_source(&self, sink: &mut String, source: Option<&str>)
fn append_source(&self, sink: &mut String, source: Option<&str>)
Append this node’s source representation to sink.
When source is Some(s), slices the original source text
directly via byte offsets (zero-copy, lossless). When
source is None, reconstructs from semantic data with
standard formatting (lossy but semantically equivalent).
Sourcefn byte_span(&self) -> ByteSpan
fn byte_span(&self) -> ByteSpan
Returns this node’s byte-offset span within the source text.
The returned ByteSpan is a compact [start, end)
byte range that can be resolved to line/column positions
via source_span() or
ByteSpan::resolve().