pub struct Document {
pub root: NodeId,
pub file: PathBuf,
nodes: BTreeMap<NodeId, Node>,
next_id: u64,
}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§nodes: BTreeMap<NodeId, Node>§next_id: u64Implementations§
Source§impl Document
impl Document
Sourcepub fn new(file: PathBuf) -> Self
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);Sourcepub fn alloc(&mut self, spec: NodeSpec) -> NodeId
pub fn alloc(&mut self, spec: NodeSpec) -> NodeId
Allocate a node from spec in the arena and return its assigned
NodeId. The arena fills in the id, an empty children list, and
the default content_hash/style_id placeholders.
§Examples
use std::path::PathBuf;
use mos_core::{Document, NodeId, NodeKind, NodeSpec, SourceSpan};
let file = PathBuf::from("main.mos");
let mut doc = Document::new(file.clone());
let id = doc.alloc(NodeSpec::new(NodeKind::Paragraph, SourceSpan::placeholder(file)));
assert_eq!(id, NodeId(1));Sourcefn node_from_spec(id: NodeId, spec: NodeSpec) -> Node
fn node_from_spec(id: NodeId, spec: NodeSpec) -> Node
Sourcepub fn alloc_child(&mut self, parent: NodeId, spec: NodeSpec) -> NodeId
pub fn alloc_child(&mut self, parent: NodeId, spec: NodeSpec) -> NodeId
Allocate a node from spec 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::{Document, NodeKind, NodeSpec, SourceSpan};
let file = PathBuf::from("main.mos");
let mut doc = Document::new(file.clone());
let child = doc.alloc_child(doc.root, NodeSpec::new(NodeKind::Paragraph, SourceSpan::placeholder(file)));
assert_eq!(doc.get(doc.root).map(|node| node.children.as_slice()), Some(&[child][..]));Sourcepub fn get(&self, id: NodeId) -> Option<&Node>
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));Sourcepub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node>
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")));Sourcepub fn nodes(&self) -> impl Iterator<Item = &Node>
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]);