scyros 0.4.0

A framework to design sound, reproducible and scalable mining repositories studies on GitHub.
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2025 Andrea Gilot
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Utility functions for working with CSV files.

use super::fs::*;
use anyhow::{anyhow, bail, Context, Result};
use csv::{Reader, StringRecord};
use std::collections::HashMap;
use std::fs::File;
use std::hash::Hash;
use std::io;
use std::io::BufWriter;
use std::io::Write;
use std::str::FromStr;

#[derive(Debug)]
pub struct CSVFile {
    path: String,
    writer: Option<BufWriter<File>>,
}

impl Write for CSVFile {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.writer
            .as_mut()
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::ReadOnlyFilesystem,
                    "The file is not opened in write mode",
                )
            })?
            .write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.writer
            .as_mut()
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::ReadOnlyFilesystem,
                    "The file is not opened in write mode",
                )
            })?
            .flush()
    }
}

impl CSVFile {
    /// Opens a CSV file in the specified mode.
    ///
    /// # Arguments
    ///
    /// * `path` - The path to the CSV file.
    /// * `mode` - The mode to open the file in.
    ///
    /// # Returns
    ///
    /// A CSV file in the specified mode or an error if the file could not be opened.
    pub fn new(path: &str, mode: FileMode) -> Result<Self> {
        Ok(Self {
            path: path.to_string(),
            writer: {
                let file: File = open_file(path, mode)?;
                if mode == FileMode::Read {
                    None
                } else {
                    Some(BufWriter::new(file))
                }
            },
        })
    }

    // TODO: Test
    /// Switches the mode of the file.
    ///
    /// # Arguments
    ///
    /// * `mode` - The new mode to switch to.
    ///
    /// # Returns
    ///
    /// The file in the new mode or an error if the file could not be opened.
    pub fn switch_mode(self, mode: FileMode) -> Result<Self> {
        Self::new(&self.path, mode)
    }

    /// Opens a reader for this file
    fn read(&self) -> Result<Reader<File>> {
        if self.writer.is_some() {
            bail!(
                "Cannot read from {} since it is in write-only mode",
                self.path
            )
        } else {
            Ok(csv::ReaderBuilder::new().from_reader(open_file(&self.path, FileMode::Read)?))
        }
    }

    // TODO: Test
    /// Writes a header to this file if it is empty.
    ///
    /// # Arguments
    ///
    /// * `header` - The header fields, escaped per RFC 4180.
    ///
    /// # Returns
    ///
    /// An error if the file is in read-only mode or the write fails.
    pub fn write_header<I, S>(&mut self, header: I) -> Result<()>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<[u8]>,
    {
        let writer = self.writer.as_mut().ok_or_else(|| {
            anyhow!(
                "Cannot write to {} since it is in read-only mode",
                self.path
            )
        })?;
        if writer.get_ref().metadata()?.len() != 0 {
            return Ok(());
        }
        let mut csv_writer = csv::WriterBuilder::new()
            .has_headers(false)
            .from_writer(writer);
        csv_writer.write_record(header)?;
        csv_writer.flush()?;
        Ok(())
    }

    /// Writes one record to this file, escaping fields per RFC 4180.
    ///
    /// # Arguments
    ///
    /// * `fields` - The fields of the record.
    ///
    /// # Returns
    ///
    /// An error if the file is in read-only mode or the write fails.
    pub fn write_record<I, S>(&mut self, fields: I) -> Result<()>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<[u8]>,
    {
        let writer = self.writer.as_mut().ok_or_else(|| {
            anyhow!(
                "Cannot write to {} since it is in read-only mode",
                self.path
            )
        })?;
        let mut csv_writer = csv::WriterBuilder::new()
            .has_headers(false)
            .from_writer(writer);
        csv_writer.write_record(fields)?;
        csv_writer.flush()?;
        Ok(())
    }

    // TODO: Test
    /// Extracts information from the records of this file and returns it in a vector.
    ///
    /// # Arguments
    ///
    /// * `extractor` - A function that extracts information from a record.
    ///
    /// # Returns
    ///
    /// A vector containing the extracted information or an error if the file could not be read or the information could not be extracted.
    pub fn extract<T, F>(&self, extractor: F) -> Result<Vec<T>>
    where
        F: Fn(usize, StringRecord) -> Result<T>,
    {
        let mut res = Vec::<T>::new();

        for (line, x) in self.read()?.records().enumerate() {
            res.push(extractor(line, x?)?);
        }
        Ok(res)
    }

