• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pomsky-lang / pomsky / 6421156176

05 Oct 2023 03:27PM UTC coverage: 80.191% (-0.1%) from 80.315%
6421156176

push

github

Aloso
chore: update deps, fix new lints

25 of 25 new or added lines in 6 files covered. (100.0%)

3267 of 4074 relevant lines covered (80.19%)

246.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.74
/pomsky-bin/src/result/mod.rs
1
use std::fmt;
2

3
use pomsky::diagnose::{DiagnosticCode, DiagnosticKind};
4
use serde::{Deserialize, Serialize};
5

6
mod serde_code;
7

8
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3✔
9
pub struct CompilationResult {
10
    /// Schema version
11
    pub version: Version,
×
12
    /// Whether compilation succeeded
13
    ///
14
    /// Equivalent to `result.output.is_some()`
15
    pub success: bool,
×
16
    /// Compilation result
17
    #[serde(skip_serializing_if = "Option::is_none")]
18
    pub output: Option<String>,
×
19
    /// Array of errors and warnings
20
    pub diagnostics: Vec<Diagnostic>,
×
21
    /// Compilation time
22
    pub timings: Timings,
×
23
}
24

25
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3✔
26
pub enum Version {
27
    #[serde(rename = "1")]
28
    V1,
29
}
30

31
impl CompilationResult {
32
    pub fn success(output: String, time_micros: u128) -> Self {
2✔
33
        Self {
2✔
34
            version: Version::V1,
35
            success: true,
36
            output: Some(output),
2✔
37
            diagnostics: vec![],
2✔
38
            timings: Timings::from_micros(time_micros),
2✔
39
        }
40
    }
2✔
41

42
    pub fn error(time_micros: u128) -> Self {
1✔
43
        Self {
1✔
44
            version: Version::V1,
45
            success: false,
46
            output: None,
1✔
47
            diagnostics: vec![],
1✔
48
            timings: Timings::from_micros(time_micros),
1✔
49
        }
50
    }
1✔
51

52
    pub fn with_diagnostics(
3✔
53
        mut self,
54
        diagnostics: impl IntoIterator<Item = pomsky::diagnose::Diagnostic>,
55
        source_code: Option<&str>,
56
    ) -> Self {
57
        self.diagnostics.extend(diagnostics.into_iter().map(|d| Diagnostic::from(d, source_code)));
6✔
58
        self
3✔
59
    }
3✔
60

61
    pub fn output_json(&self) {
3✔
62
        match serde_json::to_string(self) {
3✔
63
            Ok(string) => println!("{string}"),
3✔
64
            Err(e) => eprintln!("{e}"),
×
65
        }
66
    }
3✔
67
}
68

69
#[derive(Debug, PartialEq, Serialize, Deserialize)]
12✔
70
pub struct Diagnostic {
71
    /// "error" | "warning"
72
    pub severity: Severity,
3✔
73
    /// See [`DiagnosticKind`](pomsky::diagnose::DiagnosticKind)
74
    ///
75
    /// Currently "syntax" | "resolve" | "compat" | "unsupported" | "deprecated"
76
    /// | "limits" | "other"
77
    pub kind: Kind,
3✔
78
    /// See [`DiagnosticCode`](pomsky::diagnose::DiagnosticCode)
79
    #[serde(with = "serde_code", skip_serializing_if = "Option::is_none")]
80
    pub code: Option<DiagnosticCode>,
3✔
81
    /// List of locations that should be underlined
82
    ///
83
    /// Currently guaranteed to contain exactly 1 span
84
    pub spans: Vec<Span>,
3✔
85
    /// Error/warning message
86
    pub description: String,
3✔
87
    /// Help text
88
    ///
89
    /// Currently guaranteed to contain at most 1 string
90
    pub help: Vec<String>,
3✔
91
    /// Automatically applicable fixes
92
    ///
93
    /// Currently unused and guaranteed to be empty
94
    pub fixes: Vec<QuickFix>,
3✔
95
    /// Visual representation of the diagnostic as displayed in the CLI
96
    pub visual: String,
3✔
97
}
98

99
#[derive(Debug, PartialEq, Serialize, Deserialize)]
6✔
100
#[serde(rename_all = "lowercase")]
101
pub enum Severity {
102
    Error,
103
    Warning,
104
}
105

106
impl From<pomsky::diagnose::Severity> for Severity {
107
    fn from(value: pomsky::diagnose::Severity) -> Self {
3✔
108
        match value {
3✔
109
            pomsky::diagnose::Severity::Error => Severity::Error,
1✔
110
            pomsky::diagnose::Severity::Warning => Severity::Warning,
2✔
111
        }
112
    }
3✔
113
}
114

