1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#![allow(clippy::derive_partial_eq_without_eq)]

use std::convert::TryFrom;
use strum_macros::Display;
use strum_macros::EnumString;
use thiserror::Error;

include!(concat!(env!("OUT_DIR"), "/sarif.rs"));

#[doc = "The SARIF format version of this log file."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum Version {
  #[strum(serialize = "2.1.0")]
  V2_1_0,
}

// todo: should be generated / synced with schema.json
pub static SCHEMA_URL: &str =
  "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json";

#[doc = "The role or roles played by the artifact in the analysis."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ArtifactRoles {
  #[strum(serialize = "analysisTarget")]
  AnalysisTarget,
  #[strum(serialize = "attachment")]
  Attachment,
  #[strum(serialize = "responseFile")]
  ResponseFile,
  #[strum(serialize = "resultFile")]
  ResultFile,
  #[strum(serialize = "standardStream")]
  StandardStream,
  #[strum(serialize = "tracedFile")]
  TracedFile,
  #[strum(serialize = "unmodified")]
  Unmodified,
  #[strum(serialize = "modified")]
  Modified,
  #[strum(serialize = "added")]
  Added,
  #[strum(serialize = "deleted")]
  Deleted,
  #[strum(serialize = "renamed")]
  Renamed,
  #[strum(serialize = "uncontrolled")]
  Uncontrolled,
  #[strum(serialize = "driver")]
  Driver,
  #[strum(serialize = "extension")]
  Extension,
  #[strum(serialize = "translation")]
  Translation,
  #[strum(serialize = "taxonomy")]
  Taxonomy,
  #[strum(serialize = "policy")]
  Policy,
  #[strum(serialize = "referencedOnCommandLine")]
  ReferencedOnCommandLine,
  #[strum(serialize = "memoryContents")]
  MemoryContents,
  #[strum(serialize = "directory")]
  Directory,
  #[strum(serialize = "userSpecifiedConfiguration")]
  UserSpecifiedConfiguration,
  #[strum(serialize = "toolSpecifiedConfiguration")]
  ToolSpecifiedConfiguration,
  #[strum(serialize = "debugOutputFile")]
  DebugOutputFile,
}

#[doc = "The SARIF format version of this external properties object."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ExternalPropertiesVersion {
  #[strum(serialize = "2.1.0")]
  V2_1_0,
}

#[doc = "A value specifying the severity level of the result."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum NotificationLevel {
  #[strum(serialize = "none")]
  None,
  #[strum(serialize = "note")]
  Note,
  #[strum(serialize = "warning")]
  Warning,
  #[strum(serialize = "error")]
  Error,
}

#[doc = "Specifies the failure level for the report."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ReportingConfigurationLevel {
  #[strum(serialize = "none")]
  None,
  #[strum(serialize = "note")]
  Note,
  #[strum(serialize = "warning")]
  Warning,
  #[strum(serialize = "error")]
  Error,
}

#[doc = "A value that categorizes results by evaluation state."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ResultKind {
  #[strum(serialize = "notApplicable")]
  NotApplicable,
  #[strum(serialize = "pass")]
  Pass,
  #[strum(serialize = "fail")]
  Fail,
  #[strum(serialize = "review")]
  Review,
  #[strum(serialize = "open")]
  Open,
  #[strum(serialize = "informational")]
  Informational,
}

#[doc = "A value specifying the severity level of the result."]
#[derive(Clone, Copy, Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ResultLevel {
  #[strum(serialize = "none")]
  None,
  #[strum(serialize = "note")]
  Note,
  #[strum(serialize = "warning")]
  Warning,
  #[strum(serialize = "error")]
  Error,
}

#[doc = "The state of a result relative to a baseline of a previous run."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ResultBaselineState {
  #[strum(serialize = "new")]
  New,
  #[strum(serialize = "unchanged")]
  Unchanged,
  #[strum(serialize = "updated")]
  Updated,
  #[strum(serialize = "absent")]
  Absent,
}

#[doc = "Specifies the unit in which the tool measures columns."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ResultColumnKind {
  #[strum(serialize = "utf16CodeUnits")]
  Utf16CodeUnits,
  #[strum(serialize = "unicodeCodePoints")]
  UnicodeCodePoints,
}

#[doc = "A string that indicates where the suppression is persisted."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum SupressionKind {
  #[strum(serialize = "inSource")]
  InSource,
  #[strum(serialize = "external")]
  External,
}

#[doc = "A string that indicates the review status of the suppression."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum SupressionStatus {
  #[strum(serialize = "accepted")]
  Accepted,
  #[strum(serialize = "underReview")]
  UnderReview,
}

#[doc = "Specifies the importance of this location in understanding the code flow in which it occurs. The order from most to least important is \"essential\", \"important\", \"unimportant\". Default: \"important\"."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ThreadFlowLocationImportance {
  #[strum(serialize = "important")]
  Important,
  #[strum(serialize = "essential")]
  Essential,
}

#[doc = "The kinds of data contained in this object."]
#[derive(Display, Debug, Serialize, Deserialize, EnumString)]
#[serde(untagged)]
pub enum ToolComponentContents {
  #[strum(serialize = "localizedData")]
  LocalizedData,
  #[strum(serialize = "nonLocalizedData")]
  NonLocalizedData,
}

// todo: implement for other error types, probably convert to procmacro
#[derive(Error, Debug)]
pub enum BuilderError {
  #[error(transparent)]
  LocationBuilderError {
    #[from]
    source: LocationBuilderError,
  },
  #[error(transparent)]
  PhysicalLocationBuilderError {
    #[from]
    source: PhysicalLocationBuilderError,
  },
  #[error(transparent)]
  RegionBuilderError {
    #[from]
    source: RegionBuilderError,
  },
  #[error(transparent)]
  ArtifactLocationBuilderError {
    #[from]
    source: ArtifactLocationBuilderError,
  },
  #[error(transparent)]
  ResultBuilderError {
    #[from]
    source: ResultBuilderError,
  },
}

// Note that due to the blanket implementation in core, TryFrom<AsRef<String>>
// results in a compiler error.
// https://github.com/rust-lang/rust/issues/50133
impl TryFrom<&String> for MultiformatMessageString {
  type Error = MultiformatMessageStringBuilderError;

  fn try_from(message: &String) -> anyhow::Result<Self, Self::Error> {
    MultiformatMessageStringBuilder::default()
      .text(message.clone())
      .build()
  }
}

impl TryFrom<&String> for Message {
  type Error = MessageBuilderError;

  fn try_from(message: &String) -> anyhow::Result<Self, Self::Error> {
    MessageBuilder::default().text(message.clone()).build()
  }
}

impl TryFrom<&str> for Message {
  type Error = MessageBuilderError;

  fn try_from(message: &str) -> anyhow::Result<Self, Self::Error> {
    MessageBuilder::default().text(message).build()
  }
}

impl TryFrom<ToolComponent> for Tool {
  type Error = ToolBuilderError;

  fn try_from(
    tool_component: ToolComponent,
  ) -> anyhow::Result<Self, Self::Error> {
    ToolBuilder::default().driver(tool_component).build()
  }
}