    /// Loads the values of a column from this file into a vector.
    /// If the specified column cannot be parsed, an error is returned.
    /// If the file does not exist or is invalid, an error is returned.
    ///
    /// # Arguments
    ///
    /// * `i` - The index of the column containing the ids.
    ///
    /// # Returns
    ///
    /// A vector containing the values of the specified column or an error if the file could not be read or the column could not be parsed.
    ///
    pub fn column<T>(&self, i: usize) -> Result<Vec<T>>
    where
        T: FromStr,
    {
        self.extract(|line, record| {
            record
                .get(i)
                .with_context(|| {
                    format!(
                        "Record {}: Record length is {} but the requested column is {}",
                        line,
                        record.len(),
                        i,
                    )
                })
                .and_then(|entry| {
                    entry
                        .parse::<T>()
                        .map_err(|_| anyhow!("Could not parse record {line}"))
                })
        })
    }

    /// Loads the lines of this file into a hash map.
    /// One of the columns of the file serves as the keys of the hash map.
    ///
    /// # Arguments
    ///
    /// * `cache_path` - The cache file.
    /// * `i` - The index of the column containing the ids of the projects.
    ///
    /// # Returns
    ///
    /// A hash map containing the lines of the file indexed by the specified column or an error if the file could not be parsed.
    ///
    pub fn indexed_lines<T>(&self, i: usize) -> Result<HashMap<T, String>>
    where
        T: FromStr + Eq + Hash,
    {
        let content: String = std::fs::read_to_string(&self.path)?;
        let mut reader = csv::ReaderBuilder::new().from_reader(content.as_bytes());
        // The text of each record is taken from where the CSV reader found it, so that blank lines and
        // quoted fields spanning several lines do not shift the records against their keys.
        let mut starts_and_keys: Vec<(usize, T)> = Vec::new();
        for (line, record) in reader.records().enumerate() {
            let record = record?;
            let start = record
                .position()
                .with_context(|| format!("Record {line} has no position"))?
                .byte() as usize;
            let key: T = record
                .get(i)
                .with_context(|| format!("Record {line} has no column {i}"))?
                .parse()
                .map_err(|_| anyhow!("Could not parse record {line}"))?;
            starts_and_keys.push((start, key));
        }
        let ends: Vec<usize> = starts_and_keys
            .iter()
            .skip(1)
            .map(|(start, _)| *start)
            .chain(std::iter::once(content.len()))
            .collect();
        Ok(starts_and_keys
            .into_iter()
            .zip(ends)
            .map(|((start, key), end)| {
                (
                    key,
                    content[start..end].trim_matches(['\r', '\n']).to_string(),
                )
            })
            .collect())
    }
}

/// Cleans a string to be safely stored in a CSV file by removing quotes and replacing commas and newlines with spaces.
///
/// # Arguments
///
/// * `s` - The string to clean.
///
/// # Returns
///
/// A cleaned string safe for CSV storage.
pub fn clean_string_to_csv(s: &str) -> String {
    s.replace("\"", "")
        .replace(",", " ")
        .lines()
        .collect::<Vec<&str>>()
        .join(" ")
        .trim()
        .to_string()
}

#[cfg(test)]
mod tests {

    use std::net::IpAddr;

    use anyhow::ensure;

    use super::*;

    #[test]
    fn new_test() -> Result<()> {
        let before = std::fs::read_to_string("tests/data/small_file.csv")?;
        CSVFile::new("tests/data/small_file.csv", FileMode::Read)?;
        CSVFile::new("tests/data/small_file.csv", FileMode::Append)?;
        let after = std::fs::read_to_string("tests/data/small_file.csv")?;
        assert_eq!(before, after);
        CSVFile::new("tests/data/empty.csv", FileMode::Overwrite)?;
        ensure!(CSVFile::new("tests/data/non_existent.csv", FileMode::Read).is_err());
        CSVFile::new("tests/data/non_existent.csv", FileMode::Append)?;
        delete_file("tests/data/non_existent.csv", false)?;
        CSVFile::new("tests/data/non_existent.csv", FileMode::Overwrite)?;
        delete_file("tests/data/non_existent.csv", false)
    }

    #[test]
    fn read_test() -> Result<()> {
        CSVFile::new("tests/data/small_file.csv", FileMode::Read)?.read()?;
        ensure!(CSVFile::new("tests/data/small_file.csv", FileMode::Append)?
            .read()
            .is_err());
        ensure!(CSVFile::new("tests/data/empty.csv", FileMode::Overwrite)?
            .read()
            .is_err());
        Ok(())
    }

