Struct BorrowedSourceMap

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

BorrowedSourceMap is a source map containing borrowed or owned strings.

For a source map that owns all its internal strings, see SourceMap. This struct can be converted into it using into_owned.

§Methods

§Parsing

You can create a BorrowedSourceMap using the following methods:

These methods take mutable references as parameters because they may modify the data in place.

The parsing supports index maps if feature index-map enabled, but sections will not be retained, and sub-maps will be flattened into a regular source map.

For SourceMap, there is a from method that consumes the ownership of input for parsing.

§Construction

When the builder feature is enabled, SourceMapBuilder is available to construct BorrowedSourceMap.

§Access & Modification

The structure provides several methods to access and modify internal data, such as:

Unsafe methods allow for more extensive modifications to the source map.

Note: After making changes, call validate to ensure that the source map remains valid and does not contain broken data.

§Finding Mappings

To find mappings corresponding to specific positions, you can use:

§Output

You can serialize the source map to json string using:

Implementations§

Source§

impl<'a> BorrowedSourceMap<'a>

Source

pub fn find_mapping<P>(&self, pos: P) -> Option<Mapping>
where P: Into<Position>,

Finds the mapping for a given generated position.

If an exact match is not found, this method returns the closest preceding mapping. If there are no preceding mappings, it returns None.

§Example
let source_map = BorrowedSourceMap::from_slice(&mut buf).unwrap();
if let Some(mapping) = source_map.find_mapping((1, 2)) {
    println!("Mapping found: {:?}", mapping);
} else {
    println!("No mapping found for the given generated position.");
}
Source

pub fn finder(&self) -> MappingFinder<'_>

Creates a MappingFinder for the source map.

This stateful finder is highly efficient for frequent mapping findings, especially when traversing the source map in small increments (e.g., sequentially finding mappings from start to finish in a source map for a minified file).

§Example
let source_map = BorrowedSourceMap::from_slice(&mut buf).unwrap();
let finder = source_map.finder();
finder.find_mapping((1, 2));
finder.find_mapping((1, 6));
Source

pub fn validate(&self) -> ValidateResult<()>

Validates the source map.

Source§

impl<'a> BorrowedSourceMap<'a>

Source

pub fn file(&self) -> &Option<Cow<'a, str>>

Source

pub fn file_mut(&mut self) -> &mut Option<Cow<'a, str>>

Source

pub fn mappings(&self) -> &Mappings

Source

pub fn mappings_mut(&mut self) -> &mut Mappings

Source

pub fn name_at(&self, id: u32) -> Option<&str>

Source

pub fn names(&self) -> &[Cow<'a, str>]

Source

pub fn names_mut(&mut self) -> &mut [Cow<'a, str>]

Source

pub fn source_at(&self, id: u32) -> Option<&str>

Source

pub fn sources(&self) -> &[Option<Cow<'a, str>>]

Source

pub fn sources_mut(&mut self) -> &mut [Option<Cow<'a, str>>]

Source

pub fn source_content_at(&self, id: u32) -> Option<&str>

Source

pub fn sources_content(&self) -> &[Option<Cow<'a, str>>]

Source

pub fn sources_content_mut(&mut self) -> &mut [Option<Cow<'a, str>>]

Source

pub fn ignore_list(&self) -> &[u32]

Source

pub fn ignore_list_mut(&mut self) -> &mut Vec<u32>

This function directly returns &mut Vec and is not marked as unsafe because modifications to ignore_list will not break the primary functionality of source maps.

Source§

impl<'a> BorrowedSourceMap<'a>

Source

pub unsafe fn names_mut2(&mut self) -> &mut Vec<Cow<'a, str>>

Returns a mutable reference to the names.

§Safety

This function allows direct mutable access to the internal data structure. The caller must ensure that the name id referenced in the mappings cannot be greater than the length of the names.

It’s best to call Self::validate after making modifications.

Source

pub unsafe fn sources_mut2(&mut self) -> &mut Vec<Option<Cow<'a, str>>>

Returns a mutable reference to the sources.

§Safety

This function allows direct mutable access to the internal data structure. The caller must ensure that the source id referenced in the mappings cannot be greater than the length of the sources.

It’s best to call Self::validate after making modifications.

Source

pub unsafe fn sources_content_mut2(&mut self) -> &mut Vec<Option<Cow<'a, str>>>

Returns a mutable reference to the sources’ content.

§Safety

This function allows direct mutable access to the internal data structure. The caller must ensure that the length of the sources_content matches the length of the sources.

It’s best to call Self::validate after making modifications.

Source§

impl<'a> BorrowedSourceMap<'a>

Source

pub fn from_slice(json: &'a mut [u8]) -> ParseResult<Self>

Creates a new BorrowedSourceMap from a JSON buffer slice.

The slice is mutable to facilitate in-place replacement of escape characters in the JSON string, allowing maximum data borrowing.

Source

pub fn from_str(json: &'a mut str) -> ParseResult<Self>

Creates a new BorrowedSourceMap from a JSON string.

The string is mutable to facilitate in-place replacement of escape characters in the JSON string, allowing maximum data borrowing.

Source§

impl BorrowedSourceMap<'_>

Source

pub fn write<W>(&self, w: &mut W) -> Result<()>
where W: Write,

Source

pub fn to_vec(&self) -> Result<Vec<u8>>

Source

pub fn to_string(&self) -> Result<String>

Source§

impl BorrowedSourceMap<'static>

Source

pub fn from(source: Vec<u8>) -> ParseResult<Self>

Creates a new owned SourceMap from a JSON buffer.

Source§

impl BorrowedSourceMap<'_>

Source

pub fn into_owned(self) -> SourceMap

Convert a BorrowedSourceMap into a SourceMap that owns all its internal strings.

Source§

impl<'a> BorrowedSourceMap<'a>

Trait Implementations§

Source§

impl<'a> Clone for BorrowedSourceMap<'a>

Source§

fn clone(&self) -> BorrowedSourceMap<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BorrowedSourceMap<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for BorrowedSourceMap<'a>

§

impl<'a> RefUnwindSafe for BorrowedSourceMap<'a>

§

impl<'a> Send for BorrowedSourceMap<'a>

§

impl<'a> Sync for BorrowedSourceMap<'a>

§

impl<'a> Unpin for BorrowedSourceMap<'a>

§

impl<'a> UnwindSafe for BorrowedSourceMap<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.