use alloc::boxed::Box;
use span_lang::Span;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SourceFile {
name: Box<str>,
text: Box<str>,
span: Span,
}
impl SourceFile {
#[inline]
pub(crate) const fn new(name: Box<str>, text: Box<str>, span: Span) -> Self {
Self { name, text, span }
}
#[inline]
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[inline]
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[inline]
#[must_use]
pub const fn span(&self) -> Span {
self.span
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::boxed::Box;
use super::*;
fn boxed(s: &str) -> Box<str> {
Box::from(s)
}
#[test]
fn test_accessors_return_stored_values() {
let file = SourceFile::new(boxed("name"), boxed("body"), Span::new(4, 8));
assert_eq!(file.name(), "name");
assert_eq!(file.text(), "body");
assert_eq!(file.span(), Span::new(4, 8));
}
#[test]
fn test_empty_text_has_zero_width_span() {
let file = SourceFile::new(boxed("empty"), boxed(""), Span::empty(12));
assert_eq!(file.text(), "");
assert!(file.span().is_empty());
}
}