Skip to content

Commit f0bb335

Browse files
committed
add json for warnings
1 parent 29ab7d7 commit f0bb335

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl RichError {
213213
}
214214
}
215215

216-
pub(crate) fn get_line_col(file: &str, offset: usize) -> (usize, usize) {
216+
pub fn get_line_col(file: &str, offset: usize) -> (usize, usize) {
217217
let mut line = 1;
218218
let mut last_newline_offset = 0;
219219

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::error::{ErrorCollector, WithFile, WithFilePath};
3535
use crate::parse::ParseFromStrWithErrors;
3636
pub use crate::types::ResolvedType;
3737
pub use crate::value::Value;
38+
pub use crate::error::get_line_col;
3839
pub use crate::warning::{WarnCategory, Warning};
3940
pub use crate::witness::{Arguments, Parameters, WitnessTypes, WitnessValues};
4041

src/main.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ use base64::display::Base64Display;
22
use base64::engine::general_purpose::STANDARD;
33
use clap::{Arg, ArgAction, Command};
44

5-
use simplicityhl::{AbiMeta, TemplateProgram, WarnCategory};
5+
use simplicityhl::{get_line_col, AbiMeta, TemplateProgram, WarnCategory};
66
use std::{env, fmt};
77

8+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
9+
struct OutputWarning {
10+
message: String,
11+
file: String,
12+
line: usize,
13+
column: usize,
14+
}
15+
816
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
917
/// The compilation output.
1018
struct Output {
@@ -16,6 +24,8 @@ struct Output {
1624
abi_meta: Option<AbiMeta>,
1725
/// Commitment Merkle Root (CMR) of the program, hex encoded.
1826
cmr: String,
27+
/// Compiler warnings produced during compilation.
28+
warnings: Vec<OutputWarning>,
1929
}
2030

2131
impl fmt::Display for Output {
@@ -155,7 +165,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
155165
std::process::exit(1);
156166
});
157167

158-
let template = match TemplateProgram::new_with_path(prog_text, prog_file.as_str()) {
168+
let template = match TemplateProgram::new_with_path(prog_text.clone(), prog_file.as_str()) {
159169
Ok(t) => t,
160170
Err(e) => {
161171
eprintln!("{}", e);
@@ -176,17 +186,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
176186
});
177187

178188
let n_warnings = template.warnings().len();
189+
let warning_strings: Vec<OutputWarning> = template
190+
.warnings()
191+
.iter()
192+
.map(|w| {
193+
let (line, column) = get_line_col(&prog_text, w.span.start);
194+
OutputWarning {
195+
message: w.canonical_name.to_string(),
196+
file: prog_file.clone(),
197+
line,
198+
column,
199+
}
200+
})
201+
.collect();
179202
if n_warnings > 0 {
180-
eprint!("{}", template.format_warnings(prog_file));
181-
let word = if n_warnings == 1 {
182-
"warning"
183-
} else {
184-
"warnings"
185-
};
186-
eprintln!(
187-
"\x1b[1;33mwarning\x1b[0m: `{}` generated {} {}",
188-
prog_file, n_warnings, word,
189-
);
203+
if !output_json {
204+
eprint!("{}", template.format_warnings(prog_file));
205+
let word = if n_warnings == 1 { "warning" } else { "warnings" };
206+
eprintln!(
207+
"\x1b[1;33mwarning\x1b[0m: `{}` generated {} {}",
208+
prog_file, n_warnings, word,
209+
);
210+
}
190211
if deny_warnings {
191212
eprintln!("\x1b[1;31merror\x1b[0m: warnings treated as errors (--deny-warnings)");
192213
std::process::exit(1);
@@ -244,6 +265,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
244265
witness: witness_bytes.map(|bytes| Base64Display::new(&bytes, &STANDARD).to_string()),
245266
abi_meta: abi_opt,
246267
cmr: cmr_hex,
268+
warnings: warning_strings,
247269
};
248270

249271
if output_json {

0 commit comments

Comments
 (0)