blob: 2e8ecc4b22fe6c36c8a1d35ef015765b21ee7f48 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax;
2use anyhow::anyhow;
David Tolnay7db73692019-10-20 14:51:12 -04003use codespan_reporting::diagnostic::{Diagnostic, Label};
David Tolnaye2e7bc32020-03-18 14:02:01 -07004use codespan_reporting::files::SimpleFiles;
David Tolnay7db73692019-10-20 14:51:12 -04005use codespan_reporting::term::termcolor::{ColorChoice, StandardStream, WriteColor};
6use codespan_reporting::term::{self, Config};
David Tolnay52509842020-04-25 19:38:59 -07007use std::error::Error as StdError;
8use std::fmt::{self, Display};
9use std::io::{self, Write};
David Tolnay7db73692019-10-20 14:51:12 -040010use std::ops::Range;
11use std::path::Path;
12use std::process;
13
David Tolnay52509842020-04-25 19:38:59 -070014pub(super) type Result<T, E = Error> = std::result::Result<T, E>;
15
16#[derive(Debug)]
17pub(super) enum Error {
18 NoBridgeMod,
19 OutOfLineMod,
20 Io(io::Error),
21 Syn(syn::Error),
22}
23
24impl Display for Error {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 match self {
27 Error::NoBridgeMod => write!(f, "no #[cxx::bridge] module found"),
28 Error::OutOfLineMod => write!(f, "#[cxx::bridge] module must have inline contents"),
29 Error::Io(err) => err.fmt(f),
30 Error::Syn(err) => err.fmt(f),
31 }
32 }
33}
34
35impl StdError for Error {
36 fn source(&self) -> Option<&(dyn StdError + 'static)> {
37 match self {
38 Error::Io(err) => Some(err),
39 Error::Syn(err) => Some(err),
40 _ => None,
41 }
42 }
43}
44
45impl From<io::Error> for Error {
46 fn from(err: io::Error) -> Self {
47 Error::Io(err)
48 }
49}
50
51impl From<syn::Error> for Error {
52 fn from(err: syn::Error) -> Self {
53 Error::Syn(err)
54 }
55}
56
David Tolnay7db73692019-10-20 14:51:12 -040057pub(super) fn format_err(path: &Path, source: &str, error: Error) -> ! {
58 match error {
59 Error::Syn(syn_error) => {
60 let writer = StandardStream::stderr(ColorChoice::Auto);
61 let ref mut stderr = writer.lock();
62 for error in syn_error {
63 let _ = writeln!(stderr);
64 display_syn_error(stderr, path, source, error);
65 }
66 }
67 _ => eprintln!("cxxbridge: {:?}", anyhow!(error)),
68 }
69 process::exit(1);
70}
71
72fn display_syn_error(stderr: &mut dyn WriteColor, path: &Path, source: &str, error: syn::Error) {
73 let span = error.span();
74 let start = span.start();
75 let end = span.end();
76
77 let mut start_offset = 0;
78 for _ in 1..start.line {
79 start_offset += source[start_offset..].find('\n').unwrap() + 1;
80 }
81 start_offset += start.column;
82
83 let mut end_offset = start_offset;
84 if start.line == end.line {
85 end_offset -= start.column;
86 } else {
87 for _ in 0..end.line - start.line {
88 end_offset += source[end_offset..].find('\n').unwrap() + 1;
89 }
90 }
91 end_offset += end.column;
92
David Tolnaye2e7bc32020-03-18 14:02:01 -070093 let mut files = SimpleFiles::new();
David Tolnay7db73692019-10-20 14:51:12 -040094 let file = files.add(path.to_string_lossy(), source);
95
David Tolnaye2e7bc32020-03-18 14:02:01 -070096 let diagnostic = diagnose(file, start_offset..end_offset, error);
David Tolnay7db73692019-10-20 14:51:12 -040097
98 let config = Config::default();
99 let _ = term::emit(stderr, &config, &files, &diagnostic);
100}
101
David Tolnaye2e7bc32020-03-18 14:02:01 -0700102fn diagnose(file: usize, range: Range<usize>, error: syn::Error) -> Diagnostic<usize> {
David Tolnay7db73692019-10-20 14:51:12 -0400103 let message = error.to_string();
104 let info = syntax::error::ERRORS
105 .iter()
106 .find(|e| message.contains(e.msg));
David Tolnaye2e7bc32020-03-18 14:02:01 -0700107 let mut diagnostic = Diagnostic::error().with_message(&message);
108 let mut label = Label::primary(file, range);
109 if let Some(info) = info {
110 label.message = info.label.map_or(message, str::to_owned);
111 diagnostic.labels.push(label);
112 diagnostic.notes.extend(info.note.map(str::to_owned));
David Tolnay7db73692019-10-20 14:51:12 -0400113 } else {
David Tolnaye2e7bc32020-03-18 14:02:01 -0700114 label.message = message;
115 diagnostic.labels.push(label);
116 }
David Tolnay7db73692019-10-20 14:51:12 -0400117 diagnostic.code = Some("cxxbridge".to_owned());
118 diagnostic
119}