Skip to content

Commit

Permalink
fix: my own mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenSelph authored and slbsh committed Aug 5, 2024
1 parent 1df513e commit d126ecf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn check_reports(receiver: &Receiver<Box<Report>>, reports: &mut Vec<Report>) ->
had_error
}

fn print_reports_and_exit(reports: &mut Vec<Report>, args: &args::Args) {
fn print_reports_and_exit(reports: &mut Vec<Report>, args: &args::Args) -> ! {
if *args.level == Level::Silent {
exit(1);
}
Expand All @@ -69,7 +69,8 @@ fn print_reports_and_exit(reports: &mut Vec<Report>, args: &args::Args) {
report.display(*args.code_context);
}
});
exit(1);

exit(1)
}

fn main() {
Expand All @@ -88,7 +89,6 @@ fn main() {
Scanner::get_file(*args.file),
ReportSender::new(sender.clone()),
);

lexer.lex_tokens();
lexer.tokens.goto_front();

Expand Down
2 changes: 1 addition & 1 deletion src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Display for ReportFormatter<'_> {
match report.label.as_ref() {
Some(label) => {
let span = &label.span;
let contents = crate::Scanner::get_file(span.filename);
let contents = crate::Scanner::get(span.filename);
let line_index = match contents[..=span.start_index].rfind('\n') {
Some(val) => val + 1,
None => 0,
Expand Down
39 changes: 13 additions & 26 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,57 @@ use crate::report::{ReportKind, ReportLabel, UnwrapReport};
pub struct Scanner {
filename: &'static str,
index: usize,
contents: String,
contents: &'static mut String,
reader: BufReader<File>,
}

static CACHE: LazyLock<RwLock<HashMap<&'static str, &'static str>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));

impl Scanner {
pub fn get_cached(filename: &'static str) -> Option<&str> {
let cache = CACHE.read().expect("failed to lock on cache");

cache.get(&filename).copied()
}

pub fn get_file(filename: &'static str) -> &str {
if let Some(contents) = Scanner::get_cached(filename) {
pub fn get(filename: &'static str) -> &'static str {
if let Some(contents) = CACHE.read().unwrap().get(&filename) {
return contents;
}

let contents = Scanner::new(filename)
let contents = Self::new(filename)
.unwrap_or_fatal(
ReportKind::IOError
.new(format!("Failed to open file: '{}'", filename))
.new(format!("Failed to open file: '{filename}'"))
.into(),
)
.read()
.unwrap_or_fatal(
ReportKind::IOError
.new(format!("Failed to read file: '{}'", filename))
.new(format!("Failed to read file: '{filename}'"))
.into(),
)
.leak();

let mut cache = CACHE.write().expect("failed to lock on cache");
.contents;

cache.insert(filename, contents);
CACHE.write().unwrap().insert(filename, contents);
contents
}

fn new(filename: &'static str) -> io::Result<Self> {
let file = File::open(filename)?;
let file_size = file.metadata()?.len() as usize;

let file_size = usize::try_from(file.metadata()?.len()).unwrap();

Ok(Self {
filename,
index: 0,
contents: String::with_capacity(file_size),
contents: Box::leak(String::with_capacity(file_size).into()),
reader: BufReader::new(file),
})
}

fn read(mut self) -> io::Result<Self> {
let mut buf = [0; 1];
let mut buf = [0u8; 1];

while self.reader.read(&mut buf)? > 0 {
match std::str::from_utf8(&buf) {
Ok(s) => match s {
"\r" => {
continue;
}
"\r" => continue,
_ => self.contents.push_str(s),
},
Err(_) => {
Expand Down Expand Up @@ -98,8 +89,4 @@ impl Scanner {

Ok(self)
}

fn leak(self) -> &'static str {
Box::leak(self.contents.into_boxed_str())
}
}

0 comments on commit d126ecf

Please sign in to comment.