pub struct SourceConstructor { /* private fields */ }Expand description
A document constructor that tracks source layout for round-trip formatting.
SourceConstructor wraps DocumentConstructor and records source structure
(sections, bindings, comments) as an AST. This enables converting from other
formats (like TOML) while preserving their structure.
§Example
let mut constructor = SourceConstructor::new();
// Build: name = "Alice" (pattern #1)
constructor.begin_binding();
let scope = constructor.begin_scope();
constructor.navigate(PathSegment::Ident("name".parse()?))?;
constructor.bind_primitive("Alice".into())?;
constructor.end_scope(scope)?;
constructor.end_binding_value().unwrap();
// Build: user { name = "Bob" } (pattern #2)
constructor.begin_binding();
let scope = constructor.begin_scope();
constructor.navigate(PathSegment::Ident("user".parse()?))?;
constructor.begin_eure_block();
constructor.begin_binding();
let inner_scope = constructor.begin_scope();
constructor.navigate(PathSegment::Ident("name".parse()?))?;
constructor.bind_primitive("Bob".into())?;
constructor.end_scope(inner_scope)?;
constructor.end_binding_value().unwrap();
constructor.end_eure_block().unwrap();
constructor.end_scope(scope)?;
constructor.end_binding_block().unwrap();
let source_doc = constructor.finish();Implementations§
Source§impl SourceConstructor
impl SourceConstructor
Sourcepub fn finish(self) -> SourceDocument
pub fn finish(self) -> SourceDocument
Finish building and return the SourceDocument.
Sourcepub fn begin_scope(&mut self) -> Scope
pub fn begin_scope(&mut self) -> Scope
Begin a new scope. Returns a handle that must be passed to end_scope.
Sourcepub fn end_scope(&mut self, scope: Scope) -> Result<(), InsertError>
pub fn end_scope(&mut self, scope: Scope) -> Result<(), InsertError>
End a scope, restoring to the state when begin_scope was called.
Navigate to a child node by path segment.
Sourcepub fn require_hole(&self) -> Result<(), InsertError>
pub fn require_hole(&self) -> Result<(), InsertError>
Assert that the current node is unbound (a hole).
Sourcepub fn bind_primitive(
&mut self,
value: PrimitiveValue,
) -> Result<(), InsertError>
pub fn bind_primitive( &mut self, value: PrimitiveValue, ) -> Result<(), InsertError>
Bind a primitive value to the current node.
Sourcepub fn bind_hole(
&mut self,
label: Option<Identifier>,
) -> Result<(), InsertError>
pub fn bind_hole( &mut self, label: Option<Identifier>, ) -> Result<(), InsertError>
Bind a hole (with optional label) to the current node.
Sourcepub fn bind_empty_map(&mut self) -> Result<(), InsertError>
pub fn bind_empty_map(&mut self) -> Result<(), InsertError>
Bind an empty map to the current node.
Sourcepub fn bind_empty_array(&mut self) -> Result<(), InsertError>
pub fn bind_empty_array(&mut self) -> Result<(), InsertError>
Bind an empty array to the current node.
Sourcepub fn bind_empty_tuple(&mut self) -> Result<(), InsertError>
pub fn bind_empty_tuple(&mut self) -> Result<(), InsertError>
Bind an empty tuple to the current node.
Sourcepub fn bind_from(
&mut self,
value: impl Into<PrimitiveValue>,
) -> Result<(), InsertError>
pub fn bind_from( &mut self, value: impl Into<PrimitiveValue>, ) -> Result<(), InsertError>
Bind a value using Into<PrimitiveValue>.
Sourcepub fn current_node_id(&self) -> NodeId
pub fn current_node_id(&self) -> NodeId
Get the current node ID.
Sourcepub fn current_path(&self) -> &[PathSegment]
pub fn current_path(&self) -> &[PathSegment]
Get the current path from root.
Sourcepub fn document(&self) -> &EureDocument
pub fn document(&self) -> &EureDocument
Get a reference to the document being built.
Sourcepub fn document_mut(&mut self) -> &mut EureDocument
pub fn document_mut(&mut self) -> &mut EureDocument
Get a mutable reference to the document being built.
Sourcepub fn begin_eure_block(&mut self)
pub fn begin_eure_block(&mut self)
Enter a new EureSource block (for { eure } patterns).
Sourcepub fn set_block_value(&mut self) -> Result<(), InsertError>
pub fn set_block_value(&mut self) -> Result<(), InsertError>
Set the value binding for current block (for { = value ... } patterns).
Sourcepub fn end_eure_block(&mut self) -> Result<(), InsertError>
pub fn end_eure_block(&mut self) -> Result<(), InsertError>
End current EureSource block.
Sourcepub fn begin_binding(&mut self)
pub fn begin_binding(&mut self)
Start a binding statement.
Sourcepub fn end_binding_value(&mut self) -> Result<(), InsertError>
pub fn end_binding_value(&mut self) -> Result<(), InsertError>
End binding #1: path = value.
Sourcepub fn end_binding_block(&mut self) -> Result<(), InsertError>
pub fn end_binding_block(&mut self) -> Result<(), InsertError>
End binding #2/#3: path { eure }.
Sourcepub fn begin_section(&mut self)
pub fn begin_section(&mut self)
Start a section header.
Sourcepub fn begin_section_items(&mut self)
pub fn begin_section_items(&mut self)
Begin section #4: @ section (items follow).
Sourcepub fn end_section_items(&mut self) -> Result<(), InsertError>
pub fn end_section_items(&mut self) -> Result<(), InsertError>
End section #4: finalize section with items body.
Sourcepub fn end_section_block(&mut self) -> Result<(), InsertError>
pub fn end_section_block(&mut self) -> Result<(), InsertError>
End section #5/#6: @ section { eure }.
Sourcepub fn blank_line(&mut self)
pub fn blank_line(&mut self)
Add a blank line to the pending trivia.
Sourcepub fn add_trivia(&mut self, trivia: Trivia)
pub fn add_trivia(&mut self, trivia: Trivia)
Add trivia (comment or blank line) to the pending trivia.
Trait Implementations§
Source§impl Default for SourceConstructor
impl Default for SourceConstructor
Source§impl InterpreterSink for SourceConstructor
impl InterpreterSink for SourceConstructor
Source§type Error = InsertError
type Error = InsertError
Source§fn begin_scope(&mut self) -> Self::Scope
fn begin_scope(&mut self) -> Self::Scope
end_scope.
Scopes must be ended in LIFO order (most recent first).Source§fn end_scope(&mut self, scope: Self::Scope) -> Result<(), Self::Error>
fn end_scope(&mut self, scope: Self::Scope) -> Result<(), Self::Error>
begin_scope was called.Source§fn require_hole(&self) -> Result<(), Self::Error>
fn require_hole(&self) -> Result<(), Self::Error>
Source§fn bind_primitive(&mut self, value: PrimitiveValue) -> Result<(), Self::Error>
fn bind_primitive(&mut self, value: PrimitiveValue) -> Result<(), Self::Error>
Source§fn bind_hole(&mut self, label: Option<Identifier>) -> Result<(), Self::Error>
fn bind_hole(&mut self, label: Option<Identifier>) -> Result<(), Self::Error>
Source§fn bind_empty_map(&mut self) -> Result<(), Self::Error>
fn bind_empty_map(&mut self) -> Result<(), Self::Error>
Source§fn bind_empty_array(&mut self) -> Result<(), Self::Error>
fn bind_empty_array(&mut self) -> Result<(), Self::Error>
Source§fn bind_empty_tuple(&mut self) -> Result<(), Self::Error>
fn bind_empty_tuple(&mut self) -> Result<(), Self::Error>
Source§fn current_node_id(&self) -> NodeId
fn current_node_id(&self) -> NodeId
Source§fn current_path(&self) -> &[PathSegment]
fn current_path(&self) -> &[PathSegment]
Source§fn document(&self) -> &EureDocument
fn document(&self) -> &EureDocument
Source§fn document_mut(&mut self) -> &mut EureDocument
fn document_mut(&mut self) -> &mut EureDocument
Source§fn begin_eure_block(&mut self)
fn begin_eure_block(&mut self)
{ eure } patterns).
Pushes a new EureSource onto the builder stack.
Default: no-op.Source§fn set_block_value(&mut self) -> Result<(), Self::Error>
fn set_block_value(&mut self) -> Result<(), Self::Error>
{ = value ... } patterns).
Called after bind_* to record the value node.
Returns error if called without a preceding bind operation.
Default: no-op (returns Ok).Source§fn end_eure_block(&mut self) -> Result<(), Self::Error>
fn end_eure_block(&mut self) -> Result<(), Self::Error>
Source§fn begin_binding(&mut self)
fn begin_binding(&mut self)
path ...).
Default: no-op.Source§fn end_binding_value(&mut self) -> Result<(), Self::Error>
fn end_binding_value(&mut self) -> Result<(), Self::Error>
path = value.
Adds BindingSource with BindSource::Value to current EureSource.
Returns error if called without a preceding bind operation.
Default: no-op (returns Ok).Source§fn end_binding_block(&mut self) -> Result<(), Self::Error>
fn end_binding_block(&mut self) -> Result<(), Self::Error>
path { eure }.
Adds BindingSource with BindSource::Block to current EureSource.
The block’s EureSource was built between begin_eure_block/end_eure_block.
Returns error if called without a preceding end_eure_block.
Default: no-op (returns Ok).Source§fn begin_section(&mut self)
fn begin_section(&mut self)
@ path ...).
Default: no-op.Source§fn begin_section_items(&mut self)
fn begin_section_items(&mut self)
@ section (items follow).
Begins collecting items into SectionBody::Items.
Default: no-op.Source§fn end_section_items(&mut self) -> Result<(), Self::Error>
fn end_section_items(&mut self) -> Result<(), Self::Error>
Source§fn end_section_block(&mut self) -> Result<(), Self::Error>
fn end_section_block(&mut self) -> Result<(), Self::Error>
@ section { eure }.
Adds SectionSource with SectionBody::Block to current EureSource.
The block’s EureSource was built between begin_eure_block/end_eure_block.
Returns error if called without a preceding end_eure_block.
Default: no-op (returns Ok).