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
impl DocContent
Sourcepub fn new() -> DocContent
pub fn new() -> DocContent
Content with no tree.
Sourcepub fn place(&mut self, fragment: Arc<Fragment>) -> TreeId
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]);Sourcepub fn add_tree(&mut self, fragment: Arc<Fragment>) -> TreeId
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.
Sourcepub fn resolve(
&mut self,
site: NodeAddr,
target: TreeId,
) -> Result<(), PlacementError>
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).
Sourcepub fn fragment(&self, tree: TreeId) -> Option<&Arc<Fragment>>
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.
Sourcepub fn tree(&self, tree: TreeId) -> Option<&Arc<NodeTree<Flm>>>
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.
Sourcepub fn trees(&self) -> impl Iterator<Item = (TreeId, &Arc<Fragment>)> + '_
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.
Sourcepub fn traversal(&self) -> &[TreeId]
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.
Sourcepub fn resolution(&self) -> &BTreeMap<NodeAddr, TreeId>
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.
Sourcepub fn placement_of(&self, site: NodeAddr) -> Option<TreeId>
pub fn placement_of(&self, site: NodeAddr) -> Option<TreeId>
The tree that the node site places, or None when it places none.
Sourcepub fn placed_at(&self, target: TreeId) -> Option<NodeAddr>
pub fn placed_at(&self, target: TreeId) -> Option<NodeAddr>
The node that places the tree target, or None when no node places
it.
Sourcepub fn walk(&self) -> Walk<'_> ⓘ
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.
Sourcepub fn walk_nodes(
&self,
) -> impl Iterator<Item = (NodeAddr, NodeRef<'_, Flm>)> + '_
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.
Sourcepub fn walk_trees(&self) -> impl Iterator<Item = TreeId> + '_
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
impl Debug for DocContent
Source§impl Default for DocContent
impl Default for DocContent
Source§impl TreeSource for DocContent
impl TreeSource for DocContent
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.
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]
fn listed(&self) -> &[TreeId]
Auto Trait Implementations§
impl !RefUnwindSafe for DocContent
impl !UnwindSafe for DocContent
impl Forget for DocContent
impl Freeze for DocContent
impl Move for DocContent
impl Send for DocContent
impl Sync for DocContent
impl Unpin for DocContent
impl UnsafeUnpin for DocContent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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