From 44e5e9c5f1ed26e7f136e70b9a956d9b2c1efd9a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 7 Aug 2023 16:46:26 +0200 Subject: [PATCH] Add hint for "access denied" message --- crates/typst-library/src/meta/bibliography.rs | 2 +- crates/typst-library/src/text/raw.rs | 2 +- crates/typst/src/diag.rs | 20 ++++++++++++++++--- crates/typst/src/eval/mod.rs | 8 ++------ crates/typst/src/model/mod.rs | 4 +--- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/crates/typst-library/src/meta/bibliography.rs b/crates/typst-library/src/meta/bibliography.rs index 2b00ff445..69cf5d5e4 100644 --- a/crates/typst-library/src/meta/bibliography.rs +++ b/crates/typst-library/src/meta/bibliography.rs @@ -606,7 +606,7 @@ fn load(paths: &BibPaths, data: &[Bytes]) -> StrResult> // We might have multiple bib/yaml files for (path, bytes) in paths.0.iter().zip(data) { - let src = std::str::from_utf8(bytes).map_err(|_| FileError::InvalidUtf8)?; + let src = std::str::from_utf8(bytes).map_err(FileError::from)?; let entries = parse_bib(path, src)?; result.extend(entries); } diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs index b0abd369e..c1c4aed6c 100644 --- a/crates/typst-library/src/text/raw.rs +++ b/crates/typst-library/src/text/raw.rs @@ -440,7 +440,7 @@ fn load_syntaxes(paths: &SyntaxPaths, bytes: &[Bytes]) -> StrResult Self { - self.hints.push(hint); + pub fn hint(&mut self, hint: impl Into) { + self.hints.push(hint.into()); + } + + /// Adds a single hint to the diagnostic. + pub fn with_hint(mut self, hint: impl Into) -> Self { + self.hint(hint); self } @@ -238,7 +243,15 @@ where S: Into, { fn at(self, span: Span) -> SourceResult { - self.map_err(|message| Box::new(vec![SourceDiagnostic::error(span, message)])) + self.map_err(|message| { + let mut diagnostic = SourceDiagnostic::error(span, message); + if diagnostic.message.contains("(access denied)") { + diagnostic.hint("cannot read file outside of project root"); + diagnostic + .hint("you can adjust the project root with the --root argument"); + } + Box::new(vec![diagnostic]) + }) } } @@ -408,6 +421,7 @@ impl From for EcoString { eco_format!("{error}") } } + /// Format a user-facing error message for an XML-like file format. pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> EcoString { match error { diff --git a/crates/typst/src/eval/mod.rs b/crates/typst/src/eval/mod.rs index d79f2ea5f..d1f248c87 100644 --- a/crates/typst/src/eval/mod.rs +++ b/crates/typst/src/eval/mod.rs @@ -585,10 +585,8 @@ impl Eval for ast::Strong { vm.vt .tracer .warn(warning!(self.span(), "no text within stars").with_hint( - EcoString::from( "using multiple consecutive stars (e.g. **) has no additional effect", - ), - )); + )); } Ok((vm.items.strong)(body.eval(vm)?)) @@ -605,9 +603,7 @@ impl Eval for ast::Emph { vm.vt .tracer .warn(warning!(self.span(), "no text within underscores").with_hint( - EcoString::from( - "using multiple consecutive underscores (e.g. __) has no additional effect", - ), + "using multiple consecutive underscores (e.g. __) has no additional effect" )); } diff --git a/crates/typst/src/model/mod.rs b/crates/typst/src/model/mod.rs index d839422a9..69dc6a6bf 100644 --- a/crates/typst/src/model/mod.rs +++ b/crates/typst/src/model/mod.rs @@ -92,9 +92,7 @@ pub fn typeset( world.main().root().span(), "layout did not converge within 5 attempts", ) - .with_hint( - "check if any states or queries are updating themselves".into(), - ), + .with_hint("check if any states or queries are updating themselves"), ); break; }