pub struct SourceMap { /* private fields */ }Expand description
Byte spans of every node object in a JSON text, addressable by index path and by offset.
Build cost is O(text); span is O(depth) and
path_at is O(log nodes + depth).
use tiptap_rusty_parser::{Document, SourceMap};
let doc = Document::from_json_str(
r#"{"type":"doc","content":[{"type":"callout","x":1,"content":[{"type":"text","text":"hi"}]}]}"#,
).unwrap();
let (text, map) = SourceMap::pretty(doc.root());
let span = map.span(&[0, 0]).unwrap();
assert!(text[span.clone()].contains("\"hi\""));
// Any byte inside the text node resolves back to it.
assert_eq!(map.path_at(span.start + 3), Some(vec![0, 0]));Implementations§
Source§impl SourceMap
impl SourceMap
Sourcepub fn build(text: &str) -> Result<SourceMap, SourceMapError>
pub fn build(text: &str) -> Result<SourceMap, SourceMapError>
Scan text (a JSON node object, any formatting).
Sourcepub fn pretty(node: &Node) -> (String, SourceMap)
pub fn pretty(node: &Node) -> (String, SourceMap)
Serialize node with serde_json pretty formatting (the same text as
Document::to_string_pretty) and
map it.
Sourcepub fn span(&self, path: &[usize]) -> Option<Range<usize>>
pub fn span(&self, path: &[usize]) -> Option<Range<usize>>
Byte range of the node object at path ({ through }), or None
when the path does not resolve.
Sourcepub fn child_count(&self, path: &[usize]) -> Option<usize>
pub fn child_count(&self, path: &[usize]) -> Option<usize>
Number of children of the node at path.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SourceMap
impl RefUnwindSafe for SourceMap
impl Send for SourceMap
impl Sync for SourceMap
impl Unpin for SourceMap
impl UnsafeUnpin for SourceMap
impl UnwindSafe for SourceMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more