Skip to main content

JsonAst

Struct JsonAst 

Source
pub struct JsonAst<'a> { /* private fields */ }
Expand description

A parsed JSON/JSONC document, used only to recover a named top-level section’s exact source positions — never a substitute for a caller’s own serde_json parse of the document’s semantic content.

§Examples

use deps_core::json_ast::JsonAst;

let content = r#"{"dependencies": {"express": "^4.18.2"}}"#;
let ast = JsonAst::parse(content).unwrap();
assert!(ast.section("dependencies").is_some());
assert!(ast.section("devDependencies").is_none());

Implementations§

Source§

impl<'a> JsonAst<'a>

Source

pub fn parse(content: &'a str) -> Option<Self>

Parses content. Returns None if it isn’t valid JSONC, or its root value isn’t an object — a caller that reached this via crate::parse_json_checked should not normally see None here (jsonc-parser’s grammar is a strict superset of JSON), but must still degrade gracefully (every dependency’s position falling back to the default, zero Range) rather than assume it.

Relies on the caller having already bounded content’s nesting depth (e.g. via crate::parse_json_checked/crate::check_json_nesting_depth) — jsonc-parser’s own internal recursion cap (512, hardcoded, not configurable) is a last resort, not this workspace’s first line of defense against a stack-overflowing payload.

§Examples
use deps_core::json_ast::JsonAst;

assert!(JsonAst::parse(r#"{"a": 1}"#).is_some());
assert!(JsonAst::parse("not json").is_none());
assert!(JsonAst::parse("[1, 2, 3]").is_none(), "root value must be an object");
Source

pub fn section(&self, key: &str) -> Option<JsonSection<'_>>

Indexes key‘s top-level section by its direct properties’ own names, for O(1) per-dependency position lookup instead of an O(section length) rescan per dependency. None if key is absent at the top level or its value isn’t an object. A duplicate key within the section resolves to its last occurrence too, matching key’s own last-key-wins resolution (see find_last_prop) — the same rule applied one level deeper.

§Examples
use deps_core::json_ast::JsonAst;
use deps_core::lsp_helpers::LineOffsetTable;

let content = r#"{"require": {"vendor/pkg": "^1.0"}}"#;
let table = LineOffsetTable::new(content);
let ast = JsonAst::parse(content).unwrap();
let section = ast.section("require").unwrap();

let (name_range, version_range) = section.position("vendor/pkg", content, &table).unwrap();
let name_start = content.find("vendor/pkg").unwrap() as u32;
assert_eq!(name_range.start.character, name_start);
assert!(version_range.is_some());

Auto Trait Implementations§

§

impl<'a> Freeze for JsonAst<'a>

§

impl<'a> RefUnwindSafe for JsonAst<'a>

§

impl<'a> Send for JsonAst<'a>

§

impl<'a> Sync for JsonAst<'a>

§

impl<'a> Unpin for JsonAst<'a>

§

impl<'a> UnsafeUnpin for JsonAst<'a>

§

impl<'a> UnwindSafe for JsonAst<'a>

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more