pub struct Projector { /* private fields */ }Expand description
Projects trees, nodes, tables and diagnostics with one set of
ProjectOptions, caching the line positions it computes.
One projector serves any number of projections, and every method takes
&self, so all the entries of a table are projected through one projector.
Turning a byte offset into a line and column requires the line starts of the
source, so the projector caches them per source. The cache is behind a lock
and never changes an output, so the methods need no &mut.
The free functions of this module (project_tree, project_node, and so
on) build a projector for a single call; use a Projector directly when
projecting more than one thing. The module documentation gives the
shape of every projection.
Implementations§
Source§impl Projector
impl Projector
Sourcepub fn new(options: ProjectOptions) -> Projector
pub fn new(options: ProjectOptions) -> Projector
Creates a projector with the given options.
Sourcepub fn options(&self) -> &ProjectOptions
pub fn options(&self) -> &ProjectOptions
Returns the projector’s options.
Sourcepub fn tree(&self, tree: &NodeTree<Flm>) -> SerialValue
pub fn tree(&self, tree: &NodeTree<Flm>) -> SerialValue
Projects a whole tree: the list of its root’s children (the root container itself is not shown), or a one-item list holding the root when the root is not a list node.
Sourcepub fn nodes<'t>(
&self,
nodes: impl IntoIterator<Item = NodeRef<'t, Flm>>,
) -> SerialValue
pub fn nodes<'t>( &self, nodes: impl IntoIterator<Item = NodeRef<'t, Flm>>, ) -> SerialValue
Projects the given nodes, as a list of their projections.
Sourcepub fn node(&self, node: &NodeRef<'_, Flm>) -> SerialValue
pub fn node(&self, node: &NodeRef<'_, Flm>) -> SerialValue
Projects one node, in the shape the module documentation gives.
Sourcepub fn source_map(&self, source_map: &TextSourceMap) -> SerialValue
pub fn source_map(&self, source_map: &TextSourceMap) -> SerialValue
Projects a text source map as [{"len", "span"}, …]: for each part of
the text run, its length in bytes and the span of source text it comes
from, as line:col.
Sourcepub fn node_ref(&self, tree: &NodeTree<Flm>, node: NodeId) -> SerialValue
pub fn node_ref(&self, tree: &NodeTree<Flm>, node: NodeId) -> SerialValue
Renders a reference to a node of tree as elem@line:col: the node’s
element and the position where its span starts. This is how a table entry
that points at a node is shown. A node id that tree does not hold
renders as ?@<the id's numeric index>.
Sourcepub fn tree_ref(&self, tree: TreeId) -> SerialValue
pub fn tree_ref(&self, tree: TreeId) -> SerialValue
Renders a reference to a tree as the tree id’s display form (tree#0
for a document tree, value#3 for a synthesized value).
Sourcepub fn diagnostic(&self, diagnostic: &FlmDiagnostic) -> SerialValue
pub fn diagnostic(&self, diagnostic: &FlmDiagnostic) -> SerialValue
Projects one diagnostic as {"id", "severity", "span", "file"?, "data"}
(see the module documentation); the message is never shown.
Sourcepub fn diagnostics<'a>(
&self,
diagnostics: impl IntoIterator<Item = &'a FlmDiagnostic>,
) -> SerialValue
pub fn diagnostics<'a>( &self, diagnostics: impl IntoIterator<Item = &'a FlmDiagnostic>, ) -> SerialValue
Projects the given diagnostics, as a list of their projections.
Source§impl Projector
impl Projector
Sourcepub fn table_projection<'a>(
&'a self,
trees: &'a dyn TreeSource,
) -> TableProjection<'a>
pub fn table_projection<'a>( &'a self, trees: &'a dyn TreeSource, ) -> TableProjection<'a>
Creates the context through which a table projects itself, resolving node
references in trees.
Sourcepub fn table<T: Table>(
&self,
name: &str,
table: &T,
trees: &dyn TreeSource,
) -> SerialValue
pub fn table<T: Table>( &self, name: &str, table: &T, trees: &dyn TreeSource, ) -> SerialValue
Projects a table under its name, {"<name>": <projection>}, resolving
its node references in trees.
The inner projection is the table’s own Table::project, called
through a table_projection over trees;
project_table then puts it under name. Pass the table’s registered
name: a table is identified by its name, never by its Rust type.
§Examples
use std::collections::BTreeMap;
use std::sync::Arc;
use flm_core::model::addr::TreeId;
use flm_core::core::data::table::{ProjectCx, Table};
use flm_core::dump::{ProjectOptions, Projector, to_json};
use flm_core::lang::flm::Flm;
use flm_core::techy::core::node::NodeTree;
use flm_core::techy::serialize::SerialValue;
#[derive(Default)]
struct Count(i64);
impl Table for Count {
fn absorb(&mut self, piece: Count) {
self.0 += piece.0;
}
fn project(&self, _cx: &dyn ProjectCx) -> SerialValue {
SerialValue::Int(self.0)
}
}
// This table holds no node address, so an empty set of trees will do.
let trees: BTreeMap<TreeId, Arc<NodeTree<Flm>>> = BTreeMap::new();
let projector = Projector::new(ProjectOptions::new());
let value = projector.table("example.count", &Count(3), &trees);
assert_eq!(to_json(&value)?, r#"{"example.count":3}"#);Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Projector
impl Forget for Projector
impl Move for Projector
impl RefUnwindSafe for Projector
impl Send for Projector
impl Sync for Projector
impl Unpin for Projector
impl UnsafeUnpin for Projectorwhere
Align4<u32>: UnsafeUnpin,
Align1<u8>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<CacheEntry<FlmOrigin>>: UnsafeUnpin,
impl UnwindSafe for Projector
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