Skip to content

Commit 0384e4a

Browse files
committed
rustfmt & clean up
1 parent 8d66d58 commit 0384e4a

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/avro.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use avro_rs::types::Value;
22
use avro_rs::{Codec, Reader};
3-
use std::fs;
43
use glob::glob;
4+
use std::fs;
55
use std::path::PathBuf;
66

77
pub(crate) const NULL: &'static str = "null";
@@ -11,20 +11,21 @@ pub(crate) const CODEC_DEFLATE: &'static str = "deflate";
1111
#[derive(Debug)]
1212
pub(crate) struct AvroFile {
1313
data: Vec<u8>,
14-
path: PathBuf
14+
path: PathBuf,
1515
}
1616

1717
#[derive(Debug)]
18-
pub(crate) struct Avro {
19-
files: Vec<AvroFile>
18+
pub(crate) struct AvroCli {
19+
files: Vec<AvroFile>,
2020
}
2121

22-
impl Avro {
22+
impl AvroCli {
2323
/// Creates an `Avro` as a union of all avros in the received paths
24-
///
24+
///
2525
/// # Arguments
2626
///
2727
/// * `path` - A glob to match against Avro files to load
28+
/// * `codec` - A codec for decompression
2829
pub fn from(path: String, codec: Option<String>) -> Self {
2930
let mut paths: Vec<PathBuf> = Vec::new();
3031
for entry in glob(&path).expect("Failed to read glob pattern") {
@@ -38,50 +39,65 @@ impl Avro {
3839
panic!("No files found")
3940
}
4041

41-
let mut codec_for_decompressing: Codec = Codec::Null;
4242
// TODO: Add `Codec::Snappy`
43+
let mut codec_for_decompressing: Codec = Codec::Null;
4344
if let Some(c) = codec {
4445
if c == CODEC_DEFLATE {
4546
codec_for_decompressing = Codec::Deflate;
4647
}
4748
}
48-
4949
let mut files: Vec<AvroFile> = Vec::new();
5050
for path in paths {
51-
let mut data = fs::read(&path).expect(&format!(
52-
"Could not read from path {0}", path.display())
53-
);
51+
let mut data =
52+
fs::read(&path).expect(&format!("Could not read from path {0}", path.display()));
5453
codec_for_decompressing.decompress(&mut data).expect("Could not successfully decompress Avro file. Make sure that the codec you specified is correct");
5554
files.push(AvroFile { data, path });
5655
}
5756

58-
Avro { files }
57+
AvroCli { files }
5958
}
6059

60+
/// Get all the names of the columns.
61+
/// Relies on the first record
6162
pub fn get_all_field_names(&self) -> Vec<String> {
6263
let first_file = &self.files[0];
63-
let mut reader = Reader::new(&first_file.data[..]).expect(&format!("Could not read Avro file {}", first_file.path.display()));
64-
if let Ok(Value::Record(fields)) = reader.next().expect("Avro must have at least one record row to infer schema") {
65-
fields.iter().map(|(f, _)| f.to_owned()).collect::<Vec<String>>()
64+
let mut reader = Reader::new(&first_file.data[..]).expect(&format!(
65+
"Could not read Avro file {}",
66+
first_file.path.display()
67+
));
68+
if let Ok(Value::Record(fields)) = reader
69+
.next()
70+
.expect("Avro must have at least one record row to infer schema")
71+
{
72+
fields
73+
.iter()
74+
.map(|(f, _)| f.to_owned())
75+
.collect::<Vec<String>>()
6676
} else {
6777
Vec::new()
6878
}
6979
}
7080

81+
/// Get all columns and values
82+
///
83+
/// # Arguments
84+
/// * `fields_to_get` - Names of the columns to retrieve
7185
pub fn get_fields(&self, fields_to_get: Vec<String>) -> Vec<Vec<String>> {
7286
let mut extracted_fields: Vec<Vec<String>> = Vec::new();
7387
for file in &self.files {
74-
let reader = Reader::new(&file.data[..]).expect(&format!("Could not read Avro file {}", file.path.display()));
88+
let reader = Reader::new(&file.data[..])
89+
.expect(&format!("Could not read Avro file {}", file.path.display()));
7590

7691
for (i, row) in reader.enumerate() {
7792
let row = row.expect(&format!("Could not parse row {} from the Avro", i));
7893
if let Value::Record(fields) = row {
7994
let mut extracted_fields_for_row: Vec<String> = Vec::new();
8095
for field_name in &fields_to_get {
81-
let field_value_to_insert = match fields.iter().find(|(n, _)| n == field_name) {
82-
Some((_, val)) => format_avro_value(&val),
83-
None => NA.to_owned()
84-
};
96+
let field_value_to_insert =
97+
match fields.iter().find(|(n, _)| n == field_name) {
98+
Some((_, val)) => format_avro_value(&val),
99+
None => NA.to_owned(),
100+
};
85101
extracted_fields_for_row.push(field_value_to_insert);
86102
}
87103
extracted_fields.push(extracted_fields_for_row);

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
///! A CLI for manipulating [AVRO](https://avro.apache.org/) files.
44
///!
55
///! This crate currently expects each line to be a [Record](https://avro.apache.org/docs/1.8.1/spec.html#schema_record).
6-
use avro::Avro;
6+
use avro::AvroCli;
77
use failure::Error;
88
use prettytable::{color, Attr, Cell, Row, Table};
99
use regex::Regex;
@@ -42,9 +42,9 @@ fn main() -> Result<(), Error> {
4242
fields_to_get,
4343
path,
4444
search,
45-
codec
45+
codec,
4646
} => {
47-
let avro = Avro::from(path, codec);
47+
let avro = AvroCli::from(path, codec);
4848
let fields_to_get = if fields_to_get.is_empty() {
4949
avro.get_all_field_names()
5050
} else {
@@ -86,7 +86,8 @@ fn main() -> Result<(), Error> {
8686
.filter_map(|v| {
8787
let mut cell = Cell::new(v);
8888
if let Some(search) = &search {
89-
let search = Regex::new(&search).expect("Regular expression is invalid");
89+
let search =
90+
Regex::new(&search).expect("Regular expression is invalid");
9091
if search.is_match(v) {
9192
cell.style(Attr::Bold);
9293
cell.style(Attr::ForegroundColor(color::GREEN));

0 commit comments

Comments
 (0)