pub struct CstRootNode(/* private fields */);
Expand description
Root node in the file.
The root node contains one value, whitespace, and comments.
Implementations§
Source§impl CstRootNode
impl CstRootNode
Sourcepub fn parent(&self) -> Option<CstContainerNode>
pub fn parent(&self) -> Option<CstContainerNode>
Parent of the node.
Returns None
if this node has become disconnected from
the tree by being removed.
Sourcepub fn ancestors(&self) -> impl Iterator<Item = CstContainerNode>
pub fn ancestors(&self) -> impl Iterator<Item = CstContainerNode>
An iterator of ancestors of this node.
Sourcepub fn child_index(&self) -> usize
pub fn child_index(&self) -> usize
Current child index of the node within the children of the parent node.
Sourcepub fn previous_sibling(&self) -> Option<CstNode>
pub fn previous_sibling(&self) -> Option<CstNode>
Node that comes before this one that shares the same parent.
Sourcepub fn previous_siblings(&self) -> impl Iterator<Item = CstNode>
pub fn previous_siblings(&self) -> impl Iterator<Item = CstNode>
Siblings coming before this node. This does not include cousins.
Sourcepub fn next_sibling(&self) -> Option<CstNode>
pub fn next_sibling(&self) -> Option<CstNode>
Node that comes after this one that shares the same parent.
Sourcepub fn next_siblings(&self) -> impl Iterator<Item = CstNode>
pub fn next_siblings(&self) -> impl Iterator<Item = CstNode>
Siblings coming after this node. This does not include cousins.
Sourcepub fn indent_text(&self) -> Option<String>
pub fn indent_text(&self) -> Option<String>
Returns the indentation text if it can be determined.
Sourcepub fn trailing_comma(&self) -> Option<CstToken>
pub fn trailing_comma(&self) -> Option<CstToken>
Gets the trailing comma token of the node, if it exists.
Sourcepub fn uses_trailing_commas(&self) -> bool
pub fn uses_trailing_commas(&self) -> bool
Infers if the node or appropriate ancestor uses trailing commas.
Sourcepub fn children_exclude_trivia_and_tokens(&self) -> Vec<CstNode>
pub fn children_exclude_trivia_and_tokens(&self) -> Vec<CstNode>
Children of the current node excluding comments, whitespace, newlines, and tokens.
Sourcepub fn child_at_index(&self, index: usize) -> Option<CstNode>
pub fn child_at_index(&self, index: usize) -> Option<CstNode>
Gets the child at the specified index.
Source§impl CstRootNode
impl CstRootNode
Sourcepub fn parse(
text: &str,
parse_options: &ParseOptions,
) -> Result<Self, ParseError>
pub fn parse( text: &str, parse_options: &ParseOptions, ) -> Result<Self, ParseError>
Parses the text into a CST.
WARNING: You MUST not drop the root node for the duration of using the CST or a panic could occur in certain scenarios. This is because the CST uses weak references for ancestors and if the root node is dropped then the weak reference will be lost and the CST will panic to prevent bugs when a descendant node attempts to access an ancestor that was dropped.
use jsonc_parser::cst::CstRootNode;
use jsonc_parser::ParseOptions;
use jsonc_parser::json;
let json_text = r#"{
// comment
"data": 123
}"#;
let root = CstRootNode::parse(json_text, &ParseOptions::default()).unwrap();
let root_obj = root.object_value_or_set();
root_obj.get("data").unwrap().set_value(json!({
"nested": true
}));
root_obj.append("new_key", json!([456, 789, false]));
assert_eq!(root.to_string(), r#"{
// comment
"data": {
"nested": true
},
"new_key": [456, 789, false]
}"#);
Sourcepub fn single_indent_text(&self) -> Option<String>
pub fn single_indent_text(&self) -> Option<String>
Computes the single indentation text of the file.
Sourcepub fn newline_kind(&self) -> CstNewlineKind
pub fn newline_kind(&self) -> CstNewlineKind
Newline kind used within the JSON text.
Sourcepub fn set_value(&self, root_value: CstInputValue)
pub fn set_value(&self, root_value: CstInputValue)
Sets potentially replacing the root value found in the JSON document.
Sourcepub fn object_value(&self) -> Option<CstObject>
pub fn object_value(&self) -> Option<CstObject>
Gets the root value if its an object.
Sourcepub fn object_value_or_create(&self) -> Option<CstObject>
pub fn object_value_or_create(&self) -> Option<CstObject>
Gets or creates the root value as an object, returns Some
if successful
or None
if the root value already exists and is not an object.
Note: Use .object_value_or_set()
to overwrite the root value when
it’s not an object.
Sourcepub fn object_value_or_set(&self) -> CstObject
pub fn object_value_or_set(&self) -> CstObject
Gets the root value if it’s an object or sets the root value as an object.
Note: Use .object_value_or_create()
to not overwrite the root value
when it’s not an object.
Sourcepub fn array_value(&self) -> Option<CstArray>
pub fn array_value(&self) -> Option<CstArray>
Gets the value if its an array.
Sourcepub fn array_value_or_create(&self) -> Option<CstArray>
pub fn array_value_or_create(&self) -> Option<CstArray>
Gets or creates the root value as an object, returns Some
if successful
or None
if the root value already exists and is not an object.
Note: Use .array_value_or_set()
to overwrite the root value when
it’s not an array.
Sourcepub fn array_value_or_set(&self) -> CstArray
pub fn array_value_or_set(&self) -> CstArray
Gets the root value if it’s an object or sets the root value as an object.
Note: Use .array_value_or_create()
to not overwrite the root value
when it’s not an object.
Sourcepub fn set_trailing_commas(&self, mode: TrailingCommaMode)
pub fn set_trailing_commas(&self, mode: TrailingCommaMode)
Ensures this object’s values use trailing commas.
Note: This does not cause future values to use trailing commas. That will always be determined based on whether the file uses trailing commas or not, so it’s probably best to do this last.
Sourcepub fn clear_children(&self)
pub fn clear_children(&self)
Clears all the children from the root node making it empty.
Trait Implementations§
Source§impl Clone for CstRootNode
impl Clone for CstRootNode
Source§fn clone(&self) -> CstRootNode
fn clone(&self) -> CstRootNode
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for CstRootNode
impl Debug for CstRootNode
Source§impl Display for CstRootNode
impl Display for CstRootNode
Source§impl From<CstRootNode> for CstContainerNode
impl From<CstRootNode> for CstContainerNode
Source§fn from(value: CstRootNode) -> Self
fn from(value: CstRootNode) -> Self
Source§impl From<CstRootNode> for CstNode
impl From<CstRootNode> for CstNode
Source§fn from(value: CstRootNode) -> Self
fn from(value: CstRootNode) -> Self
Auto Trait Implementations§
impl Freeze for CstRootNode
impl !RefUnwindSafe for CstRootNode
impl !Send for CstRootNode
impl !Sync for CstRootNode
impl Unpin for CstRootNode
impl !UnwindSafe for CstRootNode
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)