Struct js_source_scopes::SourceContext
source · pub struct SourceContext<T> { /* private fields */ }
Expand description
A structure allowing fast line/column <-> byte offset remapping.
The primary use-case is to allow efficient conversion between
SourcePosition
s (line/column) to byte offsets. The SourcePosition
s
are 0-based. All offsets are treated as u32
, and creating a
SourceContext
for a source that exceeds the range of a u32
will result
in an Err
.
Examples
use js_source_scopes::{SourceContext, SourcePosition};
let src = r#"const arrowFnExpr = (a) => a;
function namedFnDecl() {}"#;
let ctx = SourceContext::new(src).unwrap();
let offset = ctx.position_to_offset(SourcePosition::new(0, 6)).unwrap() as usize;
assert_eq!(&src[offset..offset+11], "arrowFnExpr");
let offset = ctx.position_to_offset(SourcePosition::new(1, 9)).unwrap() as usize;
assert_eq!(&src[offset..offset+11], "namedFnDecl");
Implementations§
source§impl<T: AsRef<str>> SourceContext<T>
impl<T: AsRef<str>> SourceContext<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap this Source Context into the inner source buffer.
sourcepub fn new(src: T) -> Result<Self, SourceContextError>
pub fn new(src: T) -> Result<Self, SourceContextError>
Construct a new Source Context from the given src
buffer.
sourcepub fn offset_to_position(&self, offset: u32) -> Option<SourcePosition>
pub fn offset_to_position(&self, offset: u32) -> Option<SourcePosition>
Converts a byte offset into the source to the corresponding line/column.
The column is given in UTF-16 code points.
sourcepub fn position_to_offset(&self, position: SourcePosition) -> Option<u32>
pub fn position_to_offset(&self, position: SourcePosition) -> Option<u32>
Converts the given line/column to the corresponding byte offset inside the source.
Auto Trait Implementations§
impl<T> RefUnwindSafe for SourceContext<T>where T: RefUnwindSafe,
impl<T> Send for SourceContext<T>where T: Send,
impl<T> Sync for SourceContext<T>where T: Sync,
impl<T> Unpin for SourceContext<T>where T: Unpin,
impl<T> UnwindSafe for SourceContext<T>where T: UnwindSafe,
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