Skip to main content

SourceConstructor

Struct SourceConstructor 

Source
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

Source

pub fn new() -> Self

Create a new source constructor.

Source

pub fn finish(self) -> SourceDocument

Finish building and return the SourceDocument.

Source

pub fn begin_scope(&mut self) -> Scope

Begin a new scope. Returns a handle that must be passed to end_scope.

Source

pub fn end_scope(&mut self, scope: Scope) -> Result<(), InsertError>

End a scope, restoring to the state when begin_scope was called.

Source

pub fn navigate(&mut self, segment: PathSegment) -> Result<NodeId, InsertError>

Navigate to a child node by path segment.

Source

pub fn require_hole(&self) -> Result<(), InsertError>

Assert that the current node is unbound (a hole).

Source

pub fn bind_primitive( &mut self, value: PrimitiveValue, ) -> Result<(), InsertError>

Bind a primitive value to the current node.

Source

pub fn bind_hole( &mut self, label: Option<Identifier>, ) -> Result<(), InsertError>

Bind a hole (with optional label) to the current node.

Source

pub fn bind_empty_map(&mut self) -> Result<(), InsertError>

Bind an empty map to the current node.

Source

pub fn bind_empty_array(&mut self) -> Result<(), InsertError>

Bind an empty array to the current node.

Source

pub fn bind_empty_tuple(&mut self) -> Result<(), InsertError>

Bind an empty tuple to the current node.

Source

pub fn bind_from( &mut self, value: impl Into<PrimitiveValue>, ) -> Result<(), InsertError>

Bind a value using Into<PrimitiveValue>.

Source

pub fn current_node_id(&self) -> NodeId

Get the current node ID.

Source

pub fn current_path(&self) -> &[PathSegment]

Get the current path from root.

Source

pub fn document(&self) -> &EureDocument

Get a reference to the document being built.

Source

pub fn document_mut(&mut self) -> &mut EureDocument

Get a mutable reference to the document being built.

Source

pub fn begin_eure_block(&mut self)

Enter a new EureSource block (for { eure } patterns).

Source

pub fn set_block_value(&mut self) -> Result<(), InsertError>

Set the value binding for current block (for { = value ... } patterns).

Source

pub fn end_eure_block(&mut self) -> Result<(), InsertError>

End current EureSource block.

Source

pub fn begin_binding(&mut self)

Start a binding statement.

Source

pub fn end_binding_value(&mut self) -> Result<(), InsertError>

End binding #1: path = value.

Source

pub fn end_binding_block(&mut self) -> Result<(), InsertError>

End binding #2/#3: path { eure }.

Source

pub fn begin_section(&mut self)

Start a section header.

Source

pub fn begin_section_items(&mut self)

Begin section #4: @ section (items follow).

Source

pub fn end_section_items(&mut self) -> Result<(), InsertError>

End section #4: finalize section with items body.

Source

pub fn end_section_block(&mut self) -> Result<(), InsertError>

End section #5/#6: @ section { eure }.

Source

pub fn comment(&mut self, comment: Comment)

Add a comment to the pending trivia.

Source

pub fn blank_line(&mut self)

Add a blank line to the pending trivia.

Source

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl InterpreterSink for SourceConstructor

Source§

type Error = InsertError

The error type for operations.
Source§

type Scope = Scope

The scope handle type returned by begin_scope.
Source§

fn begin_scope(&mut self) -> Self::Scope

Begin a new scope. Returns a handle that must be passed to end_scope. Scopes must be ended in LIFO order (most recent first).
Source§

fn end_scope(&mut self, scope: Self::Scope) -> Result<(), Self::Error>

End a scope, restoring the sink to the state when begin_scope was called.
Source§

fn navigate(&mut self, segment: PathSegment) -> Result<NodeId, Self::Error>

Navigate to a child node by path segment. Creates the node if it doesn’t exist.
Source§

fn require_hole(&self) -> Result<(), Self::Error>

Assert that the current node is unbound (a hole). Use this before binding a value to ensure the node hasn’t already been assigned.
Source§

fn bind_primitive(&mut self, value: PrimitiveValue) -> Result<(), Self::Error>

Bind a primitive value to the current node.
Source§

fn bind_hole(&mut self, label: Option<Identifier>) -> Result<(), Self::Error>

Bind a hole (with optional label) to the current node.
Source§

fn bind_empty_map(&mut self) -> Result<(), Self::Error>

Bind an empty map to the current node.
Source§

fn bind_empty_array(&mut self) -> Result<(), Self::Error>

Bind an empty array to the current node.
Source§

fn bind_empty_tuple(&mut self) -> Result<(), Self::Error>

Bind an empty tuple to the current node.
Source§

fn current_node_id(&self) -> NodeId

Get the current node ID.
Source§

fn current_path(&self) -> &[PathSegment]

Get the current path from root.
Source§

fn document(&self) -> &EureDocument

Get a reference to the document being built.
Source§

fn document_mut(&mut self) -> &mut EureDocument

Get a mutable reference to the document being built.
Source§

fn begin_eure_block(&mut self)

Enter a new EureSource block (for { eure } patterns). Pushes a new EureSource onto the builder stack. Default: no-op.
Source§

fn set_block_value(&mut self) -> Result<(), Self::Error>

Set the value binding for current EureSource (for { = 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>

End current EureSource block. Pops from the builder stack. Returns error if the builder stack is in an invalid state. Default: no-op (returns Ok).
Source§

fn begin_binding(&mut self)

Start a binding statement (path ...). Default: no-op.
Source§

fn end_binding_value(&mut self) -> Result<(), Self::Error>

End binding #1: 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>

End binding #2/#3: 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)

Start a section header (@ path ...). Default: no-op.
Source§

fn begin_section_items(&mut self)

Begin section #4: @ section (items follow). Begins collecting items into SectionBody::Items. Default: no-op.
Source§

fn end_section_items(&mut self) -> Result<(), Self::Error>

End section #4: finalize section with items body. Adds SectionSource with SectionBody::Items to current EureSource. Returns error if the builder stack is in an invalid state. Default: no-op (returns Ok).
Source§

fn end_section_block(&mut self) -> Result<(), Self::Error>

End section #5/#6: @ 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).
Source§

fn comment(&mut self, comment: Comment)

Add a comment to the layout. Default: no-op.
Source§

fn blank_line(&mut self)

Add a blank line to the layout. Default: no-op.
Source§

fn bind_from( &mut self, value: impl Into<PrimitiveValue>, ) -> Result<(), Self::Error>

Bind a value using Into<PrimitiveValue>. Convenience method for use with the eure! macro.

Auto Trait Implementations§

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> 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<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.