Skip to main content

DocContent

Struct DocContent 

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

The content of a document: its tree arena, its traversal list and its resolution map.

It is built once, by adding trees and recording placements, and then only read: several runs may share one document, so nothing about it changes while a run is under way. The module documentation describes the three parts and the walk that they define.

assemble builds the content of a document from its files; DocContent::place, DocContent::add_tree and DocContent::resolve build it tree by tree; Doc::new and Doc::with_options make a document of it.

Implementations§

Source§

impl DocContent

Source

pub fn new() -> DocContent

Content with no tree.

Source

pub fn place(&mut self, fragment: Arc<Fragment>) -> TreeId

Adds a tree for fragment to the arena and appends it to the traversal list, so that the walk visits it after the trees listed before it; returns the tree’s TreeId.

The same Arc<Fragment> added twice gives two distinct trees with separate computed data, because each tree is a separate placement in the document.

See DocContent::add_tree for a tree that the walk reaches through a node that places it, rather than through the traversal list.

§Panics

Panics under the same condition as DocContent::add_tree.

§Examples
use std::sync::Arc;

use flm_core::model::doc::{DocContent, Fragment, FragmentLayout};
use flm_core::lang::{
    build_language, parse_fragment, FlmOrigin, LangInputs, ParseOptions,
};
use flm_core::techy::source::Source;

let lang = build_language(LangInputs::new());
let source = Arc::new(Source::new("hello").with_origin(FlmOrigin::none()));
let fragment = parse_fragment(
    &lang,
    source,
    FragmentLayout::default(),
    &ParseOptions::default(),
)?;

let mut content = DocContent::new();
let shared = Arc::new(fragment);
let first = content.place(Arc::clone(&shared));
let second = content.place(shared);
assert_ne!(first, second);
assert_eq!(content.walk_trees().collect::<Vec<_>>(), [first, second]);
Source

pub fn add_tree(&mut self, fragment: Arc<Fragment>) -> TreeId

Adds a tree for fragment to the arena without appending it to the traversal list; returns the tree’s TreeId.

Use this for a tree that the walk is to reach through a node that places it (DocContent::resolve). A tree that is neither listed nor placed is part of the document but is never walked.

§Panics

Panics if the arena already holds 2³¹ trees, the largest number of trees a TreeId can tell apart from synthesized values.

Source

pub fn resolve( &mut self, site: NodeAddr, target: TreeId, ) -> Result<(), PlacementError>

Records that the node site places the tree target: the walk enters target right after it reaches site.

site is an address in a tree as parsed. Several runs with different rewrites may share one document, and a rewrite produces a new tree with new node identifiers, so an address taken after the rewrites would hold for one run at most; the resolution map of a document therefore uses the node identifiers that the parse produced.

assemble places the tree of every included file that opens with a heading at the node of its \input command. A program that embeds the library may also use this to build a document from several fragments.

§Errors

Returns a PlacementError, and records nothing, when target or site.tree is not a tree of this document (UnknownTree), when site belongs to target itself (SelfPlacement), when another node already places target (AlreadyPlaced), or when site already places a tree (SiteTaken).

Source

pub fn fragment(&self, tree: TreeId) -> Option<&Arc<Fragment>>

The fragment of the tree tree of this document.

Returns None when tree identifies a synthesized value or its index is past the end of the arena.

Source

pub fn tree(&self, tree: TreeId) -> Option<&Arc<NodeTree<Flm>>>

The node tree of the tree tree of this document: the tree of its fragment.

Returns None under the same conditions as DocContent::fragment.

Source

pub fn trees(&self) -> impl Iterator<Item = (TreeId, &Arc<Fragment>)> + '_

Every tree of the arena with its fragment, in the order the trees were added. This is not document order; DocContent::walk_trees gives the trees in document order.

Source

pub fn len(&self) -> usize

The number of trees in the arena, walked or not.

Source

pub fn is_empty(&self) -> bool

Returns true when the arena holds no tree.

Source

pub fn traversal(&self) -> &[TreeId]

The traversal list: the trees the walk visits in their own right, in order. A tree that the walk reaches only through a node that places it is not in this list.

Source

pub fn resolution(&self) -> &BTreeMap<NodeAddr, TreeId>

The resolution map: for each node that places a tree, the tree it places. The keys are addresses in the trees as parsed.

Source

pub fn placement_of(&self, site: NodeAddr) -> Option<TreeId>

The tree that the node site places, or None when it places none.

Source

pub fn placed_at(&self, target: TreeId) -> Option<NodeAddr>

The node that places the tree target, or None when no node places it.

Source

pub fn walk(&self) -> Walk<'_> ⓘ

The walk over the trees as parsed: the steps of the traversal in document order.

The iterator yields a WalkStep when it enters a tree and when it leaves it, and one for every node in between, in pre-order (a node before its children), with the node’s address. This is the library’s definition of document order; see the module documentation for what that order is.

DocContent::walk_nodes and DocContent::walk_trees give the nodes alone and the trees alone.

Source

pub fn walk_nodes( &self, ) -> impl Iterator<Item = (NodeAddr, NodeRef<'_, Flm>)> + '_

The nodes of the walk: every node of every walked tree, in document order, with its address.

Source

pub fn walk_trees(&self) -> impl Iterator<Item = TreeId> + '_

The trees of the walk, in document order, each once.

Trait Implementations§

Source§

impl Debug for DocContent

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for DocContent

Source§

fn default() -> Self

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

impl TreeSource for DocContent

Source§

fn tree_of(&self, id: TreeId) -> Option<&NodeTree<Flm>>

Returns the tree with this id, or None when this set does not hold it.
Source§

impl WalkSource for DocContent

The content of a document is the walk’s source for its trees as parsed: the arena gives the trees, the traversal list the trees listed, and the resolution map the placements.

Source§

fn listed(&self) -> &[TreeId]

The trees of the traversal list, in the order the walk visits them. A tree that the walk reaches only through a node that places it is not in this list.
Source§

fn node_tree(&self, tree: TreeId) -> Option<&NodeTree<Flm>>

The node tree of the tree tree, or None when the source holds no tree with that identifier; the walk then does not enter it.
Source§

fn placement(&self, site: NodeAddr) -> Option<TreeId>

The tree that the node at site, a node of one of the source’s trees, places, or None when it places none.

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.