This commit is contained in:
Qyriad 2026-01-27 11:29:10 +01:00
parent d8ac4e157d
commit bcd11513ef
12 changed files with 1042 additions and 5 deletions

59
src/source.rs Normal file
View file

@ -0,0 +1,59 @@
use std::{
hash::{Hash, Hasher},
sync::{Arc, LazyLock},
};
use crate::Line;
#[allow(unused_imports)]
use crate::prelude::*;
use crate::color::{ANSI_CYAN, ANSI_RESET};
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SourceLine {
pub line: Line,
pub path: Arc<Path>,
pub text: Arc<str>,
}
impl Display for SourceLine {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "line {:03}: `{ANSI_CYAN}{}{ANSI_RESET}`", self.line.linenr(), self.text.trim())
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SourcePath {
path: Arc<Path>,
}
#[derive(Debug, Clone)]
pub struct SourceFile {
path: Arc<Path>,
file: Arc<File>,
}
impl PartialEq for SourceFile {
fn eq(&self, other: &Self) -> bool {
let paths_match = *self.path == *other.path;
if paths_match && self.file.as_raw_fd() != other.file.as_raw_fd() {
todo!("handle the case where source file paths match but FD numbers don't");
}
paths_match
}
}
impl Hash for SourceFile {
fn hash<H: Hasher>(&self, state: &mut H) {
self.path.hash(state);
self.file.as_raw_fd().hash(state);
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
enum SourceInner {
Path(SourcePath),
File(SourceFile),
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Source(SourceInner);