Address comments

This commit is contained in:
Arthur Baars
2023-03-08 13:10:03 +01:00
parent 858aa9ae63
commit 2d6f3ed6c2
3 changed files with 42 additions and 35 deletions

View File

@@ -214,10 +214,12 @@ fn longest_backtick_sequence_length(text: &str) -> usize {
} }
result result
} }
pub enum Arg<'a> { /**
* An argument of a diagnostic message format string. A message argument is either a "code" snippet or a link.
*/
pub enum MessageArg<'a> {
Code(&'a str), Code(&'a str),
Link(&'a str, &'a str), Link(&'a str, &'a str),
None,
} }
impl DiagnosticMessage { impl DiagnosticMessage {
@@ -242,16 +244,15 @@ impl DiagnosticMessage {
self self
} }
pub fn message(&mut self, text: &str, args: &[Arg]) -> &mut Self { pub fn message(&mut self, text: &str, args: &[MessageArg]) -> &mut Self {
let parts = text.split("{}"); let parts = text.split("{}");
let args = args.iter().chain(std::iter::repeat(&Arg::None));
let mut plain = String::with_capacity(2 * text.len()); let mut plain = String::with_capacity(2 * text.len());
let mut markdown = String::with_capacity(2 * text.len()); let mut markdown = String::with_capacity(2 * text.len());
for (p, a) in parts.zip(args) { for (i, p) in parts.enumerate() {
plain.push_str(p); plain.push_str(p);
markdown.push_str(p); markdown.push_str(p);
match a { match args.get(i) {
Arg::Code(t) => { Some(MessageArg::Code(t)) => {
plain.push_str(t); plain.push_str(t);
if t.len() > 0 { if t.len() > 0 {
let count = longest_backtick_sequence_length(t) + 1; let count = longest_backtick_sequence_length(t) + 1;
@@ -266,7 +267,7 @@ impl DiagnosticMessage {
markdown.push_str(&"`".repeat(count)); markdown.push_str(&"`".repeat(count));
} }
} }
Arg::Link(text, url) => { Some(MessageArg::Link(text, url)) => {
plain.push_str(text); plain.push_str(text);
self.help_link(url); self.help_link(url);
markdown.push_str("["); markdown.push_str("[");
@@ -275,7 +276,7 @@ impl DiagnosticMessage {
markdown.push_str(url); markdown.push_str(url);
markdown.push_str(")"); markdown.push_str(")");
} }
Arg::None => {} None => {}
} }
} }
self.text(&plain); self.text(&plain);
@@ -343,14 +344,17 @@ fn test_message() {
let mut m = DiagnosticLoggers::new("foo") let mut m = DiagnosticLoggers::new("foo")
.logger() .logger()
.new_entry("id", "name"); .new_entry("id", "name");
m.message("hello: {}", &[Arg::Code("hello")]); m.message("hello: {}", &[MessageArg::Code("hello")]);
assert_eq!("hello: hello", m.plaintext_message); assert_eq!("hello: hello", m.plaintext_message);
assert_eq!("hello: `hello`", m.markdown_message); assert_eq!("hello: `hello`", m.markdown_message);
let mut m = DiagnosticLoggers::new("foo") let mut m = DiagnosticLoggers::new("foo")
.logger() .logger()
.new_entry("id", "name"); .new_entry("id", "name");
m.message("hello with backticks: {}", &[Arg::Code("oh `hello`!")]); m.message(
"hello with backticks: {}",
&[MessageArg::Code("oh `hello`!")],
);
assert_eq!("hello with backticks: oh `hello`!", m.plaintext_message); assert_eq!("hello with backticks: oh `hello`!", m.plaintext_message);
assert_eq!( assert_eq!(
"hello with backticks: `` oh `hello`! ``", "hello with backticks: `` oh `hello`! ``",

View File

@@ -277,7 +277,7 @@ impl<'a> Visitor<'a> {
fn record_parse_error_for_node( fn record_parse_error_for_node(
&mut self, &mut self,
message: &str, message: &str,
args: &[diagnostics::Arg], args: &[diagnostics::MessageArg],
node: Node, node: Node,
status_page: bool, status_page: bool,
) { ) {
@@ -307,7 +307,7 @@ impl<'a> Visitor<'a> {
if node.is_missing() { if node.is_missing() {
self.record_parse_error_for_node( self.record_parse_error_for_node(
"A parse error occurred (expected {} symbol). Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.", "A parse error occurred (expected {} symbol). Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.",
&[diagnostics::Arg::Code(node.kind()), diagnostics::Arg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")], &[diagnostics::MessageArg::Code(node.kind()), diagnostics::MessageArg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")],
node, node,
true, true,
); );
@@ -316,7 +316,7 @@ impl<'a> Visitor<'a> {
if node.is_error() { if node.is_error() {
self.record_parse_error_for_node( self.record_parse_error_for_node(
"A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.", "A parse error occurred. Check the syntax of the file. If the file is invalid, correct the error or {} the file from analysis.",
&[diagnostics::Arg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")], &[diagnostics::MessageArg::Link("exclude", "https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning")],
node, node,
true, true,
); );
@@ -409,7 +409,7 @@ impl<'a> Visitor<'a> {
.location(self.path, start_line, start_column, end_line, end_column) .location(self.path, start_line, start_column, end_line, end_column)
.message( .message(
"Unknown table type: {}", "Unknown table type: {}",
&[diagnostics::Arg::Code(node.kind())], &[diagnostics::MessageArg::Code(node.kind())],
), ),
); );
@@ -461,10 +461,10 @@ impl<'a> Visitor<'a> {
self.record_parse_error_for_node( self.record_parse_error_for_node(
"Type mismatch for field {}::{} with type {} != {}", "Type mismatch for field {}::{} with type {} != {}",
&[ &[
diagnostics::Arg::Code(node.kind()), diagnostics::MessageArg::Code(node.kind()),
diagnostics::Arg::Code(child_node.field_name.unwrap_or("child")), diagnostics::MessageArg::Code(child_node.field_name.unwrap_or("child")),
diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)), diagnostics::MessageArg::Code(&format!("{:?}", child_node.type_name)),
diagnostics::Arg::Code(&format!("{:?}", field.type_info)), diagnostics::MessageArg::Code(&format!("{:?}", field.type_info)),
], ],
*node, *node,
false, false,
@@ -474,9 +474,9 @@ impl<'a> Visitor<'a> {
self.record_parse_error_for_node( self.record_parse_error_for_node(
"Value for unknown field: {}::{} and type {}", "Value for unknown field: {}::{} and type {}",
&[ &[
diagnostics::Arg::Code(node.kind()), diagnostics::MessageArg::Code(node.kind()),
diagnostics::Arg::Code(&child_node.field_name.unwrap_or("child")), diagnostics::MessageArg::Code(&child_node.field_name.unwrap_or("child")),
diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)), diagnostics::MessageArg::Code(&format!("{:?}", child_node.type_name)),
], ],
*node, *node,
false, false,
@@ -516,8 +516,8 @@ impl<'a> Visitor<'a> {
self.record_parse_error_for_node( self.record_parse_error_for_node(
"Too many values for field: {}::{}", "Too many values for field: {}::{}",
&[ &[
diagnostics::Arg::Code(node.kind()), diagnostics::MessageArg::Code(node.kind()),
diagnostics::Arg::Code(table_name), diagnostics::MessageArg::Code(table_name),
], ],
*node, *node,
false, false,
@@ -637,8 +637,8 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize)
.message( .message(
"Cannot correct end column value: end_byte index {} is not in range [1,{}].", "Cannot correct end column value: end_byte index {} is not in range [1,{}].",
&[ &[
diagnostics::Arg::Code(&index.to_string()), diagnostics::MessageArg::Code(&index.to_string()),
diagnostics::Arg::Code(&source.len().to_string()), diagnostics::MessageArg::Code(&source.len().to_string()),
], ],
) )
.severity(diagnostics::Severity::Error), .severity(diagnostics::Severity::Error),

View File

@@ -74,7 +74,10 @@ fn main() -> std::io::Result<()> {
main_thread_logger.write( main_thread_logger.write(
main_thread_logger main_thread_logger
.new_entry("configuration-error", "Configuration error") .new_entry("configuration-error", "Configuration error")
.message("{}; defaulting to 1 thread.", &[diagnostics::Arg::Code(&e)]) .message(
"{}; defaulting to 1 thread.",
&[diagnostics::MessageArg::Code(&e)],
)
.severity(diagnostics::Severity::Warning), .severity(diagnostics::Severity::Warning),
); );
1 1
@@ -95,7 +98,7 @@ fn main() -> std::io::Result<()> {
main_thread_logger.write( main_thread_logger.write(
main_thread_logger main_thread_logger
.new_entry("configuration-error", "Configuration error") .new_entry("configuration-error", "Configuration error")
.message("{}; using gzip.", &[diagnostics::Arg::Code(&e)]) .message("{}; using gzip.", &[diagnostics::MessageArg::Code(&e)])
.severity(diagnostics::Severity::Warning), .severity(diagnostics::Severity::Warning),
); );
trap::Compression::Gzip trap::Compression::Gzip
@@ -205,10 +208,10 @@ fn main() -> std::io::Result<()> {
.message( .message(
"Could not decode the file contents as {}: {}. The contents of the file must match the character encoding specified in the {} {}.", "Could not decode the file contents as {}: {}. The contents of the file must match the character encoding specified in the {} {}.",
&[ &[
diagnostics::Arg::Code(&encoding_name), diagnostics::MessageArg::Code(&encoding_name),
diagnostics::Arg::Code(&msg), diagnostics::MessageArg::Code(&msg),
diagnostics::Arg::Code("encoding:"), diagnostics::MessageArg::Code("encoding:"),
diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") diagnostics::MessageArg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
], ],
) )
.status_page() .status_page()
@@ -225,9 +228,9 @@ fn main() -> std::io::Result<()> {
.message( .message(
"Unknown character encoding {} in {} {}.", "Unknown character encoding {} in {} {}.",
&[ &[
diagnostics::Arg::Code(&encoding_name), diagnostics::MessageArg::Code(&encoding_name),
diagnostics::Arg::Code("#encoding:"), diagnostics::MessageArg::Code("#encoding:"),
diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") diagnostics::MessageArg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive")
], ],
) )
.status_page() .status_page()