115
#[derive(Debug, PartialEq, Serialize, Deserialize)]
6✔
116
#[serde(rename_all = "lowercase")]
117
pub enum Kind {
118
    Syntax,
119
    Resolve,
120
    Compat,
121
    Unsupported,
122
    Deprecated,
123
    Limits,
124
    Other,
125
}
126

127
impl From<DiagnosticKind> for Kind {
128
    fn from(value: DiagnosticKind) -> Self {
3✔
129
        match value {
3✔
130
            DiagnosticKind::Syntax => Kind::Syntax,
×
131
            DiagnosticKind::Resolve => Kind::Resolve,
×
132
            DiagnosticKind::Compat => Kind::Compat,
2✔
133
            DiagnosticKind::Unsupported => Kind::Unsupported,
×
134
            DiagnosticKind::Deprecated => Kind::Deprecated,
1✔
135
            DiagnosticKind::Limits => Kind::Limits,
×
136
            DiagnosticKind::Other => Kind::Other,
×
137
            _ => panic!("unknown diagnostic kind"),
×
138
        }
139
    }
3✔
140
}
141

142
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3✔
143
pub struct Timings {
144
    /// time of all performed compilation steps in microseconds
145
    pub all: u128,
×
146
}
147

148
impl Timings {
149
    pub fn from_micros(micros: u128) -> Self {
3✔
150
        Timings { all: micros }
3✔
151
    }
3✔
152
}
153

154
#[derive(Debug, PartialEq, Serialize, Deserialize)]
9✔
155
pub struct Span {
156
    /// Start byte offset, counting from zero, assuming UTF-8 encoding.
157
    ///
158
    /// Guaranteed to be non-negative.
159
    pub start: usize,
3✔
160
    /// End byte offset, non-inclusive, counting from zero, assuming UTF-8
161
    /// encoding.
162
    ///
163
    /// Guaranteed to be at least `start`.
164
    pub end: usize,
3✔
165
    /// Additional details only relevant to this specific span
166
    ///
167
    /// Currently unused, guaranteed to be absent
168
    #[serde(skip_serializing_if = "Option::is_none")]
169
    pub label: Option<String>,
3✔
170
}
171

172
impl From<std::ops::Range<usize>> for Span {
173
    fn from(value: std::ops::Range<usize>) -> Self {
3✔
174
        Span { start: value.start, end: value.end, label: None }
3✔
175
    }
3✔
176
}
177

178
#[derive(Debug, PartialEq, Serialize, Deserialize)]
×
179
pub struct QuickFix {
180
    /// Short description what this quick fix does
181
    pub description: String,
×
182
    /// List of changes to fix this diagnostic.
183
    ///
184
    /// Guaranteed to be in source order and non-overlapping (e.g. `1-4`,
185
    /// `7-12`, `14-15`, `16-16`)
186
    pub replacements: Vec<Replacement>,
×
187
}
188

189
#[derive(Debug, PartialEq, Serialize, Deserialize)]
×
190
pub struct Replacement {
191
    /// Start byte offset, counting from zero, assuming UTF-8 encoding.
192
    ///
193
    /// Guaranteed to be non-negative.
194
    pub start: usize,
×
195
    /// End byte offset, non-inclusive, counting from zero, assuming UTF-8
196
    /// encoding
197
    ///
198
    /// Guaranteed to be at least `start`.
199
    pub end: usize,
×
200
    /// Text to replace this part of code with
201
    pub insert: String,
×
202
}
203

204
impl Diagnostic {
205
    fn from(value: pomsky::diagnose::Diagnostic, source_code: Option<&str>) -> Self {
3✔
206
        let kind = value.kind.to_string();
3✔
207
        let display = value.default_display(source_code);
3✔
208
        let severity: &str = value.severity.into();
3✔
209

210
        let visual = match value.code {
3✔
211
            Some(code) => format!("{severity} {code}{kind}:\n{display}"),
3✔
212
            None => format!("{severity}{kind}:\n{display}"),
×
213
        };
214
        drop(display);
3✔
215

216
        Diagnostic {
3✔
217
            severity: value.severity.into(),
3✔
218
            kind: value.kind.into(),
3✔
219
            code: value.code,
3✔
220
            spans: value.span.range().into_iter().map(From::from).collect(),
3✔
221
            description: value.msg,
3✔
222
            help: value.help.into_iter().collect(),
3✔
223
            fixes: vec![],
3✔
224
            visual,
3✔
225
        }
226
    }
3✔
227
}
228

229
impl fmt::Display for CompilationResult {
230
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
×
231
        write!(f, "{self:?}")
×
232
    }
×
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc