diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-08-07 16:46:26 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-08-07 16:46:33 +0200 |
| commit | 44e5e9c5f1ed26e7f136e70b9a956d9b2c1efd9a (patch) | |
| tree | 93213cc35f5968f9b7ce89e666e7ae428a1c0e40 | |
| parent | b61eee4306f5d67c3f48874c45b7c846b31d9e21 (diff) | |
Add hint for "access denied" message
| -rw-r--r-- | crates/typst-library/src/meta/bibliography.rs | 2 | ||||
| -rw-r--r-- | crates/typst-library/src/text/raw.rs | 2 | ||||
| -rw-r--r-- | crates/typst/src/diag.rs | 20 | ||||
| -rw-r--r-- | crates/typst/src/eval/mod.rs | 8 | ||||
| -rw-r--r-- | 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 2b00ff44..69cf5d5e 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<EcoVec<hayagriva::Entry>> // 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 b0abd369..c1c4aed6 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<Arc<SyntaxSe // We might have multiple sublime-syntax/yaml files for (path, bytes) in paths.0.iter().zip(bytes.iter()) { - let src = std::str::from_utf8(bytes).map_err(|_| FileError::InvalidUtf8)?; + let src = std::str::from_utf8(bytes).map_err(FileError::from)?; out.add( SyntaxDefinition::load_from_str(src, false, None) .map_err(|e| eco_format!("failed to parse syntax file `{path}`: {e}"))?, diff --git a/crates/typst/src/diag.rs b/crates/typst/src/diag.rs index 1cc4a045..99c61608 100644 --- a/crates/typst/src/diag.rs +++ b/crates/typst/src/diag.rs @@ -133,8 +133,13 @@ impl SourceDiagnostic { } /// Adds a single hint to the diagnostic. - pub fn with_hint(mut self, hint: EcoString) -> Self { - self.hints.push(hint); + pub fn hint(&mut self, hint: impl Into<EcoString>) { + self.hints.push(hint.into()); + } + + /// Adds a single hint to the diagnostic. + pub fn with_hint(mut self, hint: impl Into<EcoString>) -> Self { + self.hint(hint); self } @@ -238,7 +243,15 @@ where S: Into<EcoString>, { fn at(self, span: Span) -> SourceResult<T> { - 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<PackageError> 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 d79f2ea5..d1f248c8 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 d839422a..69dc6a6b 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; } |
