Skip to content

Commit

Permalink
feat(offsets): nice utility function to get an offset from a Rust cal…
Browse files Browse the repository at this point in the history
…lsite

Fixes: #19
  • Loading branch information
zkat committed Aug 18, 2021
1 parent 75c2312 commit 26f409c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ that you can implement to get access to miette's (and related library's) full
reporting and such features.
*/

use std::fmt::Display;
use std::{fmt::Display, fs, panic::Location};

use crate::MietteError;

Expand Down Expand Up @@ -313,6 +313,27 @@ impl SourceOffset {

SourceOffset(offset)
}

/// Returns an offset for the _file_ location of wherever this function is
/// called. If you want to get _that_ caller's location, mark this
/// function's caller with #[track_caller] (and so on and so forth).
///
/// Returns both the filename that was given and the offset of the caller
/// as a SourceOffset
///
/// Keep in mind that this fill only work if the file your Rust source
/// file was compiled from is actually available at that location. If
/// you're shipping binaries for your application, you'll want to ignore
/// the Err case or otherwise report it.
#[track_caller]
pub fn from_current_location() -> Result<(String, Self), MietteError> {
let loc = Location::caller();
Ok((
loc.file().into(),
fs::read_to_string(loc.file())
.map(|txt| Self::from_location(&txt, loc.line() as usize, loc.column() as usize))?,
))
}
}

impl From<ByteOffset> for SourceOffset {
Expand Down

0 comments on commit 26f409c

Please sign in to comment.