docs: Add comprehensive documentation and doctests to Location struct
This commit is contained in:
parent
5d9f791353
commit
02684e3e98
1 changed files with 57 additions and 1 deletions
|
|
@ -1,12 +1,41 @@
|
||||||
/// Represents a location in the source input
|
/// Represents a location in source code for error reporting and debugging.
|
||||||
|
///
|
||||||
|
/// Stores information about where a token or error occurred in the source input,
|
||||||
|
/// including the file name, UTF-8 char offset position, and length of the relevant span.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use osdb::tokens::location::Location;
|
||||||
|
///
|
||||||
|
/// // Create a location for a token starting at UTF-8 char 10 spanning 5 UTF-8 chars
|
||||||
|
/// let loc = Location::new("main.rs".into(), 10, 5);
|
||||||
|
/// assert_eq!(loc.file, "main.rs");
|
||||||
|
/// assert_eq!(loc.offset, 10);
|
||||||
|
/// assert_eq!(loc.length, 5);
|
||||||
|
/// ```
|
||||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
pub struct Location {
|
pub struct Location {
|
||||||
|
/// The source file path where this location occurs
|
||||||
pub file: String,
|
pub file: String,
|
||||||
|
/// The starting UTF-8 char offset in the source file (0-based)
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
|
/// The number of UTF-8 chars this location spans
|
||||||
pub length: usize,
|
pub length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&Location> for std::ops::Range<usize> {
|
impl From<&Location> for std::ops::Range<usize> {
|
||||||
|
/// Converts a Location to a UTF-8 char range for slicing source input.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use osdb::tokens::location::Location;
|
||||||
|
/// let loc = Location::new("test.sql".into(), 5, 3);
|
||||||
|
/// let range: std::ops::Range<usize> = (&loc).into();
|
||||||
|
/// assert_eq!(range.start, 5);
|
||||||
|
/// assert_eq!(range.end, 8);
|
||||||
|
/// ```
|
||||||
fn from(val: &Location) -> Self {
|
fn from(val: &Location) -> Self {
|
||||||
std::ops::Range {
|
std::ops::Range {
|
||||||
start: val.offset,
|
start: val.offset,
|
||||||
|
|
@ -16,12 +45,39 @@ impl From<&Location> for std::ops::Range<usize> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Location {
|
impl Default for Location {
|
||||||
|
/// Creates a default Location with unknown file and zero positions.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use osdb::tokens::location::Location;
|
||||||
|
/// let loc = Location::default();
|
||||||
|
/// assert_eq!(loc.file, "<unknown>");
|
||||||
|
/// assert_eq!(loc.offset, 0);
|
||||||
|
/// assert_eq!(loc.length, 0);
|
||||||
|
/// ```
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(String::from("<unknown>"), 0, 0)
|
Self::new(String::from("<unknown>"), 0, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Location {
|
impl Location {
|
||||||
|
/// Constructs a new Location with specified parameters.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `file` - Source file path
|
||||||
|
/// * `offset` - Starting UTF-8 char position in the file
|
||||||
|
/// * `length` - Number of UTF-8 chars spanned by this location
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use osdb::tokens::location::Location;
|
||||||
|
/// let loc = Location::new("script.sql".into(), 20, 4);
|
||||||
|
/// assert_eq!(loc.offset, 20);
|
||||||
|
/// assert_eq!(loc.length, 4);
|
||||||
|
/// ```
|
||||||
pub fn new(file: String, offset: usize, length: usize) -> Self {
|
pub fn new(file: String, offset: usize, length: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
file,
|
file,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue