feat: display named source

This commit is contained in:
rvcas 2022-11-24 11:28:51 -05:00 committed by Lucas
parent 550f20c5e6
commit 09e77e1918
2 changed files with 37 additions and 8 deletions

View File

@ -5,7 +5,9 @@ use std::{
}; };
use aiken_lang::{ast::Span, parser::error::ParseError, tipo}; use aiken_lang::{ast::Span, parser::error::ParseError, tipo};
use miette::{Diagnostic, EyreContext, LabeledSpan, MietteHandlerOpts, RgbColors, SourceCode}; use miette::{
Diagnostic, EyreContext, LabeledSpan, MietteHandlerOpts, NamedSource, RgbColors, SourceCode,
};
#[allow(dead_code)] #[allow(dead_code)]
#[derive(thiserror::Error)] #[derive(thiserror::Error)]
@ -39,6 +41,8 @@ pub enum Error {
src: String, src: String,
named: NamedSource,
#[source] #[source]
error: Box<ParseError>, error: Box<ParseError>,
}, },
@ -47,6 +51,7 @@ pub enum Error {
Type { Type {
path: PathBuf, path: PathBuf,
src: String, src: String,
named: NamedSource,
#[source] #[source]
error: tipo::error::Error, error: tipo::error::Error,
}, },
@ -55,6 +60,7 @@ pub enum Error {
ValidatorMustReturnBool { ValidatorMustReturnBool {
path: PathBuf, path: PathBuf,
src: String, src: String,
named: NamedSource,
location: Span, location: Span,
}, },
@ -65,6 +71,7 @@ pub enum Error {
location: Span, location: Span,
path: PathBuf, path: PathBuf,
src: String, src: String,
named: NamedSource,
}, },
} }
@ -94,6 +101,7 @@ impl Error {
errors.push(Error::Parse { errors.push(Error::Parse {
path: path.into(), path: path.into(),
src: src.to_string(), src: src.to_string(),
named: NamedSource::new(path.display().to_string(), src.to_string()),
error: error.into(), error: error.into(),
}); });
} }
@ -245,22 +253,23 @@ impl Diagnostic for Error {
Error::FileIo { .. } => None, Error::FileIo { .. } => None,
Error::ImportCycle { .. } => None, Error::ImportCycle { .. } => None,
Error::List(_) => None, Error::List(_) => None,
Error::Parse { src, .. } => Some(src), Error::Parse { named, .. } => Some(named),
Error::Type { src, .. } => Some(src), Error::Type { named, .. } => Some(named),
Error::StandardIo(_) => None, Error::StandardIo(_) => None,
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { src, .. } => Some(src), Error::ValidatorMustReturnBool { named, .. } => Some(named),
Error::WrongValidatorArity { src, .. } => Some(src), Error::WrongValidatorArity { named, .. } => Some(named),
} }
} }
} }
#[derive(PartialEq, thiserror::Error)] #[derive(thiserror::Error)]
pub enum Warning { pub enum Warning {
#[error("type checking")] #[error("type checking")]
Type { Type {
path: PathBuf, path: PathBuf,
src: String, src: String,
named: NamedSource,
#[source] #[source]
warning: tipo::error::Warning, warning: tipo::error::Warning,
}, },
@ -273,7 +282,7 @@ impl Diagnostic for Warning {
fn source_code(&self) -> Option<&dyn SourceCode> { fn source_code(&self) -> Option<&dyn SourceCode> {
match self { match self {
Warning::Type { src, .. } => Some(src), Warning::Type { named, .. } => Some(named),
} }
} }
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> { fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
@ -291,7 +300,12 @@ impl Diagnostic for Warning {
impl Warning { impl Warning {
pub fn from_type_warning(warning: tipo::error::Warning, path: PathBuf, src: String) -> Warning { pub fn from_type_warning(warning: tipo::error::Warning, path: PathBuf, src: String) -> Warning {
Warning::Type { path, warning, src } Warning::Type {
path: path.clone(),
warning,
src: src.clone(),
named: NamedSource::new(path.display().to_string(), src),
}
} }
pub fn report(&self) { pub fn report(&self) {

View File

@ -17,6 +17,7 @@ use aiken_lang::{
uplc::{CodeGenerator, DataTypeKey, FunctionAccessKey}, uplc::{CodeGenerator, DataTypeKey, FunctionAccessKey},
IdGenerator, IdGenerator,
}; };
use miette::NamedSource;
use pallas::{ use pallas::{
codec::minicbor, codec::minicbor,
ledger::{addresses::Address, primitives::babbage}, ledger::{addresses::Address, primitives::babbage},
@ -156,6 +157,7 @@ impl Project {
errors.push(Error::Parse { errors.push(Error::Parse {
path: path.clone(), path: path.clone(),
src: code.clone(), src: code.clone(),
named: NamedSource::new(path.display().to_string(), code.clone()),
error: Box::new(error), error: Box::new(error),
}) })
} }
@ -201,6 +203,7 @@ impl Project {
.map_err(|error| Error::Type { .map_err(|error| Error::Type {
path: path.clone(), path: path.clone(),
src: code.clone(), src: code.clone(),
named: NamedSource::new(path.display().to_string(), code.clone()),
error, error,
})?; })?;
@ -251,6 +254,10 @@ impl Project {
location: func_def.location, location: func_def.location,
src: module.code.clone(), src: module.code.clone(),
path: module.input_path.clone(), path: module.input_path.clone(),
named: NamedSource::new(
module.input_path.display().to_string(),
module.code.clone(),
),
}) })
} }
@ -263,6 +270,10 @@ impl Project {
location: func_def.location, location: func_def.location,
src: module.code.clone(), src: module.code.clone(),
path: module.input_path.clone(), path: module.input_path.clone(),
named: NamedSource::new(
module.input_path.display().to_string(),
module.code.clone(),
),
name: func_def.name.clone(), name: func_def.name.clone(),
at_least: 2, at_least: 2,
}) })
@ -273,6 +284,10 @@ impl Project {
location: func_def.location, location: func_def.location,
src: module.code.clone(), src: module.code.clone(),
path: module.input_path.clone(), path: module.input_path.clone(),
named: NamedSource::new(
module.input_path.display().to_string(),
module.code.clone(),
),
name: func_def.name.clone(), name: func_def.name.clone(),
at_least: 3, at_least: 3,
}) })