Skip to main content

Projector

Struct Projector 

Source
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

Source

pub fn new(options: ProjectOptions) -> Projector

Creates a projector with the given options.

Source

pub fn options(&self) -> &ProjectOptions

Returns the projector’s options.

Source

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.

Source

pub fn nodes<'t>( &self, nodes: impl IntoIterator<Item = NodeRef<'t, Flm>>, ) -> SerialValue

Projects the given nodes, as a list of their projections.

Source

pub fn node(&self, node: &NodeRef<'_, Flm>) -> SerialValue

Projects one node, in the shape the module documentation gives.

Source

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.

Source

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

Source

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

Source

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.

Source

pub fn diagnostics<'a>( &self, diagnostics: impl IntoIterator<Item = &'a FlmDiagnostic>, ) -> SerialValue

Projects the given diagnostics, as a list of their projections.

Source

pub fn span(&self, span: &FlmSpan) -> String

Renders a span as L:C-L:C (start, exclusive end), or L:C when the span is empty; as byte offsets (@start-end, or @offset when empty) when the source is too large for techy to index by line.

Source

pub fn position(&self, span: &FlmSpan) -> String

Renders the start of a span as L:C, or as b<offset> when the source is too large for techy to index by line. This is the position part of a node reference (node_ref).

Source§

impl Projector

Source

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.

Source

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§

Source§

impl Debug for Projector

Source§

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

Formats the value using the given formatter. Read more

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.