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: PathBufImplementations§
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, node: Node) -> NodeId
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));Sourcepub fn alloc_child(&mut self, parent: NodeId, node: Node) -> NodeId
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][..]));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]);