Skip to main content

Span

Struct Span 

Source
pub struct Span {
    pub span_id: String,
    pub file_path: String,
    pub byte_start: usize,
    pub byte_end: usize,
    pub start_line: usize,
    pub start_col: usize,
    pub end_line: usize,
    pub end_col: usize,
    pub context: Option<SpanContext>,
    pub semantics: Option<SpanSemantics>,
    pub relationships: Option<SpanRelationships>,
    pub checksums: Option<SpanChecksums>,
}
Expand description

Span in source code (byte + line/column)

Represents a half-open range [start, end) where:

  • byte_start is inclusive (first byte INCLUDED)
  • byte_end is exclusive (first byte NOT included)

All offsets are UTF-8 byte positions. Lines are 1-indexed for user-friendliness. Columns are 0-indexed byte offsets within each line.

§Examples

Creating a span and extracting text:

use magellan::output::command::Span;

let source = "fn main() { println!(\"Hello\"); }";
let span = Span::new(
    "main.rs".into(),  // file_path
    3,   // byte_start (points to 'm')
    7,   // byte_end (points to '(')
    1,   // start_line (1-indexed)
    3,   // start_col (byte offset in line)
    1,   // end_line
    7,   // end_col
);

// Extract text using the span
let text = source.get(span.byte_start..span.byte_end).unwrap();
assert_eq!(text, "main");

§Safety

Always use .get() for UTF-8 safe slicing:

// SAFE: Returns Option<&str>, None if out of bounds
let text = source.get(span.byte_start..span.byte_end);

// UNSAFE: Can panic on invalid UTF-8 boundaries
// let text = &source[span.byte_start..span.byte_end];

§Serialization

Span implements Serialize and Deserialize for JSON output. All fields are public and included in serialization.

Fields§

§span_id: String

Stable span ID (SHA-256 hash of file_path:byte_start:byte_end)

This ID is deterministic and platform-independent. See Span::generate_id for the algorithm details.

§file_path: String

File path (absolute or root-relative)

Use consistent paths for stable IDs. The path is included in the span ID hash, so different representations of the same file (e.g., ./main.rs vs main.rs) produce different IDs.

§byte_start: usize

Byte range start (inclusive, first byte INCLUDED)

UTF-8 byte offset from the start of the file.

§byte_end: usize

Byte range end (exclusive, first byte NOT included)

UTF-8 byte offset. The span covers [byte_start, byte_end). Length is byte_end - byte_start.

§start_line: usize

Start line (1-indexed)

Line number where the span starts, counting from 1. Matches editor line numbers.

§start_col: usize

Start column (0-indexed, byte-based)

Byte offset within start_line where the span begins. This is a byte offset, not a character offset.

§end_line: usize

End line (1-indexed)

Line number where the span ends.

§end_col: usize

End column (0-indexed, byte-based)

Byte offset within end_line where the span ends (exclusive).

§context: Option<SpanContext>

Context lines around the span

§semantics: Option<SpanSemantics>

Semantic information (kind, language) - grouped in a single struct

§relationships: Option<SpanRelationships>

Relationship information (callers, callees, imports, exports)

§checksums: Option<SpanChecksums>

Checksums for content verification

Implementations§

Source§

impl Span

Source

pub fn generate_id( file_path: &str, byte_start: usize, byte_end: usize, ) -> String

Generate a stable span ID from (file_path, byte_start, byte_end)

Uses SHA-256 for platform-independent, deterministic span IDs.

§Algorithm

The hash is computed from: file_path + ":" + byte_start + ":" + byte_end The first 8 bytes (64 bits) of the hash are formatted as 16 hex characters.

§Properties

This ensures span IDs are:

  • Deterministic: same inputs always produce the same ID
  • Platform-independent: SHA-256 produces consistent results across architectures
  • Collision-resistant: 64-bit space with good distribution
§Stability

The span ID format is part of Magellan’s stable API contract. IDs generated by this function will remain consistent across versions.

§Examples
use magellan::output::command::Span;

let id1 = Span::generate_id("main.rs", 10, 20);
let id2 = Span::generate_id("main.rs", 10, 20);
let id3 = Span::generate_id("main.rs", 10, 21);

assert_eq!(id1, id2);  // Same inputs = same ID
assert_ne!(id1, id3);  // Different inputs = different ID
assert_eq!(id1.len(), 16);  // Always 16 hex characters
Source

pub fn new( file_path: String, byte_start: usize, byte_end: usize, start_line: usize, start_col: usize, end_line: usize, end_col: usize, ) -> Self

Create a new Span from component parts

Constructs a Span with a stable span_id automatically generated using Span::generate_id.

§Parameters
  • file_path: Path to the source file (absolute or root-relative)
  • byte_start: UTF-8 byte offset where the span starts (inclusive)
  • byte_end: UTF-8 byte offset where the span ends (exclusive)
  • start_line: Line number where the span starts (1-indexed)
  • start_col: Byte offset within start_line where the span starts (0-indexed)
  • end_line: Line number where the span ends (1-indexed)
  • end_col: Byte offset within end_line where the span ends (0-indexed, exclusive)
§Half-Open Convention

The span uses half-open range semantics [byte_start, byte_end):

  • byte_start is inclusive (first byte included)
  • byte_end is exclusive (first byte NOT included)
§Examples
use magellan::output::command::Span;

let span = Span::new(
    "main.rs".into(),  // file_path
    3,   // byte_start (inclusive)
    7,   // byte_end (exclusive)
    1,   // start_line (1-indexed)
    3,   // start_col (byte offset, 0-indexed)
    1,   // end_line
    7,   // end_col (byte offset, 0-indexed)
);

assert_eq!(span.byte_end - span.byte_start, 4);  // Length
assert_eq!(span.span_id.len(), 16);  // Stable ID
Source

pub fn with_context(self, context: SpanContext) -> Self

Set context on the span

Source

pub fn with_semantics(self, semantics: SpanSemantics) -> Self

Set semantic information on the span

Source

pub fn with_semantics_from(self, kind: String, language: String) -> Self

Set semantic information from kind and language strings

Source

pub fn with_relationships(self, relationships: SpanRelationships) -> Self

Set relationships on the span

Source

pub fn with_checksums(self, checksums: SpanChecksums) -> Self

Set checksums on the span

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Span

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Span

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Span

Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Span) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Span

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Span

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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