use crate::types::report::Location;
use crate::types::tokens::parsed::Token;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocatedToken {
token: Token,
location: Location,
}
impl LocatedToken {
pub(crate) const fn new(token: Token, location: Location) -> Self {
Self { token, location }
}
#[must_use]
pub const fn token(&self) -> &Token {
&self.token
}
#[must_use]
pub const fn location(&self) -> &Location {
&self.location
}
}
#[cfg(test)]
mod tests {
use std::num::NonZeroUsize;
use std::path::PathBuf;
use crate::types::{TextOrigin, TokenKind, scan};
#[test]
fn located_token_exposes_token_and_location() {
let origin = TextOrigin::Lines {
file: PathBuf::from("notes.md"),
first_line: NonZeroUsize::new(3).unwrap_or(NonZeroUsize::MIN),
};
let (tokens, diagnostics) = scan("{{skill status}}", &origin);
assert!(diagnostics.is_empty());
assert_eq!(tokens.len(), 1);
assert_eq!(
tokens.first().map(|t| t.token().kind()),
Some(TokenKind::Skill)
);
assert_eq!(
tokens.first().map(|t| t.location().to_string()),
Some("notes.md:3".to_owned())
);
}
}