Skip to content

Commit

Permalink
Merge pull request #21 from Cakefish/better-errors
Browse files Browse the repository at this point in the history
Better error reporting
  • Loading branch information
IsseW authored Mar 5, 2024
2 parents 21c32bf + 017f2d2 commit e70b308
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions bauble/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,18 @@ pub fn convert_values<C: AssetContext>(
source: &str,
) -> Result<Vec<Object>> {
let mut use_symbols = Symbols::new(&default_symbols.ctx);
let mut use_errors = Vec::new();
for use_ in values.uses {
use_symbols.add_use(&use_)?;
if let Err(e) = use_symbols.add_use(&use_) {
use_errors.push(e);
}
}

let mut symbols = default_symbols.clone();

for (symbol, _) in &values.values {
let path = format!("{path}::{symbol}");
symbols
if let Err(e) = symbols
.add_ref(
symbol.value.clone(),
Reference::Specific {
Expand All @@ -449,7 +452,10 @@ pub fn convert_values<C: AssetContext>(
module: None,
},
)
.map_err(|e| e.spanned(symbol.span))?;
.map_err(|e| e.spanned(symbol.span))
{
use_errors.push(e);
}
}
symbols.add(use_symbols);

Expand Down Expand Up @@ -478,14 +484,19 @@ pub fn convert_values<C: AssetContext>(
let mut objects = Vec::new();
let mut auto_value_idx = 0;

let mut add_value = |val: Val| {
let mut add_value = |mut val: Val| {
let name = format!("@{auto_value_idx:x}");
let span = val.value.span;

// TODO: Possibly list possible attributes for a type info, and chose which to use for which value.
let attributes =
std::mem::replace(&mut val.attributes, Attributes::default().spanned(span));

objects.push(create_object(&path, &name, val));
auto_value_idx += 1;
Val {
value: Value::Ref(format!("{path}::{name}")).spanned(span),
attributes: Attributes::default().spanned(span),
attributes,
}
};

Expand All @@ -508,21 +519,29 @@ pub fn convert_values<C: AssetContext>(
{
use ariadne::{Color, Label, Report, ReportKind, Source};

for res in parsed_objects.iter() {
if let Err(e) = res {
Report::build(ReportKind::Error, (), e.span.start)
.with_message(e.to_string())
.with_label(
Label::new(e.span.into_range())
.with_message("Here")
.with_color(Color::Red),
)
.finish()
.eprint(Source::from(source))
.unwrap()
}
for e in parsed_objects
.iter()
.filter_map(|res| match res {
Err(e) => Some(e),
Ok(_) => None,
})
.chain(use_errors.iter())
{
Report::build(ReportKind::Error, (), e.span.start)
.with_message(e.to_string())
.with_label(
Label::new(e.span.into_range())
.with_message("Here")
.with_color(Color::Red),
)
.finish()
.eprint(Source::from(source))
.unwrap()
}
}
if let Some(e) = use_errors.into_iter().next() {
return Err(e);
}
objects.extend(parsed_objects.into_iter().try_collect::<Vec<_>>()?);
Ok(objects)
}
Expand Down

0 comments on commit e70b308

Please sign in to comment.