Skip to main content

FlatNode

Struct FlatNode 

Source
#[non_exhaustive]
pub struct FlatNode {
Show 18 fields pub id: NodeId, pub parent: Option<NodeId>, pub first_child: Option<NodeId>, pub next_sibling: Option<NodeId>, pub span: Range<usize>, pub content_span: Option<Range<usize>>, pub level: Option<u32>, pub kind: Kind, pub text: Option<String>, pub destination: Option<String>, pub head: Option<bool>, pub alignment: Option<Alignment>, pub name: Option<String>, pub directive_form: Option<DirectiveForm>, pub origin: Option<ContainerOrigin>, pub marker_span: Option<Range<usize>>, pub checked: Option<bool>, pub attrs: Vec<(String, Option<String>)>,
}
Expand description

One node of an Editor::nodes snapshot — the flat AST arena as owned Rust data (the JSON-free read path). id indexes the snapshot; parent, first_child, and next_sibling link the tree (None where absent). text is the node’s primary payload (a str’s bytes, a code_block’s body, …) and destination a link/image target, each None when the kind carries no such payload. #[non_exhaustive]: a snapshot node is something twig hands you, never something you build, so it gains a field whenever a node kind’s payload is surfaced (as head/alignment were for tables). Sealing construction here keeps every future addition a minor release instead of a major one.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: NodeId§parent: Option<NodeId>§first_child: Option<NodeId>§next_sibling: Option<NodeId>§span: Range<usize>§content_span: Option<Range<usize>>§level: Option<u32>

A heading’s level; None for every other kind.

§kind: Kind§text: Option<String>§destination: Option<String>§head: Option<bool>

Whether a row/cell belongs to the table head; None for every other kind.

§alignment: Option<Alignment>

A cell’s column alignment; None for every other kind. The delimiter row (|:--|--:|) that spells the alignment out is consumed by the parser and has no node of its own, so this is the only way to recover it. Alignment::Default is a real, unspecified alignment (a bare ---) — distinct from the None a non-cell node reports.

§name: Option<String>

The name a generic container carries in its own payload rather than in kind: an HTML/XML tag ("picture", "source", …) or a directive type ("note", "embed", "vis", …, no leading colons). None for every semantic kind, whose identity is kind alone. With this an html_elements parse’s <picture>/<source> are distinguishable — both report kind == "container" — and so are a ::embed and a ::toc.

A tag and a directive type share one kind because they are one concept in the core: a named container with attributes and children. name is what tells them apart, which is why it is not optional in practice for anything a renderer cares about.

§directive_form: Option<DirectiveForm>

Which of the three generic-container SPELLINGS this node’s producer draws; None when it draws none. Pairs with name: the name says which container, this says how it is written, and a renderer needs both — the same type is a span inline (DirectiveForm::Text), a standalone block with no body (DirectiveForm::Leaf), and a wrapper around blocks (DirectiveForm::Container).

This does not answer “is it a directive?” — use origin. HTML’s parser sets a form on <div> and <span>, the two tags djot and Markdown have generic spellings for, so this reports Some(Container) for a <div> and None for a <video>: right often enough to look usable, wrong on the two tags you meet first.

§origin: Option<ContainerOrigin>

Whether a generic container was WRITTEN as a tag or as a directive; None when nothing recorded it — the node is not a container, or no parser produced it (a Builder tree).

This is the field that separates an HTML <div> from a Markdown :::div. Those two agree on kind ("container"), on name ("div") and on directive_form (Container), field for field, so none of the three can tell you which one you have.

§marker_span: Option<Range<usize>>

The node’s own MARKER — the leading bytes a rich view HIDES, on its opening line: a heading’s #s and the space after them, a list item’s - / 1. , a task item’s marker plus its [x] box, a block quote’s > . None for a node with no leading marker (every inline, a paragraph, a SETEXT heading whose --- sits under the block).

Not derivable from span and content_span. For a heading it happens to be span.start..content_span.start; for a marker-prefixed container it is not, because those report content_span == span — a prefix repeating on every line has no contiguous interior to point at. Before this field the answer was recoverable only by a per-format rule (from the item’s inner paragraph in Markdown, from the item itself in Djot), which is the “which parser produced this?” reasoning a shared AST exists to remove.

Covers ONE LINE, and this node’s own marker alone. For the whole prefix a nested construct sits behind (> 1. [ ] is four nodes’ markers plus the indent between them), call Document::line_prefix.

§checked: Option<bool>

A task list item’s checkbox state; None for every other kind.

The parser has always known this — it is what decides Kind::TaskListItem over Kind::ListItem in the first place — and until now nothing surfaced it, so a consumer rendering a clickable checkbox re-derived the state by scanning the source for [x]. That scan is fooled by a [ in prose, and it asks the bytes a question the tree had already answered. Twig would WRITE a checkbox (Editor::set_task_checked) and not read one back.

None is distinct from Some(false): a consumer treating “not a task item” as unchecked draws an empty box beside every paragraph.

§attrs: Vec<(String, Option<String>)>

The node’s {...} / HTML attributes as (key, value) pairs in source order (empty when it has none). A bare attribute (HTML disabled, or a <source media=…> used as a flag) has a None value.

Trait Implementations§

Source§

impl Clone for FlatNode

Source§

fn clone(&self) -> FlatNode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FlatNode

Source§

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

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

impl Eq for FlatNode

Source§

impl PartialEq for FlatNode

Source§

fn eq(&self, other: &FlatNode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FlatNode

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.