    #[test]
    fn empty_column_test() -> Result<()> {
        let file = CSVFile::new("tests/data/empty.csv", FileMode::Read)?;
        for i in 0..10 {
            let ids = file.column::<u64>(i)?;
            ensure!(ids.is_empty());

            let ids = file.column::<u64>(i)?;
            ensure!(ids.is_empty());
        }
        Ok(())
    }

    #[test]
    fn column_test() -> Result<()> {
        let file = CSVFile::new("tests/data/small_file.csv", FileMode::Read)?;

        let ids = file.column::<usize>(0)?;
        assert_eq!(ids, vec![0, 1, 2, 3]);

        let names = file.column::<String>(1)?;
        assert_eq!(names, vec!["a", "b", "c", "d"]);
        let forks = file.column::<u8>(2)?;
        assert_eq!(forks, vec![1, 0, 1, 0]);

        let ips = file.column::<IpAddr>(3);
        ensure!(ips.is_err());

        let ids = file.column::<i32>(1);
        ensure!(ids.is_err());

        let file = CSVFile::new("tests/data/invalid_csv.csv", FileMode::Read)?;
        ensure!(file.column::<i8>(0).is_err());
        Ok(())
    }
    #[test]
    fn indexed_lines_with_blank_lines_and_quoted_newlines() -> Result<()> {
        let path = "tests/data/indexed_lines_irregular.csv";
        write_file(path, b"id,name\n1,a\n\n2,\"b\nc\"\r\n3,d")?;
        let lines = CSVFile::new(path, FileMode::Read)?.indexed_lines::<u32>(0);
        delete_file(path, false)?;
        let lines = lines?;
        assert_eq!(lines.get(&1).map(String::as_str), Some("1,a"));
        assert_eq!(lines.get(&2).map(String::as_str), Some("2,\"b\nc\""));
        assert_eq!(lines.get(&3).map(String::as_str), Some("3,d"));
        Ok(())
    }

    #[test]
    fn indexed_lines_test() -> Result<()> {
        let file = CSVFile::new("tests/data/small_file.csv", FileMode::Read)?;
        let indexed_lines = file.indexed_lines::<i32>(0)?;
        assert_eq!(indexed_lines.len(), 4);
        assert_eq!(
            indexed_lines
                .get(&0)
                .with_context(|| "Could not find index 0")?,
            "0,a,1"
        );
        assert_eq!(
            indexed_lines
                .get(&1)
                .with_context(|| "Could not find index 1")?,
            "1,b,0"
        );
        assert_eq!(
            indexed_lines
                .get(&2)
                .with_context(|| "Could not find index 2")?,
            "2,c,1"
        );
        assert_eq!(
            indexed_lines
                .get(&3)
                .with_context(|| "Could not find index 3")?,
            "3,d,0"
        );

        let indexed_lines = file.indexed_lines::<String>(1)?;
        assert_eq!(indexed_lines.len(), 4);
        assert_eq!(
            indexed_lines
                .get("a")
                .with_context(|| "Could not find index 'a'")?,
            "0,a,1"
        );
        assert_eq!(
            indexed_lines
                .get("b")
                .with_context(|| "Could not find index 'b'")?,
            "1,b,0"
        );
        assert_eq!(
            indexed_lines
                .get("c")
                .with_context(|| "Could not find index 'c'")?,
            "2,c,1"
        );
        assert_eq!(
            indexed_lines
                .get("d")
                .with_context(|| "Could not find index 'd'")?,
            "3,d,0"
        );

        ensure!(file.indexed_lines::<bool>(3).is_err());
        ensure!(file.indexed_lines::<u64>(1).is_err());

        let empty = CSVFile::new("tests/data/empty.csv", FileMode::Read)?;

        let indexed_lines = empty.indexed_lines::<IpAddr>(0)?;
        assert_eq!(indexed_lines.len(), 0);
        Ok(())
    }

    #[test]
    fn write_record_test() -> Result<()> {
        let path = "tests/data/write_record_test.csv";
        let _ = delete_file(path, true);

        let mut file = CSVFile::new(path, FileMode::Overwrite)?;
        file.write_header(["name", "note"])?;
        file.write_record(["alice", "hello"])?;
        file.write_record(["bob", "he said \"hi\""])?;
        file.write_record(["carol", "a, b, c"])?;
        file.flush()?;

        // Read it back via CSVFile and check round-trip.
        let read_file = CSVFile::new(path, FileMode::Read)?;
        let names = read_file.column::<String>(0)?;
        let notes = read_file.column::<String>(1)?;
        assert_eq!(names, vec!["alice", "bob", "carol"]);
        assert_eq!(notes, vec!["hello", "he said \"hi\"", "a, b, c"]);

        delete_file(path, false)?;
        Ok(())
    }
}