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:
source_atsourcessources_mut- unsafe
sources_mut2
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>
impl<'a> BorrowedSourceMap<'a>
Sourcepub fn find_mapping<P>(&self, pos: P) -> Option<Mapping>
pub fn find_mapping<P>(&self, pos: P) -> Option<Mapping>
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.");
}Sourcepub fn finder(&self) -> MappingFinder<'_>
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));Sourcepub fn validate(&self) -> ValidateResult<()>
pub fn validate(&self) -> ValidateResult<()>
Validates the source map.
Source§impl<'a> BorrowedSourceMap<'a>
impl<'a> BorrowedSourceMap<'a>
pub fn file(&self) -> &Option<Cow<'a, str>>
pub fn file_mut(&mut self) -> &mut Option<Cow<'a, str>>
pub fn mappings(&self) -> &Mappings
pub fn mappings_mut(&mut self) -> &mut Mappings
pub fn name_at(&self, id: u32) -> Option<&str>
pub fn names(&self) -> &[Cow<'a, str>]
pub fn names_mut(&mut self) -> &mut [Cow<'a, str>]
pub fn source_at(&self, id: u32) -> Option<&str>
pub fn sources(&self) -> &[Option<Cow<'a, str>>]
pub fn sources_mut(&mut self) -> &mut [Option<Cow<'a, str>>]
pub fn source_content_at(&self, id: u32) -> Option<&str>
pub fn sources_content(&self) -> &[Option<Cow<'a, str>>]
pub fn sources_content_mut(&mut self) -> &mut [Option<Cow<'a, str>>]
pub fn ignore_list(&self) -> &[u32]
Sourcepub fn ignore_list_mut(&mut self) -> &mut Vec<u32>
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>
impl<'a> BorrowedSourceMap<'a>
Sourcepub unsafe fn names_mut2(&mut self) -> &mut Vec<Cow<'a, str>>
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.
Sourcepub unsafe fn sources_mut2(&mut self) -> &mut Vec<Option<Cow<'a, str>>>
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.
Sourcepub unsafe fn sources_content_mut2(&mut self) -> &mut Vec<Option<Cow<'a, str>>>
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>
impl<'a> BorrowedSourceMap<'a>
Sourcepub fn from_slice(json: &'a mut [u8]) -> ParseResult<Self>
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.
Sourcepub fn from_str(json: &'a mut str) -> ParseResult<Self>
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<'_>
impl BorrowedSourceMap<'_>
Source§impl BorrowedSourceMap<'static>
impl BorrowedSourceMap<'static>
Source§impl BorrowedSourceMap<'_>
impl BorrowedSourceMap<'_>
Sourcepub fn into_owned(self) -> SourceMap
pub fn into_owned(self) -> SourceMap
Convert a BorrowedSourceMap into a SourceMap that owns all its internal strings.
Source§impl<'a> BorrowedSourceMap<'a>
impl<'a> BorrowedSourceMap<'a>
pub fn builder() -> SourceMapBuilder<'a>
Trait Implementations§
Source§impl<'a> Clone for BorrowedSourceMap<'a>
impl<'a> Clone for BorrowedSourceMap<'a>
Source§fn clone(&self) -> BorrowedSourceMap<'a>
fn clone(&self) -> BorrowedSourceMap<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more