Integrate codespan-reporting

This commit is contained in:
Joscha 2022-01-02 11:47:22 +01:00
parent d8617ede24
commit d9c1dc78e4
3 changed files with 63 additions and 11 deletions

30
Cargo.lock generated
View file

@ -101,6 +101,16 @@ dependencies = [
"vec_map", "vec_map",
] ]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]] [[package]]
name = "colored" name = "colored"
version = "2.0.0" version = "2.0.0"
@ -391,6 +401,15 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -436,12 +455,14 @@ name = "today"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"codespan-reporting",
"colored", "colored",
"computus", "computus",
"directories", "directories",
"pest", "pest",
"pest_derive", "pest_derive",
"structopt", "structopt",
"termcolor",
"thiserror", "thiserror",
"tzfile", "tzfile",
] ]
@ -519,6 +540,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View file

@ -5,11 +5,13 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.19" chrono = "0.4.19"
codespan-reporting = "0.11.1"
colored = "2.0.0" colored = "2.0.0"
computus = "1.0.0" computus = "1.0.0"
directories = "4.0.1" directories = "4.0.1"
pest = "2.1.3" pest = "2.1.3"
pest_derive = "2.1.0" pest_derive = "2.1.0"
structopt = "0.3.25" structopt = "0.3.25"
termcolor = "1.1.2"
thiserror = "1.0.30" thiserror = "1.0.30"
tzfile = { git = "https://github.com/Garmelon/tzfile.git", branch = "tzdir" } tzfile = { git = "https://github.com/Garmelon/tzfile.git", branch = "tzdir" }

View file

@ -4,6 +4,10 @@ use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use chrono::{DateTime, NaiveDate, Utc}; use chrono::{DateTime, NaiveDate, Utc};
use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::{self, Config};
use termcolor::StandardStream;
use tzfile::Tz; use tzfile::Tz;
use self::commands::{Command, Done, File}; use self::commands::{Command, Done, File};
@ -16,22 +20,26 @@ mod format;
mod parse; mod parse;
pub mod primitives; pub mod primitives;
// TODO Move file content from `File` to `LoadedFile`
#[derive(Debug)] #[derive(Debug)]
struct LoadedFile { struct LoadedFile {
/// Canonical path for this file /// Canonical path for this file.
path: PathBuf, path: PathBuf,
// User-readable path for this file /// User-readable path for this file.
name: PathBuf, name: PathBuf,
/// Identifier for codespan-reporting.
cs_id: usize,
file: File, file: File,
/// Whether this file has been changed /// Whether this file has been changed.
dirty: bool, dirty: bool,
} }
impl LoadedFile { impl LoadedFile {
pub fn new(path: PathBuf, name: PathBuf, file: File) -> Self { pub fn new(path: PathBuf, name: PathBuf, cs_id: usize, file: File) -> Self {
Self { Self {
path, path,
name, name,
cs_id,
file, file,
dirty: false, dirty: false,
} }
@ -48,10 +56,6 @@ impl Source {
pub fn new(file: usize, command: usize) -> Self { pub fn new(file: usize, command: usize) -> Self {
Self { file, command } Self { file, command }
} }
pub fn file(&self) -> usize {
self.file
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -63,6 +67,8 @@ pub struct SourcedCommand<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct Files { pub struct Files {
files: Vec<LoadedFile>, files: Vec<LoadedFile>,
/// Codespan-reporting file database.
cs_files: SimpleFiles<String, String>,
timezone: Tz, timezone: Tz,
logs: HashMap<NaiveDate, Source>, logs: HashMap<NaiveDate, Source>,
} }
@ -75,12 +81,14 @@ impl Files {
let mut loaded = HashSet::new(); let mut loaded = HashSet::new();
let mut files = vec![]; let mut files = vec![];
Self::load_file(&mut loaded, &mut files, path)?; let mut cs_files = SimpleFiles::new();
Self::load_file(&mut loaded, &mut files, &mut cs_files, path)?;
let timezone = Self::determine_timezone(&files)?; let timezone = Self::determine_timezone(&files)?;
let logs = Self::collect_logs(&files)?; let logs = Self::collect_logs(&files)?;
Ok(Self { Ok(Self {
files, files,
cs_files,
timezone, timezone,
logs, logs,
}) })
@ -89,6 +97,7 @@ impl Files {
fn load_file( fn load_file(
loaded: &mut HashSet<PathBuf>, loaded: &mut HashSet<PathBuf>,
files: &mut Vec<LoadedFile>, files: &mut Vec<LoadedFile>,
cs_files: &mut SimpleFiles<String, String>,
name: &Path, name: &Path,
) -> Result<()> { ) -> Result<()> {
let path = name.canonicalize().map_err(|e| Error::ResolvePath { let path = name.canonicalize().map_err(|e| Error::ResolvePath {
@ -118,13 +127,14 @@ impl Files {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
loaded.insert(path.clone()); loaded.insert(path.clone());
files.push(LoadedFile::new(path, name.to_owned(), file)); let cs_id = cs_files.add(path.to_string_lossy().to_string(), content);
files.push(LoadedFile::new(path, name.to_owned(), cs_id, file));
for include in includes { for include in includes {
// Since we've successfully opened the file, its name can't be the // Since we've successfully opened the file, its name can't be the
// root directory or empty string and it must thus have a parent. // root directory or empty string and it must thus have a parent.
let include_path = name.parent().unwrap().join(include); let include_path = name.parent().unwrap().join(include);
Self::load_file(loaded, files, &include_path)?; Self::load_file(loaded, files, cs_files, &include_path)?;
} }
Ok(()) Ok(())
@ -253,4 +263,14 @@ impl Files {
true true
} }
*/ */
/* Errors */
pub fn eprint_diagnostic(&self, diagnostic: &Diagnostic<usize>) {
let mut out = StandardStream::stderr(termcolor::ColorChoice::Auto);
let config = Config::default();
if let Err(e) = term::emit(&mut out, &config, &self.cs_files, diagnostic) {
panic!("Error while reporting error: {}", e);
}
}
} }