Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub root: NodeId,
    pub file: PathBuf,
    /* private fields */
}
Expand description

The lowered semantic document graph (manifest §5, §6 stage 2).

Owns every Node and exposes them through their stable NodeId. MVP 0 stores nodes in insertion order; the manifest §5.1 hash-derived IDs land alongside the cache work in MVP 5.

§Examples

use std::path::PathBuf;

use mos_core::{Document, NodeId};

let doc = Document::new(PathBuf::from("main.mos"));

assert_eq!(doc.root, NodeId(0));

Fields§

§root: NodeId§file: PathBuf

Implementations§

Source§

impl Document

Source

pub fn new(file: PathBuf) -> Self

Create an empty document rooted at file. Allocates the Document root node (NodeId(0)) eagerly so callers can append children to it immediately.

§Examples
use std::path::PathBuf;

use mos_core::Document;

let doc = Document::new(PathBuf::from("main.mos"));

assert_eq!(doc.len(), 1);
Source

pub fn alloc(&mut self, node: Node) -> NodeId

Allocate node in the arena and return its assigned NodeId. The id field on the input is overwritten with the fresh ID.

§Examples
use std::path::PathBuf;

use mos_core::{AttrMap, ContentHash, Document, Node, NodeId, NodeKind, SourceSpan, StyleId};

let file = PathBuf::from("main.mos");
let mut doc = Document::new(file.clone());
let id = doc.alloc(Node {
    id: NodeId::default(),
    kind: NodeKind::Paragraph,
    span: SourceSpan::placeholder(file),
    content_hash: ContentHash::default(),
    style_id: StyleId::default(),
    children: Vec::new(),
    attributes: AttrMap::new(),
});

assert_eq!(id, NodeId(1));
Source

pub fn alloc_child(&mut self, parent: NodeId, node: Node) -> NodeId

Allocate node as a child of parent and return its NodeId.

§Panics

Panics if parent is not a node already allocated by this Document. Silently producing detached nodes would hide lowerer bugs in release builds, so this is intentionally a release-time assertion rather than a debug_assert!.

§Examples
use std::path::PathBuf;

use mos_core::{AttrMap, ContentHash, Document, Node, NodeId, NodeKind, SourceSpan, StyleId};

let file = PathBuf::from("main.mos");
let mut doc = Document::new(file.clone());
let child = doc.alloc_child(doc.root, Node {
    id: NodeId::default(),
    kind: NodeKind::Paragraph,
    span: SourceSpan::placeholder(file),
    content_hash: ContentHash::default(),
    style_id: StyleId::default(),
    children: Vec::new(),
    attributes: AttrMap::new(),
});

assert_eq!(doc.get(doc.root).map(|node| node.children.as_slice()), Some(&[child][..]));
Source

pub fn get(&self, id: NodeId) -> Option<&Node>

Get a node by id.

§Examples
use std::path::PathBuf;

use mos_core::{Document, NodeKind};

let doc = Document::new(PathBuf::from("main.mos"));

assert_eq!(doc.get(doc.root).map(|node| node.kind), Some(NodeKind::Document));
Source

pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node>

Mutable accessor for a single node. Used by the resolver (manifest §6 stage 3) to back-patch attributes like number onto sections and text onto @label references.

§Examples
use std::path::PathBuf;

use mos_core::{AttrValue, Document};

let mut doc = Document::new(PathBuf::from("main.mos"));
if let Some(root) = doc.get_mut(doc.root) {
    root.attributes.insert("title".to_owned(), AttrValue::Str("Demo".to_owned()));
}

assert!(doc.get(doc.root).is_some_and(|node| node.attributes.contains_key("title")));
Source

pub fn nodes(&self) -> impl Iterator<Item = &Node>

Iterate over every node in the arena in insertion order.

§Examples
use std::path::PathBuf;

use mos_core::{Document, NodeKind};

let doc = Document::new(PathBuf::from("main.mos"));
let kinds: Vec<NodeKind> = doc.nodes().map(|node| node.kind).collect();

assert_eq!(kinds, vec![NodeKind::Document]);
Source

pub fn len(&self) -> usize

Total number of nodes including the document root.

§Examples
use std::path::PathBuf;

use mos_core::Document;

let doc = Document::new(PathBuf::from("main.mos"));

assert_eq!(doc.len(), 1);
Source

pub fn is_empty(&self) -> bool

Return whether the document has no semantic content beyond the root.

§Examples
use std::path::PathBuf;

use mos_core::Document;

let doc = Document::new(PathBuf::from("main.mos"));

assert!(doc.is_empty());

Trait Implementations§

Source§

impl Debug for Document

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