From 2d6f3ed6c2e6abf912a08a218234759386c0a70c Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Wed, 8 Mar 2023 13:10:03 +0100 Subject: [PATCH] Address comments --- ruby/extractor/src/diagnostics.rs | 26 +++++++++++++++----------- ruby/extractor/src/extractor.rs | 30 +++++++++++++++--------------- ruby/extractor/src/main.rs | 21 ++++++++++++--------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/ruby/extractor/src/diagnostics.rs b/ruby/extractor/src/diagnostics.rs index d21408b3368..ae62a254b08 100644 --- a/ruby/extractor/src/diagnostics.rs +++ b/ruby/extractor/src/diagnostics.rs @@ -214,10 +214,12 @@ fn longest_backtick_sequence_length(text: &str) -> usize { } 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), Link(&'a str, &'a str), - None, } impl DiagnosticMessage { @@ -242,16 +244,15 @@ impl DiagnosticMessage { 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 args = args.iter().chain(std::iter::repeat(&Arg::None)); let mut plain = 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); markdown.push_str(p); - match a { - Arg::Code(t) => { + match args.get(i) { + Some(MessageArg::Code(t)) => { plain.push_str(t); if t.len() > 0 { let count = longest_backtick_sequence_length(t) + 1; @@ -266,7 +267,7 @@ impl DiagnosticMessage { markdown.push_str(&"`".repeat(count)); } } - Arg::Link(text, url) => { + Some(MessageArg::Link(text, url)) => { plain.push_str(text); self.help_link(url); markdown.push_str("["); @@ -275,7 +276,7 @@ impl DiagnosticMessage { markdown.push_str(url); markdown.push_str(")"); } - Arg::None => {} + None => {} } } self.text(&plain); @@ -343,14 +344,17 @@ fn test_message() { let mut m = DiagnosticLoggers::new("foo") .logger() .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.markdown_message); let mut m = DiagnosticLoggers::new("foo") .logger() .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`! ``", diff --git a/ruby/extractor/src/extractor.rs b/ruby/extractor/src/extractor.rs index b8952e971b0..7b9ed84adfb 100644 --- a/ruby/extractor/src/extractor.rs +++ b/ruby/extractor/src/extractor.rs @@ -277,7 +277,7 @@ impl<'a> Visitor<'a> { fn record_parse_error_for_node( &mut self, message: &str, - args: &[diagnostics::Arg], + args: &[diagnostics::MessageArg], node: Node, status_page: bool, ) { @@ -307,7 +307,7 @@ impl<'a> Visitor<'a> { if node.is_missing() { 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.", - &[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, true, ); @@ -316,7 +316,7 @@ impl<'a> Visitor<'a> { if node.is_error() { 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.", - &[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, true, ); @@ -409,7 +409,7 @@ impl<'a> Visitor<'a> { .location(self.path, start_line, start_column, end_line, end_column) .message( "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( "Type mismatch for field {}::{} with type {} != {}", &[ - diagnostics::Arg::Code(node.kind()), - diagnostics::Arg::Code(child_node.field_name.unwrap_or("child")), - diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)), - diagnostics::Arg::Code(&format!("{:?}", field.type_info)), + diagnostics::MessageArg::Code(node.kind()), + diagnostics::MessageArg::Code(child_node.field_name.unwrap_or("child")), + diagnostics::MessageArg::Code(&format!("{:?}", child_node.type_name)), + diagnostics::MessageArg::Code(&format!("{:?}", field.type_info)), ], *node, false, @@ -474,9 +474,9 @@ impl<'a> Visitor<'a> { self.record_parse_error_for_node( "Value for unknown field: {}::{} and type {}", &[ - diagnostics::Arg::Code(node.kind()), - diagnostics::Arg::Code(&child_node.field_name.unwrap_or("child")), - diagnostics::Arg::Code(&format!("{:?}", child_node.type_name)), + diagnostics::MessageArg::Code(node.kind()), + diagnostics::MessageArg::Code(&child_node.field_name.unwrap_or("child")), + diagnostics::MessageArg::Code(&format!("{:?}", child_node.type_name)), ], *node, false, @@ -516,8 +516,8 @@ impl<'a> Visitor<'a> { self.record_parse_error_for_node( "Too many values for field: {}::{}", &[ - diagnostics::Arg::Code(node.kind()), - diagnostics::Arg::Code(table_name), + diagnostics::MessageArg::Code(node.kind()), + diagnostics::MessageArg::Code(table_name), ], *node, false, @@ -637,8 +637,8 @@ fn location_for(visitor: &mut Visitor, n: Node) -> (usize, usize, usize, usize) .message( "Cannot correct end column value: end_byte index {} is not in range [1,{}].", &[ - diagnostics::Arg::Code(&index.to_string()), - diagnostics::Arg::Code(&source.len().to_string()), + diagnostics::MessageArg::Code(&index.to_string()), + diagnostics::MessageArg::Code(&source.len().to_string()), ], ) .severity(diagnostics::Severity::Error), diff --git a/ruby/extractor/src/main.rs b/ruby/extractor/src/main.rs index 83360eadc39..e95f7780df9 100644 --- a/ruby/extractor/src/main.rs +++ b/ruby/extractor/src/main.rs @@ -74,7 +74,10 @@ fn main() -> std::io::Result<()> { main_thread_logger.write( main_thread_logger .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), ); 1 @@ -95,7 +98,7 @@ fn main() -> std::io::Result<()> { main_thread_logger.write( main_thread_logger .new_entry("configuration-error", "Configuration error") - .message("{}; using gzip.", &[diagnostics::Arg::Code(&e)]) + .message("{}; using gzip.", &[diagnostics::MessageArg::Code(&e)]) .severity(diagnostics::Severity::Warning), ); trap::Compression::Gzip @@ -205,10 +208,10 @@ fn main() -> std::io::Result<()> { .message( "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::Arg::Code(&msg), - diagnostics::Arg::Code("encoding:"), - diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") + diagnostics::MessageArg::Code(&encoding_name), + diagnostics::MessageArg::Code(&msg), + diagnostics::MessageArg::Code("encoding:"), + diagnostics::MessageArg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") ], ) .status_page() @@ -225,9 +228,9 @@ fn main() -> std::io::Result<()> { .message( "Unknown character encoding {} in {} {}.", &[ - diagnostics::Arg::Code(&encoding_name), - diagnostics::Arg::Code("#encoding:"), - diagnostics::Arg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") + diagnostics::MessageArg::Code(&encoding_name), + diagnostics::MessageArg::Code("#encoding:"), + diagnostics::MessageArg::Link("directive", "https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-encoding+Directive") ], ) .status_page()