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 SourcePositions (line/column) to byte offsets. The SourcePositions 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

Unwrap this Source Context into the inner source buffer.

Construct a new Source Context from the given src buffer.

Converts a byte offset into the source to the corresponding line/column.

The column is given in UTF-16 code points.

Converts the given line/column to the corresponding byte offset inside the source.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Unerase this erased pointer. Read more
Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
Turn this erasable pointer into an erased pointer. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more