1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
3 | |
4 | use slint_interpreter::{Diagnostic, DiagnosticLevel}; |
5 | |
6 | /// This enum describes the level or severity of a diagnostic message produced by the compiler. |
7 | #[napi (js_name = "DiagnosticLevel" )] |
8 | pub enum JsDiagnosticLevel { |
9 | /// The diagnostic found is an error that prevents successful compilation. |
10 | Error, |
11 | |
12 | /// The diagnostic found is a warning. |
13 | Warning, |
14 | } |
15 | |
16 | impl From<DiagnosticLevel> for JsDiagnosticLevel { |
17 | fn from(diagnostic_level: DiagnosticLevel) -> Self { |
18 | match diagnostic_level { |
19 | DiagnosticLevel::Warning => JsDiagnosticLevel::Warning, |
20 | _ => JsDiagnosticLevel::Error, |
21 | } |
22 | } |
23 | } |
24 | |
25 | /// This structure represent a diagnostic emitted while compiling .slint code. |
26 | /// |
27 | /// It is basically a message, a level (warning or error), attached to a |
28 | /// position in the code. |
29 | #[napi (object, js_name = "Diagnostic" )] |
30 | pub struct JsDiagnostic { |
31 | /// The level for this diagnostic. |
32 | pub level: JsDiagnosticLevel, |
33 | |
34 | /// Message for this diagnostic. |
35 | pub message: String, |
36 | |
37 | /// The line number in the .slint source file. The line number starts with 1. |
38 | pub line_number: u32, |
39 | |
40 | // The column in the .slint source file. The column number starts with 1. |
41 | pub column_number: u32, |
42 | |
43 | /// The path of the source file where this diagnostic occurred. |
44 | pub file_name: Option<String>, |
45 | } |
46 | |
47 | impl From<Diagnostic> for JsDiagnostic { |
48 | fn from(internal_diagnostic: Diagnostic) -> Self { |
49 | let (line_number: usize, column: usize) = internal_diagnostic.line_column(); |
50 | Self { |
51 | level: internal_diagnostic.level().into(), |
52 | message: internal_diagnostic.message().into(), |
53 | line_number: line_number as u32, |
54 | column_number: column as u32, |
55 | file_name: internal_diagnosticOption<&str> |
56 | .source_file() |
57 | .and_then(|path: &Path| path.to_str()) |
58 | .map(|str: &str| str.into()), |
59 | } |
60 | } |
61 | } |
62 | |