Skip to content

Commit d5e60df

Browse files
committed
Ruby: pass diagnostics::LogWriter to extractor
1 parent e85e61b commit d5e60df

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

ruby/extractor/src/diagnostics.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Visibility {
3535
pub telemetry: bool,
3636
}
3737

38-
#[derive(Serialize)]
38+
#[derive(Serialize, Clone)]
3939
#[serde(rename_all = "camelCase")]
4040
pub struct Location {
4141
#[serde(skip_serializing_if = "Option::is_none")]
@@ -176,7 +176,7 @@ impl DiagnosticLoggers {
176176
source: Source {
177177
id: id.to_owned(),
178178
name: name.to_owned(),
179-
extractor_name: Some(self.extractor),
179+
extractor_name: Some(self.extractor.to_owned()),
180180
},
181181
markdown_message: String::new(),
182182
plaintext_message: String::new(),
@@ -233,23 +233,31 @@ impl DiagnosticMessage {
233233
self
234234
}
235235
pub fn file<'a>(&'a mut self, path: &str) -> &'a mut Self {
236-
self.location.get_or_insert(EMPTY_LOCATION).file = Some(path.to_owned());
236+
self.location.get_or_insert(EMPTY_LOCATION.to_owned()).file = Some(path.to_owned());
237237
self
238238
}
239239
pub fn start_line<'a>(&'a mut self, start_line: i32) -> &'a mut Self {
240-
self.location.get_or_insert(EMPTY_LOCATION).start_line = Some(start_line);
240+
self.location
241+
.get_or_insert(EMPTY_LOCATION.to_owned())
242+
.start_line = Some(start_line);
241243
self
242244
}
243245
pub fn start_column<'a>(&'a mut self, start_column: i32) -> &'a mut Self {
244-
self.location.get_or_insert(EMPTY_LOCATION).start_column = Some(start_column);
246+
self.location
247+
.get_or_insert(EMPTY_LOCATION.to_owned())
248+
.start_column = Some(start_column);
245249
self
246250
}
247251
pub fn end_line<'a>(&'a mut self, end_line: i32) -> &'a mut Self {
248-
self.location.get_or_insert(EMPTY_LOCATION).end_line = Some(end_line);
252+
self.location
253+
.get_or_insert(EMPTY_LOCATION.to_owned())
254+
.end_line = Some(end_line);
249255
self
250256
}
251257
pub fn end_column<'a>(&'a mut self, end_column: i32) -> &'a mut Self {
252-
self.location.get_or_insert(EMPTY_LOCATION).end_column = Some(end_column);
258+
self.location
259+
.get_or_insert(EMPTY_LOCATION.to_owned())
260+
.end_column = Some(end_column);
253261
self
254262
}
255263
}

ruby/extractor/src/extractor.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::diagnostics;
12
use crate::trap;
23
use node_types::{EntryKind, Field, NodeTypeMap, Storage, TypeName};
34
use std::collections::BTreeMap as Map;
@@ -114,6 +115,7 @@ pub fn extract(
114115
language: Language,
115116
language_prefix: &str,
116117
schema: &NodeTypeMap,
118+
diagnostics_writer: &mut diagnostics::LogWriter,
117119
trap_writer: &mut trap::Writer,
118120
path: &Path,
119121
source: &[u8],
@@ -138,6 +140,7 @@ pub fn extract(
138140
let file_label = populate_file(trap_writer, path);
139141
let mut visitor = Visitor::new(
140142
source,
143+
diagnostics_writer,
141144
trap_writer,
142145
// TODO: should we handle path strings that are not valid UTF8 better?
143146
&path_str,
@@ -204,6 +207,8 @@ struct Visitor<'a> {
204207
file_label: trap::Label,
205208
/// The source code as a UTF-8 byte array
206209
source: &'a [u8],
210+
/// A diagnostics::LogWriter to write diagnostic messages
211+
diagnostics_writer: &'a mut diagnostics::LogWriter,
207212
/// A trap::Writer to accumulate trap entries
208213
trap_writer: &'a mut trap::Writer,
209214
/// A counter for top-level child nodes
@@ -226,6 +231,7 @@ struct Visitor<'a> {
226231
impl<'a> Visitor<'a> {
227232
fn new(
228233
source: &'a [u8],
234+
diagnostics_writer: &'a mut diagnostics::LogWriter,
229235
trap_writer: &'a mut trap::Writer,
230236
path: &'a str,
231237
file_label: trap::Label,
@@ -236,6 +242,7 @@ impl<'a> Visitor<'a> {
236242
path,
237243
file_label,
238244
source,
245+
diagnostics_writer,
239246
trap_writer,
240247
toplevel_child_counter: 0,
241248
ast_node_info_table_name: format!("{}_ast_node_info", language_prefix),

ruby/extractor/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod diagnostics;
12
mod extractor;
23
mod trap;
34

@@ -59,6 +60,7 @@ fn encoding_from_name(encoding_name: &str) -> Option<&(dyn encoding::Encoding +
5960
}
6061

6162
fn main() -> std::io::Result<()> {
63+
let diagnostics = diagnostics::DiagnosticLoggers::new("ruby");
6264
tracing_subscriber::fmt()
6365
.with_target(false)
6466
.without_time()
@@ -119,6 +121,7 @@ fn main() -> std::io::Result<()> {
119121
lines
120122
.par_iter()
121123
.try_for_each(|line| {
124+
let mut diagnostics_writer = diagnostics.logger();
122125
let path = PathBuf::from(line).canonicalize()?;
123126
let src_archive_file = path_for(&src_archive_dir, &path, "");
124127
let mut source = std::fs::read(&path)?;
@@ -131,6 +134,7 @@ fn main() -> std::io::Result<()> {
131134
erb,
132135
"erb",
133136
&erb_schema,
137+
&mut diagnostics_writer,
134138
&mut trap_writer,
135139
&path,
136140
&source,
@@ -194,6 +198,7 @@ fn main() -> std::io::Result<()> {
194198
language,
195199
"ruby",
196200
&schema,
201+
&mut diagnostics_writer,
197202
&mut trap_writer,
198203
&path,
199204
&source,

0 commit comments

Comments
 (0)